Skip to content
Browse files

Initial commit of example code

  • Loading branch information...
0 parents commit c14ff016793098484591f705bfeacf120bea21f7 @jreese committed May 31, 2011
Showing with 45 additions and 0 deletions.
  1. +40 −0 example.py
  2. +5 −0 readme.mkdn
40 example.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+
+# Copyright (c) 2011 John Reese
+# Licensed under the MIT License
+
+import multiprocessing
+import os
+import signal
+import time
+
+def init_worker():
+ signal.signal(signal.SIGINT, signal.SIG_IGN)
+
+def run_worker():
+ time.sleep(15)
+
+def main():
+ print "Initializng 5 workers"
+ pool = multiprocessing.Pool(5, init_worker)
+
+ print "Starting 3 jobs of 15 seconds each"
+ for i in range(3):
+ pool.apply_async(run_worker)
+
+ try:
+ print "Waiting 10 seconds"
+ time.sleep(10)
+
+ except KeyboardInterrupt:
+ print "Caught KeyboardInterrupt, terminating workers"
+ pool.terminate()
+ pool.join()
+
+ else:
+ print "Quitting normally"
+ pool.close()
+ pool.join()
+
+if __name__ == "__main__":
+ main()
5 readme.mkdn
@@ -0,0 +1,5 @@
+This code is an example of how to handle Python's KeyboardInterrupt while also using the multiprocessing
+module's Pool for managing a set of worker processes. For more information, visit:
+
+* http://noswap.com/blog/python-multiprocessing-keyboardinterrupt/
+

0 comments on commit c14ff01

Please sign in to comment.
Something went wrong with that request. Please try again.