Created
November 30, 2015 16:10
-
-
Save pb-cdunn/d8345c36fc572d24fe10 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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