Last active
January 3, 2016 04:59
-
-
Save jtushman/8412630 to your computer and use it in GitHub Desktop.
Another mutliprocessing Ctr-C (Keyboard Interupt) that is not working -- but seems more elegant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from multiprocessing import Process, Manager, Event | |
from time import sleep | |
def f(process_number, shared_array, kb_interupt): | |
print "starting thread: ", process_number | |
while not kb_interupt.is_set(): | |
try: | |
shared_array.append(process_number) | |
sleep(3) | |
except KeyboardInterrupt: | |
print "Ignoring Keyboard interupt in process: ", process_number | |
finally: | |
print "cleaning up thread", process_number | |
else: | |
print "handling kb_interupt Event" | |
if __name__ == '__main__': | |
processes = [] | |
manager = Manager() | |
shared_array = manager.list() | |
kb_interupt = Event() | |
for i in xrange(4): | |
p = Process(target=f, args=(i, shared_array, kb_interupt)) | |
p.start() | |
processes.append(p) | |
try: | |
for process in processes: | |
process.join() | |
except KeyboardInterrupt: | |
print "Keyboard interupt in main -- trying to shut down" | |
kb_interupt.set() | |
for process in processes: | |
process.join() | |
for item in shared_array: | |
# raises "socket.error: [Errno 2] No such file or directory" | |
print item |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment