Event&Condition pyton
Event
一个线程需要根据另外一个线程的状态来确定自己的下一步操作,需要调用threading库中Event对象;Event包含一个可由线程设置的信号标志,在初始情况下,event对象的标志位为假(false);。如果一个线程等待一个event对象,而这个event对象标志为假,那么这个线程将会被一直阻塞到标志为真(true);。
同理:一个线程如果将一个event对象的信号标志设置为真,它将唤醒所有等待这个event对象的线程。
如果一个线程等待一个已经被设置为真的event对象,那么它将忽略这个事件,继续执行。
#!/usr/bin/env python
# encoding: utf-8
"""
@author: 侠之大者kamil
@file: Event_test.py
@time: 2016/3/23 9:42
"""
from threading import Thread ,Event
import time
def countdown(n,started_evt):
print('countdown starting',time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))
started_evt.set()
while n >0:
print("T-time_kamil:",n)
n -=1
time.sleep(2)
started_evt = Event()
print("Launching countdown",time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))
t = Thread(target=countdown,args=(5,started_evt))
t.start()
started_evt.wait()
print("countdown is running",time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))
结果:
C:\Python34\python.exe D:/kamil/Documents/py/process/Event_test.py
Launching countdown 2016-03-23 16:03:28
countdown starting 2016-03-23 16:03:28
T-time_kamil: 5
countdown is running 2016-03-23 16:03:28
T-time_kamil: 4
T-time_kamil: 3
T-time_kamil: 2
T-time_kamil: 1 Process finished with exit code 0
Condition
如果一个线程需要不停重复的使用event对象,最好使用condition对象实现一个周期定时器,每当定时器超时的时候,其他线程都可以检测到:
#!/usr/bin/env python
# encoding: utf-8
"""
@author: 侠之大者kamil
@file: event_test2.py
@time: 2016/3/23 14:45
"""
import time,threading
class PeriodicTimer:
def __init__(self,interval):
self._interval = interval
self._flag = 0
self._cv = threading.Condition()#使用Condition对象可以在某些事件触发或者达到特定的条件后才处理数据 def start(self):
t = threading.Thread(target=self.run)
t.daemon = True#Daemon线程表明整个Python主程序只有在Daemon子线程运行时可以退出.
t.start()
def run(self):
while True:
time.sleep(self._interval)
with self._cv:
self._flag ^=1
self._cv.notify_all()
def wait_for_tick(self):
with self._cv:
last_flag = self._flag
while last_flag == self._flag:
self._cv.wait()
ptimer = PeriodicTimer(5)
ptimer.start() def countdown(nticks):
while nticks > 0 :
ptimer.wait_for_tick()
print("t_time",nticks)
nticks -= 1
def countup(last):
n = 0
while n < last:
ptimer.wait_for_tick()
print("counting",n)
n +=1 threading.Thread(target=countdown,args=(6,)).start()
threading.Thread(target=countup,args=(3,)).start()
结果:
C:\Python34\python.exe D:/kamil/Documents/py/process/event_test2.py
t_time 6
counting 0
t_time 5
counting 1
t_time 4
counting 2
t_time 3
t_time 2
t_time 1 Process finished with exit code 0
如上文中event在为真后,所有等待的进程都要开始,如果只想唤起一个进程,那可以使用Condition 对象来替代
Event&Condition pyton的更多相关文章
- 扯扯python的多线程的同步锁 Lock RLock Semaphore Event Condition
我想大家都知道python的gil限制,记得刚玩python那会,知道了有pypy和Cpython这样的解释器,当时听说是很猛,也就意味肯定是突破了gil的限制,最后经过多方面测试才知道,还是那德行… ...
- python 多线程中的同步锁 Lock Rlock Semaphore Event Conditio
摘要:在使用多线程的应用下,如何保证线程安全,以及线程之间的同步,或者访问共享变量等问题是十分棘手的问题,也是使用多线程下面临的问题,如果处理不好,会带来较严重的后果,使用python多线程中提供Lo ...
- 1Z0-053 争议题目解析697
1Z0-053 争议题目解析697 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 697.Which statement is true about a Scheduler-gen ...
- Python-09-线程、进程、协程、异步IO
0. 什么是线程(thread)? 线程,有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元.一个标准的线程由线程ID,当前指令指针(PC),寄存器集合和堆 ...
- Python Day10
进程 在python中multiprocess模块提供了Process类,实现进程相关的功能.但是,由于它是基于fork机制的,因此不被windows平台支持.想要在windows中运行,必须使用if ...
- AngularJS 分页
前端源码: <div> <h1>列表页33</h1> <table> <thead> <tr><td>CandiID ...
- python学习笔记12 ----线程、进程
进程和线程的概念 进程和线程是操作系统中两个很重要的概念,对于一般的程序,可能有若干个进程,每一个进程有若干个同时执行的线程.进程是资源管理的最小单位,线程是程序执行的最小单位(线程可共享同一进程里的 ...
- Python之线程、进程和协程
python之线程.进程和协程 目录: 引言 一.线程 1.1 普通的多线程 1.2 自定义线程类 1.3 线程锁 1.3.1 未使用锁 1.3.2 普通锁Lock和RLock 1.3.3 信号量(S ...
- Python_进程、线程及协程
一.Python进程 IO密集型----多线程 计算密集型----多进程 1.单进程 from multiprocessing import Process def foo(i): print('你好 ...
随机推荐
- CentOS 6.6 安装redmine
Redmine是一个开源的.基于Web的项目管理和缺陷跟踪工具.它用日历和甘特图辅助项目及进度可视化显示.同时它又支持多项目管理.Redmine是一个自由开放源码软件解决方案,它提供集成的项目管理功能 ...
- Smoothing in fMRI analysis (FAQ)
Source: http://mindhive.mit.edu/node/112 1. What is smoothing? "Smoothing" is generally us ...
- Dell 服务器做Raid
Dell 服务器做Raid DELL R720 服务器 RAID阵列卡配置介绍 (H310) 关于 RAID 5 与热备份(Hot Spare) 在不同RAID组间使用热备盘——Global Hot ...
- noi题库(noi.openjudge.cn) 1.7编程基础之字符串T31——T35
T31 字符串P型编码 描述 给定一个完全由数字字符('0','1','2',-,'9')构成的字符串str,请写出str的p型编码串.例如:字符串122344111可被描述为"1个1.2个 ...
- C#一元运算重载的深入理解
using System; using System.Diagnostics; using System.Text; using System.Collections; using System.Co ...
- matlab jet color mapping C / C++ / VC 实现
在matlab中调用imagesc()将一幅灰阶图像以彩色显示时,默认使用的color mapping是Jet,其color bar 为: Jet的color mapping图为: Color map ...
- Java:注解(元数据)
初识Java注解 所谓的元数据是指用来描述数据的数据,可能刚听到元数据的时候你会有点陌生,其实任何一个使用过struts或者hibernate的开发人员都在不知不觉中使用元数据,更通俗一点来说元数据是 ...
- Bootstrap系列 -- 3. 段落
一. 段落基本用法 1. 段落使用<p>标签 2. 段落全局使用font-size=14px字体 ..... 更多请使用Firefox 查看 <p> 华盛顿大学和清华大学共同在 ...
- 使用Python 将shapefile导入mongodb
使用Python 将shapefile导入mongodb 随着big data时代的到来,各个行业都在考虑能不能把big data的思路.方法引入进来,GIS行业也不能免俗. 下面就介绍一下如何将sh ...
- [POJ3696]The Luckiest number(数论)
题目:http://poj.org/problem?id=3696 题意:给你一个数字L,你要求出一个数N,使得N是L的倍数,且N的每位数都必须是8,输出N的位数(如果不存在输出0) 分析: 首先我们 ...