生产者和消费者

import queue
from threading import Thread
import time

q = queue.Queue()


class Productor(Thread):
def run(self) -> None:
count = 0
while True:
if q.qsize() < 50:
for i in range(200):
print('----生产-----{}---'.format(count))
q.put(count)
count += 1
time.sleep(1)


class Customer(Thread):
def run(self) -> None:
while True:
if q.qsize() > 10:
for i in range(3):
print('----消费-----{}---'.format(q.get()))
else:
time.sleep(2)


p = Productor()
p.start()
for i in range(5):
i = Customer()
i.start()
posted @ 2022-04-24 13:03  狒狒桑  阅读(23)  评论(0编辑  收藏  举报