gpt4 book ai didi

python - 线程无一异常(exception)地死去

转载 作者:太空宇宙 更新时间:2023-11-03 12:29:15 25 4
gpt4 key购买 nike

我的一些工作线程有问题。我在线程的运行方法中添加了一个包罗万象的异常语句,如下所示:

 try:
"""Runs the worker process, which is a state machine"""
while self._set_exitcode is None :
assert self._state in Worker.STATES
state_methodname = "_state_%s" % self._state
assert hasattr(self, state_methodname)
state_method = getattr(self, state_methodname)
self._state = state_method() # execute method for current state

self._stop_heartbeat()
sys.exit( self._set_exitcode )
except:

self.log.debug(sys.exc_info())

我读到这是捕获可能导致问题的所有内容而不是使用 Exception, e 的事实上的方法。多亏了这种方法,我发现了一些很棒的小错误,但我的问题是工作人员仍在死去,我不确定如何进一步记录正在发生的事情或进行故障排除。

如有任何想法,我们将不胜感激。

谢谢!

最佳答案

您可以尝试检查 execution trace of your program using the trace module .例如:

% python -m trace -c -t -C ./coverage test_exit.py

来源:

import sys
import threading

class Worker(object):
def run(self):
try:
sys.exit(1)
except:
print sys.exc_info()

threading.Thread(target=Worker().run).start()

它会在执行时转储出每一行,您应该在 coverage 目录中获得一份覆盖率报告:

...
threading.py(482): try:
threading.py(483): if self.__target:
threading.py(484): self.__target(*self.__args, **self.__kwargs)
--- modulename: test_exit, funcname: run
test_exit.py(7): try:
test_exit.py(8): sys.exit(1)
test_exit.py(9): except:
test_exit.py(10): print sys.exc_info()
(<type 'exceptions.SystemExit'>, SystemExit(1,), <traceback object at 0x7f23098822d8>)
threading.py(488): del self.__target, self.__args, self.__kwargs
...

关于python - 线程无一异常(exception)地死去,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5885928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com