Python的condition和阻塞队列Queue
来源:互联网 发布:php通过ip获取地址 编辑:程序博客网 时间:2024/05/16 04:47
Python的condition和阻塞队列Queue
条件(Condition)
条件同步机制是指:一个线程等待特定条件,而另一个线程发出特定条件满足的信号。 解释条件同步机制的一个很好的例子就是生产者/消费者(producer/consumer)模型。生产者随机的往列表中“生产”一个随机整数,而消费者从列表中“消费”整数。
实例:
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2017/7/25 9:40# @File : produ.pyimport randomimport threadingimport time#生产者class Producer(threading.Thread): def __init__(self,integers,condition,name): """ 构造器 @:param integers 整数列表 @:param condition 条件同步对象 @:param name 线程名字 """ threading.Thread.__init__(self) self.integers = integers self.condition = condition self.name = name def run(self): """ 向队列中随机添加数字 :return: """ while True: integer = random.randint(0,300) self.condition.acquire() #获取锁 print '锁被',self.name,'拿了' self.integers.append(integer) #加入列表 print integer,'被',self.name,'加入列表' print '锁 被',self.name,'唤醒' self.condition.notify() print '锁被',self.name,'释放' self.condition.release() time.sleep(1)#消费者class Consumer(threading.Thread): def __init__(self,integers,condition,name): threading.Thread.__init__(self) self.integers = integers self.condition = condition self.name = name def run(self): while True: self.condition.acquire() print '锁被',self.name,'拿了' while True: if self.integers: integer = self.integers.pop() print integer,'从列表中被',self.name,'移除' break print self.name,'等待' self.condition.wait() print '锁被',self.name,'释放' self.condition.release()def main(): integers = [] condition = threading.Condition() consumer = Consumer(integers,condition,'消费者') producer = Producer(integers,condition,'生产者') producer.start() consumer.start() producer.join() consumer.join() if __name__ == '__main__': main()main()
队列(Queue)
队列是一个非常好的线程同步机制,使用队列我们不用关心锁,队列会为我们处理锁的问题。 队列(Queue)有以下4个用户感兴趣的方法:
put: 向队列中添加一个项;
get: 从队列中删除并返回一个项;
task_done: 当某一项任务完成时调用;
join: 阻塞知道所有的项目都被处理完。
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2017/7/25 10:10# @File : que.py#生产者消费者,阻塞队列实现import randomfrom Queue import Queueimport threadingimport timeclass Producer(threading.Thread): """ @:param queue 阻塞队列 @:param name 线程名字 """ def __init__(self,queue,name): threading.Thread.__init__(self) self.queue = queue self.name = name def run(self): while True: integer = random.randint(0,350) self.queue.put(integer) print self.name,'将',integer,'加入队列' time.sleep(6)class Consumer(threading.Thread): def __init__(self,queue,name): threading.Thread.__init__(self) self.queue = queue self.name = name def run(self): while True: integer = self.queue.get() print self.name,'将',integer,'从队列中移除' self.queue.task_done()def main(): queue = Queue(32) consumer = Consumer(queue,'消费者') producer = Producer(queue,'生产者') producer.start() consumer.start() if __name__ == '__main__': main()main()
阅读全文
0 0
- Python的condition和阻塞队列Queue
- 阻塞队列Queue/Deque/condition
- Condition的阻塞队列
- 通过Condition实现自己的阻塞队列
- Lock和Condition实现线程通信(附阻塞队列的实例)
- Lock和Condition实现阻塞队列与生产者消费者模式的实现
- python线程间通信方法小结:event、condition 和 queue
- (10)java5条件阻塞Condition的应用<包含阻塞队列知识>
- (10)java5条件阻塞Condition的应用<包含阻塞队列知识>
- Lock+Condition 模仿 阻塞队列(BlockQueue)
- Linux C++ 使用condition实现阻塞队列
- java 使用ReentrantLock Condition实现阻塞队列
- Linux C++ 使用condition实现阻塞队列
- java多线程:10、阻塞队列【Condition】
- python实现stack(栈)和队列(queue)
- 线程中阻塞通信Condition的wait()和signal()
- 黑马程序员-java-condition-实现线程通信和可阻塞队列
- 条件阻塞Condition的应用
- 最新pods安装,移除及问题
- (转)Windows Server下查看当前登录用户命令
- JS——Linq to js使用小结
- 高并发下的线程安全实现——互斥同步
- 微信小程序 总结
- Python的condition和阻塞队列Queue
- 一个window机器上配置多个tomcat
- HTML5终极备忘大全(图片版+文字版)
- 【备忘】传智播客最新大数据第3期实战培训完整版视频教程
- 微信小程序placeholder设置自定义颜色
- Android
- 图像几何变换:旋转,缩放,斜切
- 浅谈做的第一个项目学生后台管理系统
- 刷题报告001 洛谷1002&&1010