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核心编程例子>)的更多相关文章

  1. python函数,模块及eclipse配置python开发环境

    一.eclipse的使用 1.作用 (1)最好用的IDE (2)可调式debug (3)查看可执行过程 (4)可查看源代码 2.安装eclipse及配置 目录安装Pythonpython for ec ...

  2. Python的功能模块[1] -> struct -> struct 在网络编程中的使用

    struct模块 / struct Module 在网络编程中,利用 socket 进行通信时,常常会用到 struct 模块,在网络通信中,大多数传递的数据以二进制流(binary data)存在. ...

  3. python安装mysqlclient模块报fatal error: Python.h:解决方法

    在搭建Flask框架安装mysqlclient模块时候老是报fatal error: Python.h:错误,折腾老半天,百度了老半天看了不少大神帖子,就是没解决, 后来发现这不是个BUG,都是自己的 ...

  4. python多线程与_thread模块

    进程与线程 1.进程:计算机程序只是存储在磁盘中的可执行二进制(或其他类型)的文件.只有把他们加载到内存中并被操作系统调用,才具有其生命周期.进程则是一个执行中的程序.每个进程都拥有自己的地址空间,内 ...

  5. Python学习——struct模块的pack、unpack示例

    he struct module includes functions for converting between strings of bytes and native Python data t ...

  6. 安装python的pip模块

    安装python的pip模块 网址https://pypi.python.org/pypi/pip 选择,点击下载 将文件解压到C:\Users\Administrator\AppData\Local ...

  7. Python学习--Selenium模块

    1. Python学习--Selenium模块介绍(1) 2.Python学习--Selenium模块学习(2) 其他: 1. Python学习--打码平台

  8. Python学习---重点模块的学习【all】

    time     [时间模块] import time # print(help(time)) # time模块的帮助 print(time.time()) # 时间戳 print(time.cloc ...

  9. python 之 itertools模块

    官方:https://yiyibooks.cn/xx/python_352/library/itertools.html 参考: https://blog.csdn.net/neweastsun/ar ...

随机推荐

  1. 一、K3 Cloud 实施指导《K3 Cloud实施手册》

    1.在BOS的单据体取不到序号字段 举例:单据体标识是FValueGrid,序号标识是FSeq,取到的序号标识要写成FValueGrid_FSeq才能识别到 2.k3 Cloud目前不支持在表头字段调 ...

  2. 【zheng环境准备】安装activemq

    1.下载http://activemq.apache.org/activemq-5140-release.html 2.移动到linux机器上 3.解压 tar -zxvf apache-active ...

  3. mysql8.0驱动问题

    <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</a ...

  4. JMS规范概览

    JMS(java message service)常用的消息模式有两种:点对点(PTP)和订阅发布(Pub/Sub) Clients A 和 B是消息生产者,以两种不同的目的地向Clients C, ...

  5. 安装 powerline

    使用说明: https://powerline.readthedocs.io/en/latest/usage.html ~ vim,在 .vimrc 中添加配置 set rtp+=/usr/lib/p ...

  6. asp.net Ajax调用Aspx后台方法

    Ajax调用的前提(以aspx文件为例:) 1.首先需要在aspx文件后台中引用using System.Web.Services; 2.需要调用的方法必须是公共的(public).静态的(stati ...

  7. 洛谷 P1583魔法照片 & P1051谁拿了最多奖学金 & P1093奖学金

    题目:https://www.luogu.org/problemnew/show/P1583 思路:sort sort sort //#include<bits/stdc++.h> #in ...

  8. SET ARITHABORT {ON | OFF}讲解

    SET ARITHABORT {ON | OFF} 在查询处理过程中如果出现溢出错误或把零作为除数则查询处理是否该终止如 果为ON 则表示终止查询如果为OFF 则表示返回一个警告信息对于进行算术运 算 ...

  9. angular 使用dialog的经验

    利用angular在近期的工作中使用了dialog的方式,总结下经验 由于dialog显示的内容不同,需要用到angular 的ng-include加载不同的文件1 dialog利用指令的方式 app ...

  10. 2016年蓝桥杯省赛A组c++第3题(图论)

    /* 有一个含有10个格子的图形,现用0~9填充,连续的数不能填充在相邻的格子中(包括对角线相邻). 现每个数只能填写一次,问有多少种填充方法? 0111 1111 1110 (1表示有格子,0表示没 ...