2012-02-04 22:15:00 +00:00
|
|
|
import multiprocessing
|
|
|
|
import time
|
|
|
|
import sys
|
2012-02-06 17:21:23 +00:00
|
|
|
import random
|
2012-02-04 22:15:00 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
Task: for each of the 900 or so Gutenberg ids, calculate seed isbn(s)
|
|
|
|
|
|
|
|
a queue to hold the overall task of calculating the seed isbn
|
|
|
|
a results queue to hold the seed isbn (or error condition/timeout)
|
|
|
|
separate queues for GoogleBooks, FreebaseBooks, OpenLibrary, thingisbn queries:
|
|
|
|
ideally -- they have timeout, retry facility as well as the ability to sense general failure in API
|
|
|
|
ideally: caching of results
|
|
|
|
ability to persist jobs, suspend, restart
|
|
|
|
|
|
|
|
We will take this in steps by first writing toy models and filling them out
|
|
|
|
|
|
|
|
"""
|
2012-02-06 17:21:23 +00:00
|
|
|
|
2012-02-04 22:15:00 +00:00
|
|
|
def doubler(n):
|
2012-02-06 17:21:23 +00:00
|
|
|
#time.sleep(random.uniform(0,0.1))
|
|
|
|
print "in doubler %s " % (n)
|
2012-02-04 22:15:00 +00:00
|
|
|
return 2*n
|
|
|
|
|
|
|
|
def negator(n):
|
2012-02-06 17:21:23 +00:00
|
|
|
#time.sleep(random.uniform(0,0.1))
|
|
|
|
print "in negator %s " % (n)
|
2012-02-04 22:15:00 +00:00
|
|
|
return -n
|
|
|
|
|
|
|
|
def tripler(n):
|
2012-02-06 17:21:23 +00:00
|
|
|
#time.sleep(random.uniform(0,0.1))
|
|
|
|
print "in tripler %s " % (n)
|
2012-02-04 22:15:00 +00:00
|
|
|
return 3*n
|
|
|
|
|
|
|
|
|
2012-02-04 22:57:57 +00:00
|
|
|
class Consumer(multiprocessing.Process):
|
|
|
|
|
|
|
|
def __init__(self, task_queue, result_queue):
|
|
|
|
multiprocessing.Process.__init__(self)
|
|
|
|
self.task_queue = task_queue
|
|
|
|
self.result_queue = result_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()
|
|
|
|
self.result_queue.put(answer)
|
|
|
|
return
|
|
|
|
|
|
|
|
|
2012-02-05 17:42:07 +00:00
|
|
|
class NetTask(object):
|
2012-02-04 22:57:57 +00:00
|
|
|
def __init__(self, n):
|
|
|
|
self.n = n
|
|
|
|
def __call__(self):
|
2012-02-06 17:21:23 +00:00
|
|
|
print "NetTask %s" % (self.n)
|
|
|
|
|
2012-02-07 04:16:37 +00:00
|
|
|
results = [apply(doubler, (self.n,)),
|
|
|
|
apply(negator, (self.n,)),
|
|
|
|
apply(tripler, (self.n,))]
|
|
|
|
print results
|
|
|
|
print "NetTask about to return results for %s" % (self.n)
|
|
|
|
return (self.n, results)
|
2012-02-06 17:21:23 +00:00
|
|
|
#return (self.n, sum(r.get() for r in async_results))
|
2012-02-04 22:57:57 +00:00
|
|
|
def __str__(self):
|
|
|
|
return 'Totaler (%d)' % (self.n)
|
2012-02-07 01:10:56 +00:00
|
|
|
|
2012-02-04 22:57:57 +00:00
|
|
|
|
2012-02-04 22:15:00 +00:00
|
|
|
def main():
|
|
|
|
|
|
|
|
# generate a queue to hold the results
|
2012-02-04 22:57:57 +00:00
|
|
|
tasks = multiprocessing.JoinableQueue()
|
2012-02-06 17:21:23 +00:00
|
|
|
results_queue = multiprocessing.Queue()
|
2012-02-04 22:57:57 +00:00
|
|
|
|
2012-02-07 04:16:37 +00:00
|
|
|
doubler_queue = multiprocessing.JoinableQueue()
|
2012-02-07 01:10:56 +00:00
|
|
|
|
2012-02-06 17:21:23 +00:00
|
|
|
random.seed()
|
|
|
|
|
2012-02-04 22:57:57 +00:00
|
|
|
# Start consumers
|
|
|
|
num_consumers = multiprocessing.cpu_count()
|
|
|
|
print 'Creating %d consumers' % num_consumers
|
2012-02-06 17:21:23 +00:00
|
|
|
consumers = [ Consumer(tasks, results_queue)
|
2012-02-04 22:57:57 +00:00
|
|
|
for i in xrange(num_consumers) ]
|
|
|
|
for w in consumers:
|
|
|
|
w.start()
|
2012-02-04 22:15:00 +00:00
|
|
|
|
2012-02-06 17:21:23 +00:00
|
|
|
n_tasks = 2
|
2012-02-04 22:15:00 +00:00
|
|
|
|
|
|
|
# create a separate process for each totaler operation
|
2012-02-04 22:57:57 +00:00
|
|
|
for k in xrange(n_tasks):
|
2012-02-05 17:42:07 +00:00
|
|
|
tasks.put(NetTask(k))
|
2012-02-04 22:57:57 +00:00
|
|
|
|
|
|
|
# Add a poison pill for each consumer
|
|
|
|
for i in xrange(num_consumers):
|
|
|
|
tasks.put(None)
|
|
|
|
|
2012-02-04 22:15:00 +00:00
|
|
|
# while there is an expectation of more results, read off results in the results queue
|
|
|
|
results_so_far = 0
|
2012-02-04 22:57:57 +00:00
|
|
|
net_results = {}
|
|
|
|
|
2012-02-04 22:15:00 +00:00
|
|
|
while results_so_far < n_tasks:
|
2012-02-06 17:21:23 +00:00
|
|
|
result = results_queue.get()
|
2012-02-04 22:57:57 +00:00
|
|
|
net_results[result[0]] = result[1]
|
2012-02-04 22:15:00 +00:00
|
|
|
print result
|
|
|
|
results_so_far += 1
|
|
|
|
|
2012-02-04 22:57:57 +00:00
|
|
|
print "net results", net_results
|
2012-02-04 22:15:00 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|