thread dying without exception
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 th开发者_如何学编程e 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!
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
...
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?
精彩评论