2012-02-07 04:16:37 +00:00
|
|
|
import multiprocessing
|
2012-02-07 18:16:44 +00:00
|
|
|
from multiprocessing import Pipe
|
2012-02-07 04:16:37 +00:00
|
|
|
|
|
|
|
class DoubleTask(object):
|
2012-02-07 18:16:44 +00:00
|
|
|
def __init__(self, n, conn):
|
2012-02-07 04:16:37 +00:00
|
|
|
self.n = n
|
2012-02-07 18:16:44 +00:00
|
|
|
self.conn = conn # pipe to send results back
|
2012-02-07 04:16:37 +00:00
|
|
|
def __call__(self):
|
|
|
|
print "DoubleTask %s" % (self.n)
|
2012-02-07 18:16:44 +00:00
|
|
|
self.conn.send((self.n, 2*self.n))
|
|
|
|
self.conn.close()
|
2012-02-07 04:16:37 +00:00
|
|
|
return (self.n, 2*self.n)
|
|
|
|
def __str__(self):
|
|
|
|
return 'DoubleTask (%d)' % (self.n)
|
2012-02-07 15:50:07 +00:00
|
|
|
|
|
|
|
class TripleTask(object):
|
|
|
|
def __init__(self, n):
|
|
|
|
self.n = n
|
|
|
|
def __call__(self):
|
|
|
|
print "TripleTask %s" % (self.n)
|
|
|
|
return (self.n, 3*self.n)
|
|
|
|
def __str__(self):
|
|
|
|
return 'TripleTask (%d)' % (self.n)
|
|
|
|
|
|
|
|
class BigTask(object):
|
2012-02-07 18:16:44 +00:00
|
|
|
def __init__(self, n, doubler_queue):
|
2012-02-07 15:50:07 +00:00
|
|
|
self.n = n
|
2012-02-07 18:16:44 +00:00
|
|
|
self.doubler_queue = doubler_queue
|
2012-02-07 15:50:07 +00:00
|
|
|
def __call__(self):
|
|
|
|
print "BigTask %s" % (self.n)
|
2012-02-07 18:16:44 +00:00
|
|
|
# put the DoubleTask into the doubler_queue and wait for a result
|
|
|
|
parent_conn, child_conn = Pipe()
|
|
|
|
|
|
|
|
double_task = self.doubler_queue.put(DoubleTask(self.n, child_conn))
|
|
|
|
double_task_answer = parent_conn.recv()
|
|
|
|
|
|
|
|
return (self.n, double_task_answer + TripleTask(self.n)())
|
2012-02-07 15:50:07 +00:00
|
|
|
def __str__(self):
|
|
|
|
return 'BigTask (%d)' % (self.n)
|
2012-02-07 04:16:37 +00:00
|
|
|
|
2012-02-07 18:16:44 +00:00
|
|
|
class SimpleConsumer(multiprocessing.Process):
|
2012-02-07 04:16:37 +00:00
|
|
|
|
2012-02-07 18:16:44 +00:00
|
|
|
def __init__(self, task_queue):
|
2012-02-07 04:16:37 +00:00
|
|
|
multiprocessing.Process.__init__(self)
|
|
|
|
self.task_queue = task_queue
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
proc_name = self.name
|
|
|
|
while True:
|
|
|
|
next_task = self.task_queue.get()
|
|
|
|
if next_task is None:
|
|
|
|
# Poison pill means shutdown
|
|
|
|
print '%s: Exiting' % proc_name
|
|
|
|
self.task_queue.task_done()
|
|
|
|
break
|
|
|
|
print '%s: %s' % (proc_name, next_task)
|
|
|
|
answer = next_task()
|
|
|
|
self.task_queue.task_done()
|
2012-02-07 18:16:44 +00:00
|
|
|
return answer
|
2012-02-07 04:16:37 +00:00
|
|
|
|
2012-02-07 18:16:44 +00:00
|
|
|
class Consumer(SimpleConsumer):
|
2012-02-07 04:16:37 +00:00
|
|
|
|
|
|
|
def __init__(self, task_queue, result_queue):
|
2012-02-07 18:16:44 +00:00
|
|
|
pass
|
2012-02-07 04:16:37 +00:00
|
|
|
|
|
|
|
def run(self):
|
2012-02-07 18:16:44 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-07 04:16:37 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
manager = multiprocessing.Manager()
|
|
|
|
|
2012-02-07 15:50:07 +00:00
|
|
|
big_queue = multiprocessing.JoinableQueue()
|
2012-02-07 04:16:37 +00:00
|
|
|
doubler_queue = multiprocessing.JoinableQueue()
|
2012-02-07 15:50:07 +00:00
|
|
|
triple_queue = multiprocessing.JoinableQueue()
|
|
|
|
|
|
|
|
double_results = manager.dict()
|
|
|
|
triple_results = manager.dict()
|
|
|
|
big_results = multiprocessing.Queue()
|
|
|
|
|
|
|
|
doubler_processor = ConsumerWithResultDict(doubler_queue, double_results)
|
|
|
|
triple_processor = ConsumerWithResultDict(triple_queue, triple_results)
|
|
|
|
big_processor = Consumer(big_queue, big_results)
|
2012-02-07 04:16:37 +00:00
|
|
|
|
|
|
|
doubler_processor.start()
|
2012-02-07 15:50:07 +00:00
|
|
|
triple_processor.start()
|
|
|
|
big_processor.start()
|
2012-02-07 04:16:37 +00:00
|
|
|
|
|
|
|
n_tasks = 10
|
|
|
|
for k in xrange(n_tasks):
|
2012-02-07 18:16:44 +00:00
|
|
|
big_queue.put(BigTask(k, doubler_queue))
|
2012-02-07 15:50:07 +00:00
|
|
|
|
2012-02-07 04:16:37 +00:00
|
|
|
doubler_queue.put(None) # mark the end
|
2012-02-07 15:50:07 +00:00
|
|
|
triple_queue.put(None)
|
|
|
|
big_queue.put(None)
|
|
|
|
|
|
|
|
# while there is an expectation of more results, read off results in the results queue
|
|
|
|
results_so_far = 0
|
|
|
|
net_results = {}
|
2012-02-07 04:16:37 +00:00
|
|
|
|
2012-02-07 15:50:07 +00:00
|
|
|
while results_so_far < n_tasks:
|
|
|
|
result = big_results.get()
|
|
|
|
net_results[result[0]] = result[1]
|
|
|
|
print result
|
|
|
|
results_so_far += 1
|
2012-02-07 04:16:37 +00:00
|
|
|
|
2012-02-07 15:50:07 +00:00
|
|
|
print "net results", net_results
|
2012-02-07 04:16:37 +00:00
|
|
|
|
|
|
|
|