线程死亡无一例外 [英] thread dying without exception

查看:78
本文介绍了线程死亡无一例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的某些工作线程有问题.我在线程的run方法中添加了一个catchall异常语句,如下所示:

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

我读到,这是捕获所有可能引起问题的方法的实际方法,而不是使用Exception, e.由于此方法,我发现了一些很棒的小错误,但是我的问题是工人们仍在垂死之中,我不确定如何进一步记录正在发生的事情或进行故障排除.

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.

谢谢!

推荐答案

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

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

来源:

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目录中获得一份覆盖率报告:

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

这篇关于线程死亡无一例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆