Skip to content

Commit c54bbe2

Browse files
committed
make output a bit more informative
1 parent d68aa8c commit c54bbe2

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

demos/task-queue.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
from multiprocessing import Process, Queue
22

3-
def f(q):
3+
def f(q, i):
44
while True:
55
x = q.get()
66
if x is None:
77
break
8-
print(x**2)
8+
print('[{0}] {1}'.format(i, x**2))
99

1010
q = Queue()
11-
for i in range(10):
11+
for i in range(100):
1212
q.put(i)
1313

14-
# task queue: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
14+
# task queue: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ..., 99]
1515

16-
for i in range(3):
17-
q.put(None)
18-
p = Process(target=f, args=(q, ))
19-
p.start()
16+
n = 3 # no. of processes
17+
p = [] # list of processes
18+
for i in range(n):
19+
p.append(Process(target=f, args=(q, i, )))
20+
q.put(None) # add sentinels to signal STOP
21+
22+
# start work on all processes
23+
for i in range(n):
24+
p[i].start()
2025

0 commit comments

Comments
 (0)