python使用队列实现生产者消费者
来源:互联网 发布:mac os 10.6 dmg下载 编辑:程序博客网 时间:2024/06/05 06:22
# -*- coding: utf-8 -*-"""Created on Thu May 8 11:09:11 2014@author: lifeix"""import timeimport threadingfrom Queue import Queueimport randomclass Producer(threading.Thread): def __init__(self, t_name, queue): threading.Thread.__init__(self, name=t_name) self.data = queue def run(self): for i in range(20): print "%s:%s 正在生产 %d to the queue!\n" %(time.ctime(), self.getName(),i) self.data.put(i) time.sleep(random.randrange(10)/5) print "%s: %s finished!" %(time.ctime(), self.getName()) class Consumer(threading.Thread): def __init__(self, t_name, queue): threading.Thread.__init__(self, name=t_name) self.data = queue def run(self): for i in range(20): val = self.data.get() print "%s: %s is 正在消费. %d in the queue is consumed!\n" %(time.ctime(),self.getName(), val) time.sleep(random.randrange(10)/5) print "%s: %s finished!" %(time.ctime(), self.getName()) if __name__== '__main__': #test() #test1() queue = Queue() producer = Producer('Pro.', queue) consumer = Consumer('Con.', queue) producer.start() consumer.start() producer.join() consumer.join() print 'All threads terminate!'
0 0
- python使用队列实现生产者消费者
- python使用queue队列实现生产者消费者
- Python 生产者消费者--队列实现
- 使用阻塞队列BlockingQueue实现生产者消费者
- 使用AutoResetEvent信号实现生产者消费者队列
- python使用threading.condition实现生产者消费者
- Python使用生成器实现生产者消费者模式
- python:多线程+队列Queue 实现:生产者和消费者demo
- 多进程 队列 实现生产者消费者模型 python 笔记
- 生产者消费者JAVA队列实现
- 实现一个生产者消费者队列
- 队列实现生产者消费者模型
- 阻塞队列实现生产者消费者
- java使用阻塞队列实现生产者消费者模式
- 生产者、消费者问题,python实现
- Python 多线程|Queue队列|生产者消费者模式|
- 使用Python多线程如何实现生产者消费者模式?
- Java 队列 实现生产者-消费者模式
- javadoc常用命令
- Linux中设置服务自启动的三种方式
- 数据结构(五)之二叉查找树
- ajax回调函数不执行
- c语言中的数组和指针
- python使用队列实现生产者消费者
- Java 多线程
- matlab reshape使用
- Spring中的ContextLoaderListener使用
- html5开发之viewport使用
- 如何用sys as sysdba权限连接数据库进行Exp/Imp
- 任意的进制转换
- centos集群5 常见出错的配置
- 向大家推荐博士数学论坛!