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 ...
随机推荐
- SpringBoot(十五)-- 修改SpringBoot默认的错误页面
将以下代码放置到 main方法中.然后在resources 中的static中新建404.html.405.html,这里可以自定义错误编码,不局限于这两个. @Bean public Embedde ...
- jquery-ui弹框登录前端写法
新建一个div: <div class="container" id="loginForm" hidden> <h4 class=" ...
- Phoenix系列:原子的Upsert
Phoenix的插入语句是Upsert,Update和Insert的组合语义.即,如果数据表中没有这条记录那么插入这条记录,如果有则更新.判断是否存在相同的数据是使用ON DUPLICATE KEY来 ...
- 错误票据|2013年蓝桥杯B组题解析第七题-fishers
错误票据 某涉密单位下发了某种票据,并要在年终全部收回. 因为工作人员疏忽,在录入ID号的时候发生了一处错误,造成了某个ID断号,另外一个ID重号. 你的任务是通过编程,找出断号的ID和重号的ID. ...
- [No000018F]Vim自动缩进配置、原理和tab键替换空格-Vim使用技巧(4)
一.Vim缩进介绍 在没有设置Vim自动缩进的条件下,可以手动使用Vim命令对特定行进行缩进处理.在Vim插入模式下,按下 Tab 键时默认会输入一个制表符,可通过Vim配置项将 Tab 替换为空格, ...
- [No0000182]Parallel Programming with .NET-Partitioning in PLINQ
Every PLINQ query that can be parallelized starts with the same step: partitioning. Some queries ma ...
- CSS3 ::selection 选择器
::selection { color:#ff0000; } ::-moz-selection { color:#ff0000; }详见 http://www.css88.com/archives/5 ...
- MVC 实用架构设计(三)——EF-Code First(5):二级缓存
一.前言 今天我们来谈谈EF的缓存问题. 缓存对于一个系统来说至关重要,但是是EF到版本6了仍然没有见到有支持查询结果缓存机制的迹象.EF4开始会把查询语句编译成存储过程缓存在Sql Server中, ...
- 9 tips to improve spoken english
https://www.youtube.com/watch?v=FUW_FN8uzy0 1. don't translate everything into your native language ...
- Copycat - CopycatServer
Server被拉起有两种方式, Address address = new Address("123.456.789.0", 5000); CopycatServer.Builde ...