码迷,mamicode.com
首页 > 其他好文 > 详细

生产者-消费者

时间:2020-04-13 19:30:05      阅读:57      评论:0      收藏:0      [点我收藏+]

标签:产品   cte   print   star   name   queue   glob   start   count   

 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()

 

生产者-消费者

标签:产品   cte   print   star   name   queue   glob   start   count   

原文地址:https://www.cnblogs.com/monsterhy123/p/12693307.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!