#JoinableQueue 队列
#put 存放
#get 获取
#task_done 计数器
#join 配合task_done 来使用 阻塞
#put 一次数据属性加一
#get 一次数据,task_done 属性-1
#join 根据队列task_done 计数器计算队列属性是否等于0,等于0,代码放行,不等于0,代码阻塞
from multiprocessing import Process, Queue,JoinableQueue
import time, random
# 消费者消费西瓜
def consumer(q, name):
while True:
food = q.get()
print("{}吃掉了{}".format(name,food))
q.task_done()
# 生产者生产西瓜
def producer(q, person, food):
for i in range(1, 1000):
q.put(food + str(i))
print("{}生产了{}个".format(person,food + str(i)))
if __name__ == "__main__":
q = JoinableQueue()
p = Process(target=producer, args=(q, "张三", "西瓜"))
p.start()
p1 = Process(target=consumer, args=(q, "李四"))
p1.start()
p1.join()
q.join()