Skip to content

Instantly share code, notes, and snippets.

@oseiskar
Created March 24, 2018 07:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oseiskar/dbd38098038df8944e21b41c42668440 to your computer and use it in GitHub Desktop.
Save oseiskar/dbd38098038df8944e21b41c42668440 to your computer and use it in GitHub Desktop.
Python multiprocessing and exception handling example with pathos
from pathos.multiprocessing import ProcessingPool as Pool
def parallel_map(func, array, n_workers):
def compute_batch(i):
try:
return func(i)
except KeyboardInterrupt:
raise RuntimeError("Keyboard interrupt")
p = Pool(n_workers)
err = None
# pylint: disable=W0703,E0702
# some bs boilerplate from StackOverflow
try:
return p.map(compute_batch, array)
except KeyboardInterrupt, e:
print 'got ^C while pool mapping, terminating the pool'
p.terminate()
err = e
except Exception, e:
print 'got exception: %r:, terminating the pool' % (e,)
p.terminate()
err = e
if err is not None:
raise err
# example
if __name__ == '__main__':
import time
def do_work(i):
print('italian strike %i' % i)
time.sleep(2)
return i
# note: can't pickle functions with the normal multiprocessing package
print(parallel_map(do_work, range(10), n_workers=3))
@chrisvaughn
Copy link

many thanks for posting this. I've been struggling to handle KeyboardInterrupt and general exceptions in just a regular python multiprocessing pool this example helped point out what I was doing wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment