我正在使用 ThreadPoolExecutor 执行导致错误的函数。当一个线程内发生错误(并且它未被处理)时,看起来它正在终止该线程,但不会向 stderr 打印任何内容或导致程序崩溃。为什么会这样?我可以做到这一点,以便任何线程中未处理的错误导致整个程序崩溃吗?或者它可以打印在某个地方,这样我就可以看到发生了什么。
我注意到错误可以在 try-catch block 中捕获,因此它肯定会被引发。如果在没有线程的情况下调用相同的函数,则会按预期引发错误。
from concurrent.futures import ThreadPoolExecutor
import time
def error_causer(x):
print(x)
raise ValueError("This bad!")
with ThreadPoolExecutor(max_workers=1) as executor:
executor.map(error_causer, ["coming from thread"])
print("After don't catch. Should have crashed by now...")
time.sleep(2)
error_causer("coming from outside thread")
当在线程内引发 ValueError 时,此程序不会崩溃,但在线程外引发 ValueError 时会崩溃。
输出是:
coming from thread
After don't catch. Should have crashed by now...
coming from outside thread
Traceback (most recent call last):
File "./another_threading_test.py", line 16, in <module>
error_causer("coming from outside thread")
File "./another_threading_test.py", line 7, in error_causer
raise ValueError("This bad!")
ValueError: This bad!
我预计它会在 print("after don't catch")
请您参考如下方法:
executor.map() 不会引发错误,因为工作线程会静默终止而不会引发错误,除非您尝试从迭代器中检索结果,这是根据 docs :
map(func, *iterables, timeout=None, chunksize=1)
If a func call raises an exception, then that exception will be raised when its value is retrieved from the iterator.
您可以通过在返回的 iterator 上调用 next() 来验证这一点:
def error_causer(x):
print(x)
raise ValueError("This bad!")
with ThreadPoolExecutor(max_workers=1) as executor:
iterator = executor.map(error_causer, ["coming from thread"])
next(iterator)
现在在迭代器上调用 next() 后,您会看到主线程中引发的错误来自终止主线程的工作线程:
Traceback (most recent call last):
File "/Users/ambhowmi/PycharmProjects/Datastructures/TwoStacksQueue.py", line 13, in <module>
next(iterator)
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/_base.py", line 598, in result_iterator
yield fs.pop().result()
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/_base.py", line 428, in result
return self.__get_result()
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/Users/ambhowmi/PycharmProjects/Datastructures/TwoStacksQueue.py", line 8, in error_causer
raise ValueError("This bad!")
ValueError: This bad!
