Skip to content

Instantly share code, notes, and snippets.

@pb-cdunn
Created November 30, 2015 16:10
Show Gist options
  • Save pb-cdunn/d8345c36fc572d24fe10 to your computer and use it in GitHub Desktop.
Save pb-cdunn/d8345c36fc572d24fe10 to your computer and use it in GitHub Desktop.
import multiprocessing as M
import Queue
import time, sys
def go(x):
print 'hi-%s'%repr(x)
i = 0
while True:
q.put('Q'*(1<<20))
#q.put('put-%s-%d' %(x, i))
i += 1
time.sleep(0.5)
print 'slept'
print 'bye'
def f(x):
try:
go(x)
except KeyboardInterrupt:
msg = 'handled-%s' %x
print msg
raise
def main(prog, n='1'):
global q
n = int(n)
q = M.Queue()
p = M.Pool(processes=n)
p.map_async(f, range(n))
try:
while True:
try:
got = q.get(block=True)
#got = q.get(block=True, timeout=1)
print 'got', len(got)
except Queue.Empty:
print 'Empty'
pass
except KeyboardInterrupt:
print 'handled'
time.sleep(1)
#q.close()
#q.join_thread()
p.terminate()
p.join()
#print 'sleeping before re-raising'
#time.sleep(1)
raise
print 'ending'
main(*sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment