from multiprocessing import Process,JoinableQueue import time import random def producer(name,food,q): for i in range(3): data = '%s生产了%s%s' % (name, food, i) time.sleep(random.randint(1, 3)) print(data) q.put(data) def consumer(name,q): while True: food = q.get() time.sleep(random.randint(1,3)) print('%s吃了%s' % (name, food)) q.task_done() if __name__ == '__main__': q = JoinableQueue() p1 = Process(target=producer, args=('xxx厨师', '包子', q)) p2 = Process(target=producer, args=('yyy厨师', '泔水', q)) c1 = Process(target=consumer, args=('111顾客', q)) c2 = Process(target=consumer, args=('222顾客', q)) p1.start() p2.start() c1.daemon = True c2.daemon = True c1.start() c2.start() p1.join() p2.join() q.join()