4/23
生产者消费者模型
from multiprocessing import Process, Queue, JoinableQueue
import time
import random
def producter(food, q):
for i in range(3):
data = f'{food}店生产了第{i}个{food}'
time.sleep(random.randint(1, 2))
print(data)
q.put(data)
def customer(name, q):
while 1:
food = q.get()
time.sleep(random.randint(1, 2))
print(f'{name}吃了{food}')
q.task_done()
if __name__ == '__main__':
q = JoinableQueue()
p1 = Process(target=producter, args=('炸鸡', q,))
p2 = Process(target=producter, args=('汉堡', q,))
c1 = Process(target=customer, args=('egon', q,))
c2 = Process(target=customer, args=('tank', q,))
p1.start()
p2.start()
c1.daemon = True
c2.daemon = True
c1.start()
c2.start()
p1.join()
p2.join()
q.join()