Permalink
Browse files

Merge pull request #1 from l33twolf/master

Extension of KeyboardInterrupt handling to Process subclass
  • Loading branch information...
2 parents c14ff01 + c2daea5 commit 62dabbbf0feb4df8557451b20550b07baa4e99df @jreese committed Jun 16, 2011
Showing with 54 additions and 2 deletions.
  1. +54 −2 example.py
View
@@ -14,7 +14,7 @@ def init_worker():
def run_worker():
time.sleep(15)
-def main():
+def poolExample():
print "Initializng 5 workers"
pool = multiprocessing.Pool(5, init_worker)
@@ -36,5 +36,57 @@ def main():
pool.close()
pool.join()
+
+class ConsumerProcess( multiprocessing.Process ):
+ def __init__( self, q, *args, **kwargs ):
+ self.q = q
+ super( ConsumerProcess, self ).__init__( *args, **kwargs )
+ self.start()
+
+ def run( self ):
+ init_worker()
+ ps = []
+ for d in iter( self.q.get, None ):
+ if( d == 'killjobs' ):
+ for p in ps:
+ p.terminate()
+
+ else:
+ ps.append( multiprocessing.Process( target=run_worker ) )
+ ps[-1].daemon = True
+ ps[-1].start()
+
+ for p in ps:
+ p.join()
+
+
+def processExample():
+ print "Initializing consumer process"
+ q = multiprocessing.Queue()
+
+ p = ConsumerProcess( q )
+
+ print "Starting 3 jobs of 15 seconds each"
+ for i in range(3):
+ q.put( i )
+
+ try:
+ print "Waiting 10 seconds"
+ time.sleep( 10 )
+
+ except KeyboardInterrupt:
+ print "Caught KeyboardInterrupt, terminating consumer"
+ q.put( 'killjobs' )
+
+ else:
+ print "Quitting normally"
+
+ finally:
+ q.put( None )
+ q.close()
+ p.join()
+
+
if __name__ == "__main__":
- main()
+ poolExample()
+ processExample()

0 comments on commit 62dabbb

Please sign in to comment.