FAILED attempt to use Pool to process tripler, negator, etc. Now changing gears

pull/1/head
Raymond Yee 2012-02-06 17:10:56 -08:00
parent 818174fee9
commit 278701c56b
1 changed files with 23 additions and 19 deletions

View File

@ -33,9 +33,6 @@ def tripler(n):
return 3*n
doubler_pool = multiprocessing.Pool(1) #use one core for now
negator_pool = multiprocessing.Pool(1)
tripler_pool = multiprocessing.Pool(1)
class Consumer(multiprocessing.Process):
@ -66,12 +63,12 @@ class NetTask(object):
self.n = n
def __call__(self):
print "NetTask %s" % (self.n)
global doubler_pool, negator_pool, tripler_pool
print doubler_pool, negator_pool, tripler_pool
async_results = [doubler_pool.apply_async(doubler, (self.n,)),
negator_pool.apply_async(negator, (self.n,)),
tripler_pool.apply_async(tripler, (self.n,))]
print async_results
print "NetTask about to return async_results for %s" % (self.n)
return (self.n, async_results)
@ -79,21 +76,9 @@ class NetTask(object):
def __str__(self):
return 'Totaler (%d)' % (self.n)
def main():
def pooler():
# generate a queue to hold the results
tasks = multiprocessing.JoinableQueue()
results_queue = multiprocessing.Queue()
random.seed()
# Start consumers
num_consumers = multiprocessing.cpu_count()
print 'Creating %d consumers' % num_consumers
consumers = [ Consumer(tasks, results_queue)
for i in xrange(num_consumers) ]
for w in consumers:
w.start()
global doubler_pool, negator_pool, tripler_pool
TO_CALC = 10
results = []
@ -105,11 +90,30 @@ def main():
for result in results:
print(sum(r.get() for r in result))
## -------
#
#doubler_pool = None
#negator_pool = None
#tripler_pool = None
def main():
# generate a queue to hold the results
tasks = multiprocessing.JoinableQueue()
results_queue = multiprocessing.Queue()
global doubler_pool, negator_pool, tripler_pool
doubler_pool = multiprocessing.Pool(1) #use one core for now
negator_pool = multiprocessing.Pool(1)
tripler_pool = multiprocessing.Pool(1)
random.seed()
# Start consumers
num_consumers = multiprocessing.cpu_count()
print 'Creating %d consumers' % num_consumers
consumers = [ Consumer(tasks, results_queue)
for i in xrange(num_consumers) ]
for w in consumers:
w.start()
# demonstrate that we can do stuff with the pools
pooler()
n_tasks = 2