[PY3]——threading.Event
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过去了
'''
[PY3]——threading.Event的更多相关文章
- threading event
#!usr/bin/env python 2 #coding: utf-8 3 #Author: Andy 4 5 import threading 6 import time 7 8 def pro ...
- Python多线程的threading Event
Python threading模块提供Event对象用于线程间通信.它提供了一组.拆除.等待用于线程间通信的其他方法. event它是沟通中最简单的一个过程之中,一个线程产生一个信号,号.Pytho ...
- {Python之线程} 一 背景知识 二 线程与进程的关系 三 线程的特点 四 线程的实际应用场景 五 内存中的线程 六 用户级线程和内核级线程(了解) 七 python与线程 八 Threading模块 九 锁 十 信号量 十一 事件Event 十二 条件Condition(了解) 十三 定时器
Python之线程 线程 本节目录 一 背景知识 二 线程与进程的关系 三 线程的特点 四 线程的实际应用场景 五 内存中的线程 六 用户级线程和内核级线程(了解) 七 python与线程 八 Thr ...
- pythonl练习笔记——threading线程中的事件Event
1 事件Event 使用方法:e = threading.Event() Event对象主要用于线程间通信,确切地说是用于主线程控制其他线程的执行. Event事件提供了三个方法:wait等待.cle ...
- 人生苦短之我用Python篇(线程/进程、threading模块:全局解释器锁gil/信号量/Event、)
线程: 有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元.是一串指令的集合.线程是程序中一个单一的顺序控制流程.进程内一个相对独立的.可调度的执行单元,是 ...
- python第五十一天----线程,Event,队列
进程与线程的区别: 线程==指令集,进程==资源集 (线程集) 1.同一个进程中的线程共享内存空间,进程与进程之间是独立的 2.同一个进程中的线程是可以直接通讯交流的,进程与间通讯必需通过一个中间的 ...
- Python标准模块--threading
1 模块简介 threading模块在Python1.5.2中首次引入,是低级thread模块的一个增强版.threading模块让线程使用起来更加容易,允许程序同一时间运行多个操作. 不过请注意,P ...
- python 线程之 threading(三)
python 线程之 threading(一)http://www.cnblogs.com/someoneHan/p/6204640.html python 线程之 threading(二)http: ...
- python成长之路【第十一篇】:网络编程之线程threading模块
一.threading模块介绍 threading 模块建立在 _thread 模块之上.thread 模块以低级.原始的方式来处理和控制线程,而 threading 模块通过对 thread 进行二 ...
随机推荐
- 3D空间中射线与三角形的交叉检测算法【转】
引言 射线Ray,在3D图形学中有很多重要的应用.比如,pick操作就是使用射线Ray来实现的,还有诸如子弹射线的碰撞检测等等都可以使用射线Ray来完成.所以,在本次博客中,将会简单的像大家介绍下,如 ...
- C# 获取每一个像素点的RGB
int x, y; x = e.X; y = e.Y; Color pixel = MyImage.GetPixel(x, y); byte R = pixel.R; byte G = pixel.G ...
- 在Asp.Net MVC中使用NPOI插件实现对Excel的操作(导入,导出,合并单元格,设置样式,输入公式)
前言 NPOI 是 POI 项目的.NET版本,它不使用 Office COM 组件,不需要安装 Microsoft Office,目前支持 Office 2003 和 2007 版本. 1.整个Ex ...
- The Beam Model:Stream & Tables翻译(上)
作者:周思华 欢迎访问网易云社区,了解更多网易技术产品运营经验. 本文尝试描述Beam模型和Stream & Table理论间的关系(前者描述于数据流模型论文.the-world-beyond ...
- js中的基本类型与引用类型学习
一.基本数据类型 ECMAScript 有 5 种原始类型(primitive type),即 Undefined.Null.Boolean.Number 和 String,也称为基本数据类型,ES6 ...
- C语言作业03-函数
1.本章学习总结 1.1 思维导图 1.2本章学习体会,代码量学习体会 1.2.1学习体会 通过这几周的函数学习,让我明白了函数的重要性,在很多时候运用函数,会使得代码分工明确,逻辑严密,不繁琐.函数 ...
- Android与js交互
本文转载自:http://blog.csdn.net/it1039871366/article/details/46372207 一文. Android 中可以通过webview来实现和js的交互,在 ...
- Exp3 免杀原理与实践 20164321 王君陶
Exp3 免杀原理与实践 20164321 王君陶 1实验要求 1.1 正确使用msf编码器(0.5分),msfvenom生成如jar之类的其他文件(0.5分),veil-evasion(0.5分), ...
- if判断和switch case 和三元运算符整理
if判断和switch case 和三元运算符整理 例子1:if判断写法: <script type="text/javascript"> var num = 12; ...
- python 设置默认的导包路径
在python中 可以通过 sys 模块添加导包时的搜寻路径, sys.path 返回的是所有默认导包路径的列表(搜索次序从下标为零开始,直到寻找到需要导入的包结束) sys.path.insert( ...