Last active
August 29, 2015 14:05
-
-
Save tokoroten/218b6c7d227435f67c9d to your computer and use it in GitHub Desktop.
python multiprocess_pool error handling
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
#coding:utf-8 | |
import multiprocessing | |
import traceback | |
import random | |
import time | |
import sys | |
def random_error_func(x): | |
n = random.random() | |
time.sleep(0.5) | |
if n < 0.1: | |
return 1/0 # make exception | |
else: | |
return x*x | |
if __name__ == "__main__": | |
pool = multiprocessing.Pool(10) | |
results = [] | |
for i in xrange(20): | |
r = pool.apply_async(random_error_func, (i,)) | |
results.append(r) | |
for result in results: | |
try: | |
print result.get() # sub process error, coming here | |
except: | |
print traceback.format_exc(sys.exc_info()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment