multiprocessing: excepthook not getting called

D

Dave Cook

Why doesn't my excepthook get called in the child process?

import sys
import multiprocessing as mp

def target():
name = mp.current_process().name
def exceptHook(*args):
print 'exceptHook:', name, args
sys.excepthook = exceptHook
raise ValueError

if __name__=='__main__':
p = mp.Process(target=target)
p.start()
p.join()
# try it here in main
target()

Thanks,
Dave Cook
 
P

Philipp Hagemeister

multiprocessing just mimicks the threading module here, see
http://bugs.python.org/issue1230540 . Why do you need excepthook in the
first place?

You can perfectly simulate it by wrapping the root method (target in
your example) in a try .. catch:

import multiprocessing
import sys

def printErrors(func):
def wrapped(*args, **kwargs):
try:
func()
except:
print ('except: ', sys.exc_info())
return wrapped

@printErrors
def target():
raise ValueError()

if __name__ == '__main__':
p = multiprocessing.Process(target=target)
p.start()
p.join()
# try it here in main
target()


Cheers,

Philipp


Why doesn't my excepthook get called in the child process?

import sys
import multiprocessing as mp

def target():
name = mp.current_process().name
def exceptHook(*args):
print 'exceptHook:', name, args
sys.excepthook = exceptHook
raise ValueError

if __name__=='__main__':
p = mp.Process(target=target)
p.start()
p.join()
# try it here in main
target()

Thanks,
Dave Cook



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEAREKAAYFAk/YUeUACgkQ9eq1gvr7CFyvFACdFAJhbrKFbKfJ7Dy1B7p2JwjQ
xrwAn3Jd+GbQdKOdagrBY5jk+GCTvwGB
=tcyv
-----END PGP SIGNATURE-----
 
P

Philipp Hagemeister

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


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEAREKAAYFAk/YXCEACgkQ9eq1gvr7CFzgowCgrazVtrR5LJ6JyYsuIjMEV6m8
qmwAoId+KLB+erZJoLBbUWceWQ+1oSkl
=Zty9
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top