Skip to content

Commit

Permalink
make output a bit more informative
Browse files Browse the repository at this point in the history
  • Loading branch information
mlouhivu committed Jan 19, 2018
1 parent d68aa8c commit c54bbe2
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions demos/task-queue.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
from multiprocessing import Process, Queue

def f(q):
def f(q, i):
while True:
x = q.get()
if x is None:
break
print(x**2)
print('[{0}] {1}'.format(i, x**2))

q = Queue()
for i in range(10):
for i in range(100):
q.put(i)

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

for i in range(3):
q.put(None)
p = Process(target=f, args=(q, ))
p.start()
n = 3 # no. of processes
p = [] # list of processes
for i in range(n):
p.append(Process(target=f, args=(q, i, )))
q.put(None) # add sentinels to signal STOP

# start work on all processes
for i in range(n):
p[i].start()

0 comments on commit c54bbe2

Please sign in to comment.