python的_thread模块来实现多线程(<python核心编程例子>)
python中_thread模块是一个低级别的多线程模块,它的问题在于主线程运行完毕后,会立马把子线程给结束掉,不加处理地使用_thread模块是不合适的.这里把书中讲述的有关_thread使用的例子自己实现了一遍,做以记录.
#例子一:自己手动在主线程中设置等待时间
import _thread
from time import ctime, sleep def loop0():
print("loop0 starts at:{}".format(ctime()))
sleep(4)
print("loop0 ends at:{}".format(ctime())) def loop1():
print("loop1 starts at:{}".format(ctime()))
sleep(2)
print("loop1 ends at:{}".format(ctime())) if __name__ == "__main__":
print("start_time:{}".format(ctime()))
_thread.start_new_thread(loop0, ())
_thread.start_new_thread(loop1, ())
#此处设置sleep(6)是因为_thread主线程结束后,会马上杀死其他线程
sleep(6)
print("end_time:{}".format(ctime()))
#例子二:通过锁可以实现所有线程全部运行后立即退出
import _thread
from time import ctime, sleep #每个loop等待的时间
wait_time_list = [4, 2] def loop(i, wait_time, lock):
"""
根据传入参数创建多个loop函数
:param i:
:param wait_time:
:param lock:
:return:
"""
print("loop{} starts at:{}".format(i, ctime()))
sleep(wait_time)
print("loop{} ends at:{}".format(i, ctime()))
#释放锁
lock.release() def main():
print("start_time:", ctime())
nloops = range(len(wait_time_list))
locks = []
#创建锁,上锁
for i in nloops:
lock = _thread.allocate_lock()
lock.acquire()
locks.append(lock) #之所以另起一个循环,是为了尽量保证所有线程能够同时启动,因为上面的循环中锁的操作也要花费一些时间
for i in nloops:
_thread.start_new_thread(loop, (i, wait_time_list[i], locks[i]))#loop函数中的参数放到元组里 #等待所有的子线程释放锁后,结束主线程.(等待时间取决于执行时间最长的子线程,假如第一个子线程执行时间最长,等它执行完毕,下面的循环就不会再进入了.)
for i in nloops:
#注意这里locked()要带括号
while locks[i].locked():
pass
print("all done! end_time:", ctime()) if __name__ == '__main__':
main()
python的_thread模块来实现多线程(<python核心编程例子>)的更多相关文章
- python函数,模块及eclipse配置python开发环境
一.eclipse的使用 1.作用 (1)最好用的IDE (2)可调式debug (3)查看可执行过程 (4)可查看源代码 2.安装eclipse及配置 目录安装Pythonpython for ec ...
- Python的功能模块[1] -> struct -> struct 在网络编程中的使用
struct模块 / struct Module 在网络编程中,利用 socket 进行通信时,常常会用到 struct 模块,在网络通信中,大多数传递的数据以二进制流(binary data)存在. ...
- python安装mysqlclient模块报fatal error: Python.h:解决方法
在搭建Flask框架安装mysqlclient模块时候老是报fatal error: Python.h:错误,折腾老半天,百度了老半天看了不少大神帖子,就是没解决, 后来发现这不是个BUG,都是自己的 ...
- python多线程与_thread模块
进程与线程 1.进程:计算机程序只是存储在磁盘中的可执行二进制(或其他类型)的文件.只有把他们加载到内存中并被操作系统调用,才具有其生命周期.进程则是一个执行中的程序.每个进程都拥有自己的地址空间,内 ...
- Python学习——struct模块的pack、unpack示例
he struct module includes functions for converting between strings of bytes and native Python data t ...
- 安装python的pip模块
安装python的pip模块 网址https://pypi.python.org/pypi/pip 选择,点击下载 将文件解压到C:\Users\Administrator\AppData\Local ...
- Python学习--Selenium模块
1. Python学习--Selenium模块介绍(1) 2.Python学习--Selenium模块学习(2) 其他: 1. Python学习--打码平台
- Python学习---重点模块的学习【all】
time [时间模块] import time # print(help(time)) # time模块的帮助 print(time.time()) # 时间戳 print(time.cloc ...
- python 之 itertools模块
官方:https://yiyibooks.cn/xx/python_352/library/itertools.html 参考: https://blog.csdn.net/neweastsun/ar ...
随机推荐
- [原]openstack-kilo--issue(十七) heat创建网络Quota exceeded for resources OverQuotaClient: resources.dmz_protected_network_sub
----- 1 ------- 在使用heat创建网络的时候,报错如下 INFO heat.engine.stack [-] Stack CREATE FAILED (mmsc_network_s ...
- vue编程式路由实现新窗口打开
一. 标签实现新窗口打开: 官方文档中说 v-link 指令被 组件指令替代,且 不支持 target=”_blank” 属性,如果需要打开一个新窗口必须要用标签,但事实上vue2版本的 是支持 ta ...
- scala breeze使用替换底层线性库
scala -J-Dcom.github.fommil.netlib.NativeSystemBLAS.natives=mkl_rt.dll -cp "D:\betn\Scala\Scala ...
- Linux----面试
1:tcp和udp的区别 TCP:是面向连接的流传输控制协议,具有高可靠性,确保传输数据的正确性,有验证重发机制,因此不会出现丢失或乱序. UDP:是无连接的数据报服务,不对数据报进行检查与修改,无须 ...
- MyEclipse中同时启动两个tomcat
开发的时候,有些时候需要同时启动两个项目.首先配置tomcat,方法如下:(转载自:http://bendan123812.iteye.com/blog/1716789) 一.把Tomcat复制一份并 ...
- js常用校验
//验证金钱数字obj.regexMoney = function (money) { var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)| ...
- 主席树||可持久化线段树||离散化||[CQOI2015]任务查询系统||BZOJ 3932||Luogu P3168
题目: [CQOI2015]任务查询系统 题解: 是一道很经典的题目.大体思路是抓优先级来当下标做主席树,用时刻作为主席树的版本.然而优先级范围到1e7去了,就离散化一遍.然后把每个事件的开始(s). ...
- [No0000176]Git常用命令速查表(收藏大全)
名词 master: 默认开发分支 origin: 默认远程版本库 Index / Stage:暂存区 Workspace:工作区 Repository:仓库区(或本地仓库) Remote:远程仓库 ...
- [No000012F]WPF(7/7) - 样式,触发器和动画
WPF Tutorial : Beginning [^] WPF Tutorial : Layout-Panels-Containers & Layout Transformation [^] ...
- 启用hive hwi方法
hive启动hwi: ./hive --service hwi ls: cannot access /opt/cdh-5.3.6/hive-0.13.1/lib/hive-hwi-*.war: No ...