-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy path52.multiprocess_with_queue.py
More file actions
55 lines (47 loc) · 1.12 KB
/
52.multiprocess_with_queue.py
File metadata and controls
55 lines (47 loc) · 1.12 KB
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
from multiprocessing import Process, Queue
def producer(q1):
for x in [1, 2, 3]:
print("Producer:", x)
q1.put(x)
q1.put(None)
def add_one(q1, q2):
while True:
x = q1.get()
if x is None:
q2.put(None)
break
y = x + 1
print("Add one:", y)
q2.put(y)
def multiply(q2):
while True:
x = q2.get()
if x is None:
break
print("Multiply:", x * 5)
if __name__ == "__main__":
q1 = Queue()
q2 = Queue()
p1 = Process(target=producer, args=(q1,))
p2 = Process(target=add_one, args=(q1, q2))
p3 = Process(target=multiply, args=(q2,))
# What will be printed (order may vary because processes run in parallel):
# Producer: 1
# Add one: 2
# Multiply: 10
# Producer: 2
# Add one: 3
# Multiply: 15
# Producer: 3
# Add one: 4
# Multiply: 20
#
# Notes:
# - Exact interleaving is NOT guaranteed.
# - These 9 lines are the expected set of outputs.
p1.start()
p2.start()
p3.start()
p1.join()
p2.join()
p3.join()