Catching exceptions with multi-processing

Jean-Michel Pichavant jeanmichel at sequans.com
Fri Jun 19 11:17:10 EDT 2015


----- Original Message -----
> From: "Oscar Benjamin" <oscar.j.benjamin at gmail.com>
> A simple way to approach this could be something like:
> 
> #!/usr/bin/env python3
> 
> import math
> import multiprocessing
> 
> def sqrt(x):
>     if x < 0:
>         return 'error', x
>     else:
>         return 'success', math.sqrt(x)
> 
> if __name__ == "__main__":
>     numbers = [1, 2, 3, -1, -3]
>     pool = multiprocessing.Pool()
>     for ret, val in pool.imap(sqrt, numbers):
>         if ret == 'error':
>             raise ValueError(val)
>         print(val)
> 
> Just replace the raise statement with whatever you want to do (write
> to a file etc). Since all errors are handled in the master process
> there are no issues with writing to a file.
> 
> --
> Oscar

The main problem with this approach is that it does not handle unexpected exceptions within subprocesses.

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


More information about the Python-list mailing list