[Python 多线程] Condition (十)
Condition常用于生产者、消费者模型,为了解决生产者消费者速度匹配问题。
构造方法Condition(lock=None),可以传入一个Lock或RLock对象,默认RLock。
方法:
acquire(*args) 获取锁
release() 释放锁
wait(timeout=None) 等待通知或直到发生超时
notify(n=1) 唤醒至多指定个数的等待的线程,没有等待的线程就没有任何操作
notify_all() 唤醒所有等待的线程。wake up
以下例子,不考虑线程安全问题:
例1:
#Condition
import threading,random,logging
logging.basicConfig(level=logging.INFO) class Dispatcher:
def __init__(self):
self.data = 0
self.event = threading.Event() def produce(self):
for i in range(100):
self.event.wait(1) #每1秒生成一条数据
data = random.randint(1,100)
self.data = data def custom(self):
while True:
logging.info(self.data)
self.event.wait(0.5) # 替换为1表示消费者每1秒取一次数据 d = Dispatcher()
p = threading.Thread(target=d.produce)
c = threading.Thread(target=d.custom) c.start()
p.start() 以下结果:
INFO:root:0
INFO:root:0
INFO:root:0
INFO:root:77
INFO:root:64
INFO:root:64
INFO:root:8
INFO:root:8
INFO:root:85
生产者每1秒钟生成一条数据,消费者每0.5秒就来取一次数据。
例2:
#Condition 通知机制,解决重复
import threading,random,logging
logging.basicConfig(level=logging.INFO) class Dispatcher:
def __init__(self):
self.data = 0
self.event = threading.Event()
self.cond = threading.Condition() def produce(self):
for i in range(100):
data = random.randint(1,100)
with self.cond:
self.data = data
self.cond.notify_all() #通知所有waiter
self.event.wait(1) #1秒生产一次数据 def custom(self):
while True:
with self.cond:
self.cond.wait() #无限等待
logging.info(self.data) #消费 self.event.wait(0.5) #0.5秒消费一次数据 d = Dispatcher()
p = threading.Thread(target=d.produce)
c = threading.Thread(target=d.custom) c.start()
p.start() 运行结果:
INFO:root:64
INFO:root:43
INFO:root:11
INFO:root:28
INFO:root:30
INFO:root:33
INFO:root:93
INFO:root:69
INFO:root:4
使用with来管理Condition的上下文(acquire/release),利用Condition通知机制解决消费者获取重复数据。
例3:
#Condition 先生成后消费,1对1
import threading,random,logging
logging.basicConfig(level=logging.INFO,format="%(thread)d %(threadName)s %(message)s") class Dispatcher:
def __init__(self):
self.data = 0
self.event = threading.Event()
self.cond = threading.Condition() def produce(self):
for i in range(100):
data = random.randint(1,100)
logging.info(self.data)
with self.cond:
self.data = data
self.cond.notify(1)
# self.cond.notify_all()
self.event.wait(1) def custom(self):
while True:
with self.cond:
self.cond.wait()
logging.info(self.data) self.event.wait(0.5) d = Dispatcher()
p = threading.Thread(target=d.produce,name='produce')
c = threading.Thread(target=d.custom,name='c')
c1 = threading.Thread(target=d.custom,name='c1')
p.start() e = threading.Event()
e.wait(3) c1.start()
c.start() 运行结果:
7520 produce 0
7520 produce 78
7520 produce 88
7520 produce 14
7520 produce 83
2508 c1 86
7520 produce 86
1136 c 79
7520 produce 79
2508 c1 77
7520 produce 77
1136 c 47
7520 produce 47
2508 c1 76
7520 produce 76
1136 c 69
生产者先生产数据,2个消费者一个一个来消费数据。
例4:
#Condition 1对多,2个2个通知
import threading,random,logging
logging.basicConfig(level=logging.INFO,format="%(thread)d %(threadName)s %(message)s") class Dispatcher:
def __init__(self):
self.data = 0
self.event = threading.Event()
self.cond = threading.Condition() def produce(self):
for i in range(100):
data = random.randint(1,100)
logging.info(self.data)
with self.cond:
self.data = data
self.cond.notify(2)
# self.cond.notify_all()
self.event.wait(1) def custom(self):
while True:
with self.cond:
self.cond.wait()
logging.info(self.data)
# self.event.wait(0.5) d = Dispatcher()
p = threading.Thread(target=d.produce,name='produce') for i in range(5):
threading.Thread(target=d.custom,name='c-{}'.format(i)).start() p.start() 以下结果:
8688 produce 0
10376 c-0 90
7928 c-1 90
8688 produce 90
7640 c-2 61
10748 c-3 61
8688 produce 61
10376 c-0 73
1344 c-4 73
8688 produce 73
7928 c-1 57
7640 c-2 57
8688 produce 57
10748 c-3 71
10376 c-0 71
1对多,2个2个通知来处理数据。
以上例子中,程序本身不是线程安全的,程序逻辑有很多瑕疵,但是可以很好的帮助理解Condition的使用,和生产者消费者模型。
Condition总结:
Condition采用通知机制,常用于生产者消费者模型中,解决生产者消费者速度匹配的问题。
使用方法:
使用Condition,必须先acquire,用完之后要release,因为内部使用了锁,默认使用RLock,最好的方法是使用with上下文管理。
生产者wait,会阻塞等待通知,被激活。
生产者生产好消息,对消费者发通知,可以使用notidy_all() 通知所有消费者或者notify()。
[Python 多线程] Condition (十)的更多相关文章
- [Python 多线程] asyncio (十六)
asyncio 该模块是3.4版本加入的新功能. 先来看一个例子: def a(): for x in range(3): print('a.x', x) def b(): for x in 'abc ...
- Python 多线程 Condition 的使用
Condition Condition(条件变量)通常与一个锁关联.需要在多个Contidion中共享一个锁时,可以传递一个Lock/RLock实例给构造方法,否则它将自己生成一个RLock实例. 可 ...
- python多线程--Condition(条件对象)
Condition class threading.Condition(lock=None 这个类实现条件变量对象.条件变量允许一个或多个线程等待,知道它们被另一个线程唤醒. 如果给出了lock参数而 ...
- [Python 多线程] Concurrent (十五)
concurrent包只有一个模块: concurrent.futures - 启动并行任务 异步并行任务编程模块,提供一个高级的异步可执行的便利接口. futures模块提供了2个池执行器 Thre ...
- 关于Python多线程condition变量的应用
''' 所谓条件变量,即这种机制是在满足了特定的条件后,线程才可以访问相关的数据. 它使用Condition类来完成,由于它也可以像锁机制那样用,所以它也有acquire方法和release方法,而且 ...
- 第十五章、Python多线程之信号量和GIL
目录 第十五章.Python多线程之信号量和GIL 1. 信号量(Semaphore) 2. GIL 说明: 第十五章.Python多线程之信号量和GIL 1. 信号量(Semaphore) 信号量用 ...
- 第十五章、Python多线程同步锁,死锁和递归锁
目录 第十五章.Python多线程同步锁,死锁和递归锁 1. 引子: 2.同步锁 3.死锁 引子: 4.递归锁RLock 原理: 不多说,放代码 总结: 5. 大总结 第十五章.Python多线程同步 ...
- Python 多线程、多进程 (二)之 多线程、同步、通信
Python 多线程.多进程 (一)之 源码执行流程.GIL Python 多线程.多进程 (二)之 多线程.同步.通信 Python 多线程.多进程 (三)之 线程进程对比.多线程 一.python ...
- Python多线程多进程那些事儿看这篇就够了~~
自己以前也写过多线程,发现都是零零碎碎,这篇写写详细点,填一下GIL和Python多线程多进程的坑~ 总结下GIL的坑和python多线程多进程分别应用场景(IO密集.计算密集)以及具体实现的代码模块 ...
随机推荐
- Java基础教程(4)--面向对象概念
如果你之前从来没有使用过面向对象编程语言,那么在学习Java之前需要先理解几个有关面向对象编程的基本概念.这篇教程将会向你介绍对象.类.集成.接口和包的概念,以及这些概念是如何与现实世界相关联,并 ...
- SQLite metadata
http://www.devart.com/dotconnect/sqlite/docs/MetaData.html https://github.com/sqlitebrowser/sqlitebr ...
- SPOJ:NSUBSTR - Substrings
题面 字符串$ S \(最多包含\) 25 \(万个小写拉丁字母.我们将\) F(x) \(定义为长度为\) x \(的某些字符串出现在\) s \(中的最大次数.例如,对于字符串\) "a ...
- Vue.js小案例(2)
即时搜索 这个例子主要应用了vue.js的自定义过滤器,可以通过Vue.filter()注册一个全局过滤器,具体用法可以参考这里,vue.js也提供了一些内置过滤器. CSS代码: [v-cloak] ...
- 使用Hugo搭建个人博客站点
Hugo是个什么东东这里直接忽略,想了解的请查阅其他资料,我们直接上手操作. 安装Hugo 到 Hugo Releases 下载对应的操作系统版本的Hugo二进制文件 解压后得到 hugo_0.17_ ...
- <Android 应用 之路> MPAndroidChart~ScatterChart
简介 MPAndroidChart是PhilJay大神给Android开发者带来的福利.MPAndroidChart是一个功能强大并且使用灵活的图表开源库,支持Android和IOS两种,这里我们暂时 ...
- CentOS6.5(3)----设置自己安装的程序开机自动启动
CentOS6.5系统下设置自己安装的程序开机自动启动 方法1. 把启动程序的命令添加到 /etc/rc.d/rc.local 文件中,比如设置开机启动 mysqld: #!/bin/sh # # T ...
- (WCF) 多线程 (Multi-threading) 和 并发性 (Concurency)
问题:WCF 有个Server端,还有个Client端,他们之间是如何进行并发,多线程通信的呢?多个Client端同时访问Server,如何保证Server端的操作线程安全呢? 在理解WCF Conc ...
- QT5.9 新特性与版本回顾
原文链接: http://blog.qt.io/blog/2017/05/31/qt-5-9-released 翻译内容如下,采用的是第三方某在线翻译软件,所以有些地方不是太精确,纵然大吉做了一定的调 ...
- MySQL order null 0 - 把null和0(零)排在最后
1.一般的order by 语句其返回的结果为 SELECT `vcenter_ip`, `status`, `sla_id` FROM vm_list ORDER BY sla_id ASC; 2. ...