threading线程例子 (27-08)
利用阻塞的时间空闲去执行另一个子线程
import threading
from time import ctime, sleep def music(func):
for i in range(2):
print(func, ctime()) # 1 执行 # 4 执行
sleep(1) # 阻塞
print("end music", ctime()) # 3 执行 # 5 执行 def move(func):
for i in range(2):
print(func, ctime()) # 2 执行 # 7 执行
sleep(5) # 阻塞
print("end move", ctime()) # 6 执行 # 8 执行 threads=[]
t1 = threading.Thread(target=music,args=("小苹果",))
threads.append(t1)
t2 = threading.Thread(target=move,args=("华尔街之狼",))
threads.append(t2) if __name__ == "__main__":
for t in threads: # 遍历执行两个子线程
t.start()
# 整体执行时间为10秒
代码运行结果:
小苹果 Fri Sep 7 16:19:09 2018
华尔街之狼 Fri Sep 7 16:19:09 2018
end music Fri Sep 7 16:19:10 2018
小苹果 Fri Sep 7 16:19:10 2018
end music Fri Sep 7 16:19:11 2018
end move Fri Sep 7 16:19:14 2018
华尔街之狼 Fri Sep 7 16:19:14 2018
end move Fri Sep 7 16:19:19 2018
threading线程例子 (27-08)的更多相关文章
- python并发编程之threading线程(一)
进程是系统进行资源分配最小单元,线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位.进程在执行过程中拥有独立的内存单元,而多个线程共享内存等资源. 系列文章 py ...
- Python学习笔记--threading线程
通过线程来实现多任务并发.提高性能.先看看例子. #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2020-03-02 21:10:39 ...
- pythonl练习笔记——threading线程中的事件Event
1 事件Event 使用方法:e = threading.Event() Event对象主要用于线程间通信,确切地说是用于主线程控制其他线程的执行. Event事件提供了三个方法:wait等待.cle ...
- DELPHI线程例子-FC
{优秀的数据库应用应当充分考虑数据库访问的速度问题.通常可以通过优化数据库.优化 查询语句.分页查询等途径收到明显的效果.即使是这样,也不可避免地会在查询时闪现一个带有 SQL符号的沙漏,即鼠标变成了 ...
- 多进程 multiprocessing 多线程Threading 线程池和进程池concurrent.futures
multiprocessing.procsess 定义一个函数 def func():pass 在if __name__=="__main__":中实例化 p = process( ...
- Python Threading 线程/互斥锁/死锁/GIL锁
导入线程包 import threading 准备函数线程,传参数 t1 = threading.Thread(target=func,args=(args,)) 类继承线程,创建线程对象 class ...
- threading线程中的方法(27-11)
t1.start() # 执行线程 t1.join() # 阻塞 t1.setDaemon(True) #守护线程 threading.current_thread() # 查看执行的是哪一个线程 t ...
- import threading线程进程
cpu在执行一个子线程的时候遇到sleep就会利用这段停顿时间去执行另一个子线程.两个子线程谁先跳出sleep就执行谁. import threadingimport time start = tim ...
- java线程例子登山
Through its implementation, this project will familiarize you with the creation and execution of thr ...
随机推荐
- 解决 AUTH` failed: ERR Client sent AUTH, but no password is set [tcp://127.0.0.1:6379]
页面报错: ConnectionException In AbstractConnection.php line 155 AUTH` failed: ERR Client sent AUTH, but ...
- Thymeleaf语法总结
Thymeleaf是Spring boot推荐使用的模板引擎. 一.th属性 html有的属性,Thymeleaf基本都有,而常用的属性大概有七八个.其中th属性执行的优先级从1~8,数字越低优先级越 ...
- storm集群的安装
storm图解 storm的基本概念 Topologies:拓扑,也俗称一个任务 Spoults:拓扑的消息源 Bolts:拓扑的处理逻辑单元 tuple:消息元组,在Spoults和Bolts传递数 ...
- NX二次开发-打开文件夹,并同时选中指定文件
NX9+VS2012 #include <uf.h> #include <uf_ui.h> #include <uf_part.h> #include <at ...
- flutter 修饰盒子
decoration: BoxDecoration( borderRadius: BorderRadius.circular(), //圆角 gradient: RadialGradient( col ...
- 2019 牛客多校第二场 H Second Large Rectangle
题目链接:https://ac.nowcoder.com/acm/contest/882/H 题目大意 给定一个 n * m 的 01 矩阵,求其中第二大的子矩阵,子矩阵元素必须全部为 1.输出其大小 ...
- Metasploit 模块和位置
Metasploit Framework由许多的模块组成的. 一.Exploits(漏洞模块) 定义为使用“有效载荷(payloads)”的模块 没有“有效载荷”的攻击是辅助模块 二.Payloads ...
- 专题:“find -perm”
Search for files which have read and write permission for their owner, and group, but which other us ...
- Git 学习(一)安装 Git
这里写自定义目录标题 这一章介绍怎么安装 Git 大家都是开发老司机,就不简介什么是 Git 了,直接开花. 在 Linux 上安装Git 在 Windows 上安装 Git 初次使用 Git 前的配 ...
- de4Dot用法 解决 .net程序 reflecter反编译 “索引超出了数组界限”问题
de4Dot 反混淆工具.当你反编译 .net写的dll 或exe时出现:索引超出了数组界限 问题时 可以去网上下这个工具,通过cmd命令 打开de4dot的exe 空格 dll的全路径. 这样 :D ...