队列实现生产者与消费者
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()