Class Event
{
__init__(self) clear(self) is_set(self) set(self) wait(self,timeout=None) }

is_set(self)

if and only if 内部标志为真时返回Ture

wait(self,timeout=N)

如果内部标志=Ture,立即返回
如果内部标志=False,就阻塞(等待),直到另一个thread调用了set()方法,它的内部标志变为了Ture
如果设置了timeout,则至多等待N秒时间

set(self)

将内部变量设置为True。所有等待它成真的线程都被唤醒(awake)了

clear(self)

将内部变量设置为False

class TestThread(threading.Thread):
def __init__(self,name,event):
super(TestThread,self).__init__()
self.name=name
self.event=event def run(self):
logging.info("{} start".format(self.name))
self.event.wait()
logging.info("{} finished".format(self.name)) def main():
event=threading.Event() threads=[] for i in range(1,4):
threads.append(TestThread(str(i),event)) logging.info("main thread start") event.clear() for thread in threads:
thread.start() logging.info("sleep 5s...")
time.sleep(5)
logging.info("now awake other threads")
event.set() if __name__ == '__main__':
main() '''
19:43:50 [MainThread] main thread start
19:43:50 [1] 1 start
19:43:50 [2] 2 start
19:43:50 [3] 3 start
19:43:50 [MainThread] sleep 5s...
19:43:55 [MainThread] now awake other threads
19:43:55 [1] 1 finished
19:43:55 [2] 2 finished
19:43:55 [3] 3 finished
'''
class TestThread(threading.Thread):
def __init__(self,name,event):
super(TestThread,self).__init__()
self.name=name
self.event=event def run(self):
logging.info("{} start".format(self.name))
self.event.wait(5)
logging.info("{} finished".format(self.name)) def main():
event=threading.Event() threads=[] for i in range(1,4):
threads.append(TestThread(str(i),event)) logging.info("main thread start") event.clear() for thread in threads:
thread.start() logging.info("sleep 10s...")
time.sleep(10)
logging.info("10s过去了")
event.set() if __name__ == '__main__':
main() '''
19:48:36 [MainThread] main thread start
19:48:36 [1] 1 start
19:48:36 [2] 2 start
19:48:36 [3] 3 start
19:48:36 [MainThread] sleep 10s...
19:48:41 [1] 1 finished
19:48:41 [2] 2 finished
19:48:41 [3] 3 finished
19:48:46 [MainThread] 10s过去了
'''

《Python:super没那么简单》

《你不知道的super》

[PY3]——threading.Event的更多相关文章

  1. threading event

    #!usr/bin/env python 2 #coding: utf-8 3 #Author: Andy 4 5 import threading 6 import time 7 8 def pro ...

  2. Python多线程的threading Event

    Python threading模块提供Event对象用于线程间通信.它提供了一组.拆除.等待用于线程间通信的其他方法. event它是沟通中最简单的一个过程之中,一个线程产生一个信号,号.Pytho ...

  3. {Python之线程} 一 背景知识 二 线程与进程的关系 三 线程的特点 四 线程的实际应用场景 五 内存中的线程 六 用户级线程和内核级线程(了解) 七 python与线程 八 Threading模块 九 锁 十 信号量 十一 事件Event 十二 条件Condition(了解) 十三 定时器

    Python之线程 线程 本节目录 一 背景知识 二 线程与进程的关系 三 线程的特点 四 线程的实际应用场景 五 内存中的线程 六 用户级线程和内核级线程(了解) 七 python与线程 八 Thr ...

  4. pythonl练习笔记——threading线程中的事件Event

    1 事件Event 使用方法:e = threading.Event() Event对象主要用于线程间通信,确切地说是用于主线程控制其他线程的执行. Event事件提供了三个方法:wait等待.cle ...

  5. 人生苦短之我用Python篇(线程/进程、threading模块:全局解释器锁gil/信号量/Event、)

    线程: 有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元.是一串指令的集合.线程是程序中一个单一的顺序控制流程.进程内一个相对独立的.可调度的执行单元,是 ...

  6. python第五十一天----线程,Event,队列

    进程与线程的区别: 线程==指令集,进程==资源集  (线程集) 1.同一个进程中的线程共享内存空间,进程与进程之间是独立的 2.同一个进程中的线程是可以直接通讯交流的,进程与间通讯必需通过一个中间的 ...

  7. Python标准模块--threading

    1 模块简介 threading模块在Python1.5.2中首次引入,是低级thread模块的一个增强版.threading模块让线程使用起来更加容易,允许程序同一时间运行多个操作. 不过请注意,P ...

  8. python 线程之 threading(三)

    python 线程之 threading(一)http://www.cnblogs.com/someoneHan/p/6204640.html python 线程之 threading(二)http: ...

  9. python成长之路【第十一篇】:网络编程之线程threading模块

    一.threading模块介绍 threading 模块建立在 _thread 模块之上.thread 模块以低级.原始的方式来处理和控制线程,而 threading 模块通过对 thread 进行二 ...

随机推荐

  1. const 用法全面总结

    C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性,本人根据各方面查到的资料进行总结如下,期望对朋友们有所帮助. Const 是C++中常用的类型修饰符,常类型是指使用类 ...

  2. WebService 工作原理及实例教程

    一.WebService到底是什么? 先来看下标准的定义:Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统 ...

  3. 分享一个经验,代码打开mysql链接,执行存储过程时,提示:Table 'mysql.proc' doesn't exist

    先说说的场景 老项目,因为服务器升级了mysql数据库版本,从5.7.13升到8.0.15 然而代码里面有直连数据的访问,通过执行存储过程来查询数据的业务,此时抛出异常   Table 'mysql. ...

  4. Win(Phone)10开发第(1)弹,桌面和手机的扩展API,还我后退键

    喜大普奔的win10 uap开发预览版终于出了,这次更新跟8.1的变化不是很大,但是将原本win8.1和wp8.1uap的分项目的形式,改为了整合成一个项目,经过一次编译打包成一个appx包,实现了无 ...

  5. 基于Easyui框架的datagrid绑定数据,新增,修改,删除方法(四)

    @{ ViewBag.Title = "xxlist"; } <script type="text/javascript" language=" ...

  6. python远程执行命令

    def run(): try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ...

  7. jieba库的使用与词云

    一.准备 在制作词云之前我们需要自行安装三个库,它们分别是:jieba, wordcloud, matplotlib 安装方法基本一致,下面我以安装wordcloud的过程为例. 第一步,按下Win+ ...

  8. linux下各个目录里面都装了什么

    文章来源:http://blog.csdn.net/sunstars2009918/article/details/7038772 搞电脑的人总想知道自己的系统里到底有些什么东西,于是我就在Linux ...

  9. C#里面const和readonly

    一.const关键字限定一个变量不允许被改变. 使用const在一定程度上可以提高程序的安全性和可靠性. 1.用于修改字段或局部变量的声明,表示指定的字段或局部变量的值是常数,不能被修改. 2.常数声 ...

  10. mysql主从同步详细教程

    1.安装好主数据库和从数据库,这个大家肯定都会,如果不是很明白,可以参考我前面的安装教程. 例子: 假如我需要同步test1.test2数据库 系统:centos7 主库主机:192.168.1.25 ...