Guest User

Keyboard Interrupts with python's multiprocessing Pool

a guest
Dec 24th, 2011
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. from multiprocessing import Pool
  2. from time import sleep
  3. from sys import exit
  4.  
  5. def slowly_square(i):
  6. sleep(1)
  7. return i*i
  8.  
  9. def go():
  10. pool = Pool(8)
  11. try:
  12. results = pool.map(slowly_square, range(40))
  13. except KeyboardInterrupt:
  14. # **** THIS PART NEVER EXECUTES. ****
  15. pool.terminate()
  16. print "You cancelled the program!"
  17. sys.exit(1)
  18. print "nFinally, here are the results: ", results
  19.  
  20. if __name__ == "__main__":
  21. go()
  22.  
  23. import threading
  24. cond = threading.Condition(threading.Lock())
  25. cond.acquire()
  26. cond.wait(None)
  27. print "done"
  28.  
  29. results = pool.map(slowly_square, range(40))
  30.  
  31. results = pool.map_async(slowly_square, range(40)).get(9999999)
  32.  
  33. from multiprocessing import Pool
  34. import time
  35.  
  36. class KeyboardInterruptError(Exception): pass
  37.  
  38. def f(x):
  39. try:
  40. time.sleep(x)
  41. return x
  42. except KeyboardInterrupt:
  43. raise KeyboardInterruptError()
  44.  
  45. def main():
  46. p = Pool(processes=4)
  47. try:
  48. print 'starting the pool map'
  49. print p.map(f, range(10))
  50. p.close()
  51. print 'pool map complete'
  52. except KeyboardInterrupt:
  53. print 'got ^C while pool mapping, terminating the pool'
  54. p.terminate()
  55. print 'pool is terminated'
  56. except Exception, e:
  57. print 'got exception: %r, terminating the pool' % (e,)
  58. p.terminate()
  59. print 'pool is terminated'
  60. finally:
  61. print 'joining pool processes'
  62. p.join()
  63. print 'join complete'
  64. print 'the end'
  65.  
  66. if __name__ == '__main__':
  67. main()
  68.  
  69. staring the pool map
  70. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  71. pool map complete
  72. joining pool processes
  73. join complete
  74. the end
  75.  
  76. staring the pool map
  77. got ^C while pool mapping, terminating the pool
  78. pool is terminated
  79. joining pool processes
  80. join complete
  81. the end
  82.  
  83. import signal
  84.  
  85. ...
  86.  
  87. def init_worker():
  88. signal.signal(signal.SIGINT, signal.SIG_IGN)
  89.  
  90. ...
  91.  
  92. def main()
  93. pool = multiprocessing.Pool(size, init_worker)
  94.  
  95. ...
  96.  
  97. except KeyboardInterrupt:
  98. pool.terminate()
  99. pool.wait()
  100.  
  101. def slowly_square(i):
  102. try:
  103. sleep(1)
  104. return i * i
  105. except KeyboardInterrupt:
  106. print 'You EVIL bastard!'
  107. return 0
Advertisement
Add Comment
Please, Sign In to add comment