1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
import time import random
def producer(queue, food,name): for i in range(10): data = '%s:这个厨师,做了第%s个%s' % (name, i, food) print(data) time.sleep(random.randint(1, 3)) queue.put('第%s个%s' % (i, food))
def consumer(queue,name): while True: try: res = queue.get(timeout=20) time.sleep(random.randint(1, 3)) print('%s这个消费者,吃了%s' % (name, res)) except Exception as e: print(e) break
if __name__ == '__main__': queue = Queue(3) p1 = Process(target=producer, args=[queue, '包子','egon']) p2 = Process(target=producer, args=[queue, '骨头','lqz']) p3 = Process(target=producer, args=[queue, '泔水','jsason']) p1.start() p2.start() p3.start()
c1 = Process(target=consumer, args=[queue, '孟良']) c2 = Process(target=consumer, args=[queue,'池劲涛' ]) c3 = Process(target=consumer, args=[queue,'池劲涛' ]) c4 = Process(target=consumer, args=[queue,'池劲涛' ]) c1.start() c2.start() c3.start() c4.start()
|