Last active
October 6, 2016 09:47
-
-
Save moskytw/7c4174af969cd35c9aa2fb9872ec4cbd to your computer and use it in GitHub Desktop.
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 | |
# -*- coding: utf-8 -*- | |
import signal | |
from time import sleep | |
from multiprocessing import TimeoutError | |
from multiprocessing.dummy import Pool | |
def work(i): | |
print('Working on {} ...'.format(i)) | |
sleep(1) | |
return i | |
def complicated_main(): | |
'''Work on both Python 2 and 3. | |
Python 2's main thread will wait non-daemon threads, and lock can't be | |
interrupt by signal. | |
''' | |
print('Running complicated main ...') | |
pool = Pool(4) | |
# hack: make the flag writable in the handler | |
pool.ctrl_c_happens = False | |
# stop running and waiting if Ctrl-C | |
def end_pool(signum, frame): | |
print('Ctrl-C happens!') | |
pool.terminate() | |
pool.ctrl_c_happens = True | |
signal.signal(signal.SIGINT, end_pool) | |
# run and wait tasks | |
# mapr: map result | |
mapr = pool.map_async(work, range(8)) | |
retvals = [] | |
while True: | |
try: | |
print('Are tasks finished?') | |
retvals = mapr.get(timeout=1) | |
except TimeoutError: | |
pass | |
else: | |
break | |
print('Does Ctrl-C happen?') | |
if pool.ctrl_c_happens: | |
break | |
print(retvals) | |
def simple_main(): | |
'''Work on Python 3.2 up only. | |
Python 3.2 up's main thread will also wait non-daemon threads, but lock | |
can be interrupt by signal. | |
''' | |
print('Running simple main ...') | |
pool = Pool(4) | |
try: | |
print(pool.map(work, range(8))) | |
except KeyboardInterrupt: | |
print([]) | |
if __name__ == '__main__': | |
import sys | |
if len(sys.argv) >= 2 and sys.argv[1] == 'simple': | |
simple_main() | |
else: | |
complicated_main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment