Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement