实现一个线程不断生成一个随机数到一个队列中(考虑使用Queue这个模块) 实现一个线程从上面的队列里面不断的取出奇数 实现另外一个线程从上面的队列里面不断取出偶数 Python中使用线程有两种方式:函数或者用类来包装线程对象。 函数式:调用thread模块中的start_new_thread()函数来产生新线程。或者创建threading.Thread的子类来包装一个线程对象。 此处采用第二种方式,即创建自己的线程类,必要时重写threading.Thread类的方法,线程的控制可以由自己定制。 最简单的同步机制就是“锁”。锁对象由threading.RLock类创建。 锁只能提供最基本的同步。假如只在发生某些事件时才访问一个“临界区”,这时需要使用条件变量Condition。Python中Queue对象也提供对线程的同步支持。在此次实践中就是使用queue对象实现一个生产者和两个消费者形成的FIFO的队列。生产者将数据依次存入队列,消费者从队列取出数据。 #!/usr/bin/env python #coding:utf8 import random,threading,time from Queue import Queue #Producer thread class 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(10): #随机产生10个数字 ,可以修改为任意大小 randomnum=random.randint(1,99) print "%s: %s is producing %d to the queue!" % (time.ctime(), self.getName(), randomnum) self.data.put(randomnum) #将数据依次存入队列 time.sleep(1) print "%s: %s finished!" %(time.ctime(), self.getName()) #Consumer thread class Consumer_even(threading.Thread): def __init__(self,t_name,queue): threading.Thread.__init__(self,name=t_name) self.data=queue def run(self): while 1: try: val_even = self.data.get(1,5) #get(self, block=True, timeout=None) ,1就是阻塞等待,5是超时5秒 if val_even%2==0: print "%s: %s is consuming. %d in the queue is consumed!" % (time.ctime(),self.getName(),val_even) time.sleep(2) else: self.data.put(val_even) time.sleep(2) except: #等待输入,超过5秒 就报异常 print "%s: %s finished!" %(time.ctime(),self.getName()) break class Consumer_odd(threading.Thread): def __init__(self,t_name,queue): threading.Thread.__init__(self, name=t_name) self.data=queue def run(self): while 1: try: val_odd = self.data.get(1,5) if val_odd%2!=0: print "%s: %s is consuming. %d in the queue is consumed!" % (time.ctime(), self.getName(), val_odd) time.sleep(2) else: self.data.put(val_odd) time.sleep(2) except: print "%s: %s finished!" % (time.ctime(), self.getName()) break #Main thread def main(): queue = Queue() producer = Producer('Pro.', queue) consumer_even = Consumer_even('Con_even.', queue) consumer_odd = Consumer_odd('Con_odd.',queue) producer.start() consumer_even.start() consumer_odd.start() producer.join() consumer_even.join() consumer_odd.join() print 'All threads terminate!' if __name__ == '__main__': main() 输出结果 Sun Jun 16 21:03:52 2013: Pro. is producing 71 to the queue! Sun Jun 16 21:03:52 2013: Con_odd. is consuming. 71 in the queue is consumed! Sun Jun 16 21:03:53 2013: Pro. is producing 24 to the queue! Sun Jun 16 21:03:54 2013: Pro. is producing 80 to the queue! Sun Jun 16 21:03:54 2013: Con_even. is consuming. 24 in the queue is consumed! Sun Jun 16 21:03:55 2013: Pro. is producing 94 to the queue! Sun Jun 16 21:03:56 2013: Con_even. is consuming. 94 in the queue is consumed! Sun Jun 16 21:03:56 2013: Pro. is producing 19 to the queue! Sun Jun 16 21:03:57 2013: Pro. is producing 88 to the queue! Sun Jun 16 21:03:58 2013: Pro. is producing 16 to the queue! Sun Jun 16 21:03:59 2013: Pro. is producing 70 to the queue! Sun Jun 16 21:04:00 2013: Con_even. is consuming. 80 in the queue is consumed! Sun Jun 16 21:04:00 2013: Pro. is producing 58 to the queue! Sun Jun 16 21:04:01 2013: Pro. is producing 2 to the queue! Sun Jun 16 21:04:02 2013: Con_odd. is consuming. 19 in the queue is consumed! Sun Jun 16 21:04:02 2013: Con_even. is consuming. 16 in the queue is consumed! Sun Jun 16 21:04:02 2013: Pro. finished! Sun Jun 16 21:04:04 2013: Con_even. is consuming. 88 in the queue is consumed! Sun Jun 16 21:04:06 2013: Con_even. is consuming. 2 in the queue is consumed! Sun Jun 16 21:04:08 2013: Con_even. is consuming. 58 in the queue is consumed! Sun Jun 16 21:04:10 2013: Con_even. is consuming. 70 in the queue is consumed! Sun Jun 16 21:04:17 2013: Con_odd. finished! Sun Jun 16 21:04:17 2013: Con_even. finished! All threads terminate! 转载请保留固定链接: https://linuxeye.com/program/1766.html |