Created
April 18, 2014 06:52
-
-
Save cldotdev/11028209 to your computer and use it in GitHub Desktop.
Python 2.7 Multiprocessing and KeyboardInterrupts
This file contains hidden or 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
| #!/usr/bin/env python | |
| # http://jtushman.github.io/blog/2014/01/14/python-%7C-multiprocessing-and-interrupts/ | |
| from multiprocessing import Process | |
| from multiprocessing.managers import SyncManager | |
| import time | |
| import signal | |
| def mgr_init(): | |
| signal.signal(signal.SIGINT, signal.SIG_IGN) | |
| print 'initialized manager' | |
| def f(x): | |
| try: | |
| if x: | |
| time.sleep(2) | |
| print (x.pop()) | |
| except KeyboardInterrupt: | |
| print('keyboard interrupt in process') | |
| if __name__ == '__main__': | |
| manager = SyncManager() | |
| manager.start(mgr_init) | |
| shared_array = manager.list(range(19)) | |
| processes = [] | |
| try: | |
| while shared_array: | |
| if len(shared_array) > 4: | |
| for i in range(4): | |
| p = Process(target=f, args=(shared_array,)) | |
| p.start() | |
| processes.append(p) | |
| else: | |
| for i in range(len(shared_array)): | |
| p = Process(target=f, args=(shared_array,)) | |
| p.start() | |
| processes.append(p) | |
| for i in processes: | |
| i.join() | |
| processes[:] = [] | |
| except KeyboardInterrupt: | |
| print('interrupt') | |
| finally: | |
| manager.shutdown() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment