from multiprocessing import Process, JoinableQueue
import random
import time
import os
def producer(name, q):
time.sleep(random.randrange(1, 3))
print(f'{os.getpid()},技师[{name}]上线!')
q.put(name)
q.join()
def customer(name, q):
print('欢迎来到天上人间桑拿中心'.center(30, '='))
while True:
res = q.get()
time.sleep(random.randrange(1, 3))
print(f'{name}叫了[{res}]进行服务!')
q.task_done() #向q.join()发送一次信号,证明一个数据已经被取走了
if name == 'main':
q = JoinableQueue()
# 两个生产者
p1 = Process(target=producer, args=('egon_dsb', q))
p2 = Process(target=producer, args=('tank', q))
# 一个消费者
c1 = Process(target=customer, args=('jason', q))
p1.start()
p2.start()
c1.daemon = True
c1.start()
p1.join()
p2.join()
q.join()