Qix- / better-exceptions

Pretty and useful exceptions in Python, automatically.
MIT License
4.59k stars 202 forks source link

Can't work in child process #129

Closed yafeng520 closed 7 months ago

yafeng520 commented 1 year ago

code of running in the child process

def run_child_process():
    exception_name = 'exception_in_child_process'
    raise Exception(exception_name)

if __name__ == "__main__":
    from multiprocessing import Process
    p = Process(target=run_child_process)
    p.start()
    p.join()

result

Process Process-1: Traceback (most recent call last): File "/opt/homebrew/Cellar/python@3.10/3.10.12_1/Frameworks/Python.framework/Versions/3.10/lib/python3.10/multiprocessing/process.py", line 314, in _bootstrap self.run() File "/opt/homebrew/Cellar/python@3.10/3.10.12_1/Frameworks/Python.framework/Versions/3.10/lib/python3.10/multiprocessing/process.py", line 108, in run self._target(*self._args, **self._kwargs) File "/Users/fuyafeng/test/traceback_with_variables_test.py", line 6, in run_child_process raise Exception(exception_name) Exception: exception_in_child_process

code of running in the main process

if __name__ == "__main__":
    exception_name = 'exception_in_main_process'
    raise Exception(exception_name)

result

Traceback (most recent call last): File "/Users/fuyafeng/test/traceback_with_variables_test.py", line 8, in raise Exception(exception_name) └ 'exception_in_main_process' Exception: exception_in_main_process

muthvar1 commented 11 months ago

Seeing the same issue. I think this issue is across both better exceptions module and rich traceback too. It does not work across threads, and only works within the main thread

James4Ever0 commented 7 months ago

I had the same problem too. When using this with sse_starlette which runs tasks in TaskGroup it will not trace exceptions in the tasks, leaving me frustrated for hours because of unable to find anything useful in the traceback.

I want to know if any library can handle exceptions in separate thread.

Qix- commented 7 months ago

Eh yeah, this is to be expected. There's nothing magic that we're doing here. You'd have to re-initialize better-exceptions in each thread/child process you're spawning. They're going to behave as though they're fresh processes.

Unless I'm missing something, this is expressly out of scope for the project :/ Sorry about that.