import threading,time,queue,random
def scz():
while True:
num=random.randint(1,100000)
q.put(num)#放进队列
print("生产者产生了%d数据" %(num))
time.sleep(5)
q.task_done()#完成任务
def xfz():
while True:
item=q.get()#从队列取出
if item is None:
break
print("消费者消费了%d数据"%item)
time.sleep(3)
q.task_done()
if __name__ == '__main__':
q=queue.Queue()
for i in range(4):
threading.Thread(target=scz).start()
for i in range(3):
threading.Thread(target=xfz).start()
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240813180353098-2140851696.jpg)