线程无异常地死亡

发布于 2024-11-05 05:32:17 字数 746 浏览 0 评论 0原文

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

 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。由于这种方法,我发现了一些很大的小错误,但我的问题是工人仍然在死亡,我不知道如何进一步记录正在发生的事情或排除故障。

任何想法将不胜感激。

谢谢!

I'm having an issue with some of my worker threads. I've added a catchall exception statement in the thread's run method like so:

 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())

I read this was the defacto way to catch everything that may be causing an issue instead of using Exception, e. I've found some great little bugs thanks to this method but my problem is that the workers' are still dying and I'm not sure how to further record what's going on or troubleshoot.

Any thoughts would be greatly appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千紇 2024-11-12 05:32:17

您可以尝试使用 trace 模块检查程序的执行跟踪。例如:

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

Source:

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
...

You could try examining the execution trace of your program using the trace module. For example:

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

Source:

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()

It will dump out each line as it is executed, and you should get a coverage report in the coverage directory:

...
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
...
独﹏钓一江月 2024-11-12 05:32:17

是什么让您认为某些线程过早退出?他们是否可能干净地退出,但您的日志记录方法不是线程安全的?

What makes you think that some threads are exiting prematurely? Is it possible they're exiting cleanly but your logging method isn't thread-safe?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文