Skip to content

Instantly share code, notes, and snippets.

@minghan
Created July 27, 2011 09:40
Show Gist options
  • Save minghan/1109016 to your computer and use it in GitHub Desktop.
Save minghan/1109016 to your computer and use it in GitHub Desktop.
Multiprocessing example
from multiprocessing import Pool, JoinableQueue, Manager
# Note that jqueue.task_done() must be called to decrement the count if jqueue.join() is to be used correctly.
import time, os, signal
def handler(signum, frame):
print "signal caught in %d" % os.getpid()
signal.signal(signal.SIGTERM, handler)
def run_worker(q):
try:
print "start with worker %d" % os.getpid()
t = q.get_nowait()
time.sleep(t)
print "done with worker %d" % os.getpid()
except JoinableQueue.Empty:
return
q.task_done()
print "task done"
p = Pool(3)
m = Manager()
q = m.JoinableQueue()
q.put(8)
q.put(8)
q.put(8)
#q.put(8)
#q.put(8)
for i in range(3):
p.apply_async(run_worker, (q,))
print os.getpid()
p.close()
print "closed"
try:
p.join()
except IOError:
print "terminated sending"
p.terminate()
print "terminated sent"
print "joining...."
p.join()
print "done %d hm" % + os.getpid()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment