Search code examples
pythonpython-3.xmultiprocessingpython-multiprocessing

python3 how to find out which line in apply_async target function caused error when error_callback is applied


Using python 3.4

I have a target function that fails and apply_async calls my error callback function instead of my callback function.

The problem is that the exception captured and sent to the error_callback is not helpful and I can't figure out where the error in the target function occurs.

My error callback

  def error_callback(self,e):
    print('error_callback()',e)
    # tested one of these at a time
    traceback.print_exc()
    traceback.print_last()
    traceback.print_stack()

And the traceback outputs

print_exc

  File "/usr/lib/python3.4/traceback.py", line 125, in _iter_chain
    context = exc.__context__
  AttributeError: 'NoneType' object has no attribute '__context__'

print_last

  File "/usr/lib/python3.4/traceback.py", line 262, in print_last
    raise ValueError("no last exception")
  ValueError: no last exception

print_stack

  File "./relative/path/to/my/script.py", line 71, in traceback
    traceback.print_stack()

I'd have to confess that I ain't really comfortable with the traceback library and how to use it but it seems to me that it's not possible to use traceback in this case?

What should I do?


Solution

  • The traceback functions you're calling are all expecting to get the exception info from sys.exec_info, which is where exception information is usually stored while an exception is being caught. However, since the actual exception in your case came from a child process, they're not finding any exception information when you call them in the parent process.

    You need to use extract the relevant information from the Exception object e that you received from the child process. Try using traceback.print_exception with appropriate arguments:

    traceback.print_exception(type(e), e, e.__traceback__)