Created
September 2, 2016 17:33
-
-
Save FrankMN/5494de5ae571faf181276dbf21d39a8e to your computer and use it in GitHub Desktop.
python multiprocessing with some sub-process doesn't exit properly
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
| import time | |
| from multiprocessing import Pool, TimeoutError | |
| import multiprocessing | |
| def run(input): | |
| name = multiprocessing.current_process().name | |
| print name, 'start' | |
| if input == 1000000-1: | |
| # this try to emulate one process doesn't end properly | |
| time.sleep(100) | |
| return input ** 2 | |
| print name, 'end' | |
| if __name__ == '__main__': | |
| p = Pool(processes=4) | |
| nums = range(1000000) | |
| name = multiprocessing.current_process().name | |
| print "main process", name, 'start' | |
| start = time.time() | |
| res = p.map_async(run, nums) | |
| try: | |
| print res.get(timeout=5) | |
| except TimeoutError as te: | |
| print te.message | |
| end = time.time() | |
| print 'main process', name, 'end' | |
| print 'multi-processes runs %0.2f seconds.' % ((end - start)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment