forked from amyreese/multiprocessing-keyboardinterrupt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
92 lines (68 loc) · 1.92 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/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 poolExample():
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()
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__":
poolExample()
processExample()