生产者-消费者
1 import time 2 from threading import Thread 3 from queue import Queue 4 class Producter(Thread): 5 def run(self): 6 global queue 7 count = 0 8 while True: 9 #判断队列的大小 10 if queue.qsize()<1000: 11 for i in range(100): 12 count += 1 13 msg = '生产第'+str(count)+'个产品' 14 queue.put(msg) 15 print(msg) 16 time.sleep(0.5) 17 class Consumer(Thread): 18 def run(self): 19 global queue 20 while True: 21 if queue.qsize()>100: 22 for i in range(10): 23 msg = self.name+'消费'+queue.get() 24 print(msg) 25 time.sleep(1) 26 if __name__ == '__main__': 27 queue = Queue() 28 p = Producter() 29 c = Consumer() 30 p.start() 31 time.sleep(1) 32 c.start()
正是江南好风景