from threading import Thread
from queue import Queue
import time
que = Queue(1000)
class Producter(Thread):
def run(self):
global que
count = 0
while True:
if que.qsize() < 10:
for i in range(10):
count += 1
msg = "{} 号产品生产成功".format(count)
que.put(count)
print(msg)
time.sleep(0.5)
class Constumer(Thread):
def run(self):
global que
while True:
if que.qsize() >= 10:
for i in range(10):
msg = "{} 消费 {}".format(self.name,que.get())
print(msg)
time.sleep(0.5)
if __name__=="__main__":
p = Producter()
c = Constumer()
p.start()
time.sleep(0.5)
c.start()
p.join()
c.join()
python实现生产者和消费者
最新推荐文章于 2023-11-01 21:42:08 发布