Skip to content

Instantly share code, notes, and snippets.

@tokoroten
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tokoroten/218b6c7d227435f67c9d to your computer and use it in GitHub Desktop.
Save tokoroten/218b6c7d227435f67c9d to your computer and use it in GitHub Desktop.
python multiprocess_pool error handling
#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