[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 进行二 ...
随机推荐
- Thread in depth 4:Synchronous primitives
There are some synchronization primitives in .NET used to achieve thread synchronization Monitor c# ...
- Django:如何给文章列表添加图片
思路: 使用ajax方式将图片和文本一起通过formData提交到后台,Django后台通过request.POST和request.FILES方式接收数据 1.前端代码 {% extends 'ba ...
- C#使用oledb操作excel文件的方法
本文实例讲述了C#使用oledb操作excel文件的方法.分享给大家供大家参考.具体分析如下: 不管什么编程语言都会提供操作Excel文件的方式,C#操作Excel主要有以下几种方式: 1.Excel ...
- 用MVC5+EF6+WebApi 做一个考试功能(六) 仓储模式 打造EF通用仓储类
前言 年底工作比较忙,年度总结还没写,项目要上线,回老家过年各种准备.尤其是给长辈给侄子侄女准备礼物头都大了. 原来想年前先出一版能用的,我看有点悬了,尽量先把大体功能弄出来,扔掉一些,保证能考试,然 ...
- pandas 对dataframe一列中某些值进行处理
https://github.com/Bifzivkar/Boutique-Travel-Services-Predict/blob/master/feature/5_extract_feature. ...
- PHP 单点登录实现方案
单点登录SSO(Single Sign On)说得简单点就是在一个多系统共存的环境下,用户在一处登录后,就不用在其他系统中登录,也就是用户的一次登录能得到其他所有系统的信任.单点登录在大型网站里使用得 ...
- Heap-451. Sort Characters By Frequency
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- git 常用命令(不定期更新)
过程写写吧,总是忘记.1,在一个文件夹下 键入 git init ,使之成为Git可以管理的仓库.2,编写一个文件readme.txt.3,把文件添加到仓库 git add readme.txt4,把 ...
- 【JS深入学习】——函数创建和重载
今天做一个关注/取消的功能,由于需要向后台发送请求,想通过控制用户点击发送的频次减少不必要的请求,即在一定时间内,用户点击多次但只发送一次数据,自然而然想到了使用[函数节流]. function th ...
- D09——C语言基础学PYTHON
C语言基础学习PYTHON——基础学习D09 20180903内容纲要: 线程.进程 1.paramiko 2.线程.进程初识 3.多线程 (1)线程的调用方式 (2)join (3)线程锁.递归锁. ...