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 ...
随机推荐
- Android进阶:ListView性能优化异步加载图片 使滑动效果流畅
ListView 是一种可以显示一系列项目并能进行滚动显示的 View,每一行的Item可能包含复杂的结构,可能会从网络上获取icon等的一些图标信息,就现在的网络速度要想保持ListView运行的很 ...
- php实现多域名共享session会话
php会话机制参考:我的随笔 缘起 网站,通常会有多个服务器,多个子域名,每个节点运行着不同模块.有时为了整体体验,用户用同一个用户名.密码浏览整站,不用重复登录.这时候就需要多服务器共享sessio ...
- ftp服务器Serv-U 设置允许自动创建不存在的目录
一.由来 最近改写了项目中ftp上传部分的代码. 用到的组件为: <dependency> <groupId>commons-net</groupId> <a ...
- Unity 给Mono脚本添加Try Catch工具
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Run ...
- aspectj 注解
aspectj是一个面向切面编程的框架,即实现了aop,这不是spring,它本身很小,方便简洁,spring将其整合成自己的. 与spring本身对aop的支持不同,顾问采用正则表达式或者方法名或通 ...
- mysql的多表查询join
http://blog.csdn.net/jintao_ma/article/details/51260458 https://zhidao.baidu.com/question/1304158100 ...
- [No0000183]Parallel Programming with .NET-How PLINQ processes an IEnumerable<T> on multiple cores
As Ed Essey explained in Partitioning in PLINQ, partitioning is an important step in PLINQ execution ...
- [No0000F1]js获取喜马拉雅和荔枝FM电台专辑音频
荔枝FM小书签.txt javascript: (function() { if ($('#down_url')) { $('#down_url').remove(); }; $(document.b ...
- tensorflow 的tf.where详解
最近在用到数据筛选,观看代码中有tf.where()的用法,不是很常用,也不是很好理解.在这里记录一下 tf.where( condition, x=None, y=None, name=None ) ...
- Gym - 101375H MaratonIME gets candies 交互题
交互题介绍:https://loj.ac/problem/6 题意:输出Q X ,读入><= 来猜数,小于50步猜出就算过样例 题解:根本不需要每次输出要打cout.flush()... ...