Python队列同步
来源:互联网 发布:淘宝怎么查看我的评价 编辑:程序博客网 时间:2024/05/16 17:53
Python的Queue模块中提供了同步的、线程安全的队列类,包括FIFO(先入先出)队列Queue,LIFO(后入先出)队列LifoQueue,和优先级队列PriorityQueue。这些队列都实现了锁原语,能够在多线程中直接使用。可以使用队列来实现线程间的同步。
import threadingimport timefrom Queue import Queueclass Producer(threading.Thread): def run(self): global queue count=0 while True: for i in range(100): if queue.qsize()>1000: pass else: count=count+1 msg='produce'+str(count) queue.put(msg) print msg time.sleep(1)class Consumer(threading.Thread): def run(self): global queue while True: for i in range(3): if queue.qsize()<100: pass else: msg=self.name+'consume'+queue.get() print msg time.sleep(1)queue=Queue()def test(): for i in range(500): queue.put('init produce'+str(i)) for i in range(2): p=Producer() p.start() for i in range(5): c=Consumer() c.start()if __name__=='__main__': test()
阅读全文
0 0
- Python 线程同步队列
- Python队列同步
- Python线程编程(三)同步队列
- python多线程编程(6): 队列同步
- python多线程编程(6): 队列同步
- python多线程编程(6): 队列同步
- 同步队列
- 同步队列
- Queue同步队列类在python中的应用
- SynchronousQueue(同步队列) 原理
- 多线程同步队列实现
- 同步队列协作线程
- 队列、同步与锁
- SynchronousQueue 同步队列
- 同步队列SynchronousQueue
- python 队列
- Python- 队列
- python 队列
- 对象存在哪里?
- Redis与mysql同步之路
- HTML5 跨平台app调试api接口方案
- Rxjava2(二)
- 实例:把一个/oldboy目录及其子目录所有的扩展名.sh结尾的文件中包含oldboy的字符全部替换为beautiful
- Python队列同步
- Maven项目中缺少jar包引起的报错 nested exception is java.lang.NoClassDefFoundError
- 【Emgucv】图像卷积(TEST)
- solr-4.10.3的IK Analyzer分词器配置
- Linux 在终端中打开新的终端:gnome-terminal
- android Studio运行时,出现java.lang.NoClassDefFoundError
- PAT 1016. 部分A+B (15)
- LinuxC语言概述及开发工具的使用
- 俯视膜拜初等数论