关于Python多线程condition变量的应用
'''
所谓条件变量,即这种机制是在满足了特定的条件后,线程才可以访问相关的数据。
它使用Condition类来完成,由于它也可以像锁机制那样用,所以它也有acquire方法和release方法,而且它还有wait,notify,notifyAll方法。 一个简单的生产消费者模型,通过条件变量的控制产品数量的增减,调用一次生产者产品就是+1,调用一次消费者产品就会-1.
使用 Condition 类来完成,由于它也可以像锁机制那样用,所以它也有 acquire 方法和 release 方法,而且它还有
wait, notify, notifyAll 方法。
''' import threading
import queue, time, random # 产品类
class Goods:
def __init__(self):
self.count = 0 def add(self, num=1):
self.count += num def sub(self):
if self.count >= 0:
self.count -= 1 def empty(self):
return self.count <= 0 # 生产者
class Producer(threading.Thread): def __init__(self, condition, goods, sleeptime=1):
threading.Thread.__init__(self)
self.cond = condition
self.goods = goods
self.sleeptime = sleeptime def run(self):
cond = self.cond
goods = self.goods
while True:
# 锁住资源
cond.acquire()
goods.add()
print("产品数量:", goods.count, "生产者线程")
# 唤醒所有等待的线程 -> 其实就是唤醒消费者进程
cond.notifyAll()
# 解锁资源
cond.release()
time.sleep(self.sleeptime) # 消费者
class Consumer(threading.Thread):
def __init__(self, condition, goods, sleeptime=2):
threading.Thread.__init__(self)
self.cond = condition
self.goods = goods
self.sleeptime = sleeptime def run(self):
cond = self.cond
goods = self.goods
while True:
time.sleep(self.sleeptime)
# 锁住资源
cond.acquire()
# 如无产品则让线程等待
while goods.empty():
cond.wait()
goods.sub()
print("产品数量:", goods.count, "消费者线程")
# 解锁资源
cond.release()
g = Goods() c = threading.Condition() pro = Producer(c, g) pro.start() con = Consumer(c, g) con.start()
''' event 对象最好单次使用,就是说,你创建一个 event 对象,让某个线程等待这个对象,
一旦这个对象被设置为真,你就应该丢弃它。尽管可以通过 clear() 方法来重置 event 对
象,但是很难确保安全地清理 event 对象并对它重新赋值。很可能会发生错过事件、死锁
或者其他问题(特别是,你无法保证重置 event 对象的代码会在线程再次等待这个 event对象之前执行)。如果一个线程需要不停地重复使用 event 对象,你最好使用 Condition
对象来代替。下面的代码使用 Condition 对象实现了一个周期定时器,每当定时器超时的
时候,其他线程都可以监测到:
''' import threading
import time class PeriodicTimer:
def __init__(self, interval):
self._interval = interval
self._flag = 0
self._cv = threading.Condition() def start(self):
t = threading.Thread(target=self.run)
t.daemon = False
t.start() def run(self):
# Run the timer and notify waiting threads after each interval
while True:
time.sleep(self._interval)
with self._cv:
self._flag ^= 1
self._cv.notify_all() def wait_for_tick(self):
# wait for the next tick of the timer with self._cv:
last_flag = self._flag
while last_flag == self._flag:
self._cv.wait() ptimer = PeriodicTimer(2)
ptimer.start() def countdown(nticks):
while nticks > 0:
ptimer.wait_for_tick()
print('T-minus', 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=(10,)).start()
threading.Thread(target=countup, args=(10,)).start()
关于Python多线程condition变量的应用的更多相关文章
- Python 多线程 Condition 的使用
Condition Condition(条件变量)通常与一个锁关联.需要在多个Contidion中共享一个锁时,可以传递一个Lock/RLock实例给构造方法,否则它将自己生成一个RLock实例. 可 ...
- python多线程--Condition(条件对象)
Condition class threading.Condition(lock=None 这个类实现条件变量对象.条件变量允许一个或多个线程等待,知道它们被另一个线程唤醒. 如果给出了lock参数而 ...
- [Python 多线程] Condition (十)
Condition常用于生产者.消费者模型,为了解决生产者消费者速度匹配问题. 构造方法Condition(lock=None),可以传入一个Lock或RLock对象,默认RLock. 方法: acq ...
- python多线程编程
Python多线程编程中常用方法: 1.join()方法:如果一个线程或者在函数执行的过程中调用另一个线程,并且希望待其完成操作后才能执行,那么在调用线程的时就可以使用被调线程的join方法join( ...
- Python多线程和Python的锁
Python多线程 Python中实现多线程有两种方式,一种基于_thread模块(在Python2.x版本中为thread模块,没有下划线)的start_new_thread()函数,另一种基于th ...
- Day9 - Python 多线程、进程
Python之路,Day9, 进程.线程.协程篇 本节内容 操作系统发展史介绍 进程.与线程区别 python GIL全局解释器锁 线程 语法 join 线程锁之Lock\Rlock\信号量 将线 ...
- 搞定python多线程和多进程
1 概念梳理: 1.1 线程 1.1.1 什么是线程 线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发 ...
- python多线程几种方法实现
python多线程编程 Python多线程编程中常用方法: 1.join()方法:如果一个线程或者在函数执行的过程中调用另一个线程,并且希望待其完成操作后才能执行,那么在调用线程的时就可以使用被调线程 ...
- 浅析Python多线程
学习Python多线程的资料很多,吐槽Python多线程的博客也不少.本文主要介绍Python多线程实际应用,且假设读者已经了解多线程的基本概念.如果读者对进程线程概念不甚了解,可参见知名博主 阮一峰 ...
随机推荐
- 我的arcgis培训照片5
来自:http://www.cioiot.com/successview-528-1.html
- UVA10056 - What is the Probability ?(概率)
UVA10056 - What is the Probability ? (概率) 题目链接 题目大意:有n个人玩游戏,一直到一个人胜出之后游戏就能够结束,要不然就一直从第1个到第n个循环进行,没人一 ...
- 大文本 通过 hadoop spark map reduce 获取 特征列 的 属性值 计算速度
大文本 通过 hadoop spark map reduce 获取 特征列 的 属性值 计算速度
- ios18---自定义控件3改进
控制器: // XMGViewController.h #import <UIKit/UIKit.h> @interface XMGViewController : UIViewContr ...
- Linux/Android——input子系统核心 (三)【转】
本文转载自:http://blog.csdn.net/jscese/article/details/42123673 之前的博客有涉及到linux的input子系统,这里学习记录一下input模块. ...
- python调用java程序--jpype
官方网站:http://jpype.sourceforge.net/ 官方使用文档:http://jpype.sourceforge.net/doc/user-guide/userguide.html ...
- libnids 显示UDP数据报,编译,运行,正确。
#include<stdio.h>#include<nids.h>#include<string.h>#include <sys/socket.h>#i ...
- Android网络相关代码
/** * 获取本机的Ip * * @return */ public String getLocalIpAddress() { try { for (Enumeration<NetworkIn ...
- 【转载】Java - Wait & Notify
[本文转自]http://www.cnblogs.com/dolphin0520/p/3920385.html 这三个方法的文字描述可以知道以下几点信息: 1)wait().notify()和noti ...
- Unity资源的查找
Object.Destroy static function Destroy(obj: Object, t: float = 0.0F): void; Description Removes a ...