multiprocessing: excepthook not getting called

Philipp Hagemeister phihag at phihag.de
Wed Jun 13 05:23:43 EDT 2012



On 06/13/2012 11:00 AM, Dave Cook wrote:
> Originally, I was trying to send formatted
> tracebacks back to the main process on a queue.
You can still do that:

import multiprocessing
import sys

def queueErrors(q):
    def decorator(func):
        def wrapped(*args, **kwargs):
            try:
                func()
            except:
                q.put((multiprocessing.current_process().name,
repr(sys.exc_info())))
        return wrapped
    return decorator

q = multiprocessing.Queue()

@queueErrors(q)
def target():
    raise ValueError()

if __name__ == '__main__':
    p = multiprocessing.Process(target=target)
    p.start()
    p.join()
    # try it here in main
    while True:
        pname,exc = q.get()
        print('Caught error in process %r: %s' % (pname, exc))

It gets somewhat harder when you also want to catch termination of
threads, but you can just queue a special message.

- Philipp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20120613/503a3d72/attachment.sig>


More information about the Python-list mailing list