Python多线程的简单实现(生产者消费者模型)

时间:2023-03-09 21:40:23
Python多线程的简单实现(生产者消费者模型)
 __author__ = "JentZhang"

 import time, threading, queue

 q = queue.Queue(maxsize=)  # 声明队列

 def Producer(name):
'''生产者'''
count =
while True:
q.put(count) # 往队列中添加数据
print("[%s] 生产了第%s包子\n" % (name, count))
count +=
time.sleep() def Consumer(name):
'''消费者'''
while True:
i = q.get() # 从队列中取数据
print("====[%s] 吃了第%s个包子\n" % (name, i))
time.sleep() '''设置多线程'''
p = threading.Thread(target=Producer, args=("Jent",))
c1 = threading.Thread(target=Consumer, args=("张三",))
c2 = threading.Thread(target=Consumer, args=("李四",)) '''线程开启'''
p.start()
c1.start()
c2.start()