-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
42 lines (34 loc) · 971 Bytes
/
example.py
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
# Example for usage of the PoolWorker class.
from parallel_runner import PoolWorker, CLIController
import time
class Exhibitionist(PoolWorker):
def job(self, prm):
print self.name + "in da house! got " + str(prm)
print "Thinking hard..."
time.sleep(5) # Just to show what happens on long runs
return (prm, prm**2)
if __name__ == "__main__":
from multiprocessing import Pipe, Queue
import sys
tasks = Queue()
out = Queue()
for num in xrange(20):
tasks.put(num)
time.sleep(1)
# Run workers:
w = []
for p in xrange(4):
pside, cside = Pipe()
t = Exhibitionist(tasks, cside, out)
w.append((t, pside))
t.start()
time.sleep(0.1)
# Command line that quits when the last process quits.
ctrl = CLIController(w)
ctrl.prompt = "sim> "
ctrl.listen_loop(out,
lambda r: sys.stdout.write("Slave says %d**2 = %d\n" % (r[0], r[1])))
# Wait for everyone to finish:
print "\nWaiting for jobs to finish..."
for proc in w:
proc[0].join()