代码
from multiprocessing import Process, Queue
def consumer(q):
while True:
res = q.get()
if res is None: break
print('\033[45m 吃 %s\033[0m' % ( res))
def producer(name, q):
res = '%s' % (name)
q.put(res)
print('\033[44m 生产了 %s\033[0m' % ( res))
if __name__ == '__main__':
q = Queue()
p1 = Process(target=producer, args=('包子', q))
p2 = Process(target=producer, args=('馒头', q))
p3 = Process(target=producer, args=('花卷', q))
c1 = Process(target=consumer, args=(q,))
c2 = Process(target=consumer, args=(q,))
p1.start()
p2.start()
p3.start()
c1.start()
c2.start()
p1.join()
p2.join()
p3.join()
q.put(None)
q.put(None)
输出