'sys.excepthook'和线程 [英] 'sys.excepthook' and threading

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

问题描述

我正在使用Python 2.5,并尝试在我的程序中使用自定义的 excepthook 。在主线程中,它工作得很好。但是在从线程模块开始的线程中,通常会调用 excepthook



这是一个示例,显示问题。取消注释评论显示所需的行为。

 导入线程,sys 

def myexcepthook(type,value ,tb):
print'myexcepthook'

class A(threading.Thread,object):

def __init __(self):
线程。

$ run $($)
$ b $打印'A'
raise异常('in thread')

如果__name__ ==__main__:
sys.excepthook = myexcepthook
A()

那么,如何使用我自己的 excepthook 一个线程?

解决方案

看起来这个bug仍然存在于(至少)3.4,而其中的一个解决方法讨论Nadia Alramli链接似乎也在Python 3.4中工作。



为了方便和文档,我会发布代码(在我的意见n)这里最好的解决方法。我更新了编码风格和评论,使其更加PEP8和Pythonic。

  import sys 
import thread
$ b def setup_thread_excepthook():

sys.excepthook线程错误的解决方法:
http://bugs.python.org/issue1230540

在创建任何线程之前从主线程调用一次


init_original = threading.Thread .__ init__

def init

init_original(self,* args,** kwargs)
run_original = self.run

def run_with_except_hook(* args2,** kwargs2):
try:
run_original(* args2,** kwargs2)
除了异常:
sys.excepthook(* sys.exc_info())

self.run = run_with_except_hook

threading.Thread .__ init__ = init


I am using Python 2.5 and trying to use a self-defined excepthook in my program. In the main thread it works perfectly fine. But in a thread started with the threading module the usual excepthook is called.

Here is an example showing the problem. Uncommenting the comment shows the desired behaviour.

import threading, sys

def myexcepthook(type, value, tb):
    print 'myexcepthook'

class A(threading.Thread, object):

    def __init__(self):
        threading.Thread.__init__(self, verbose=True)
#       raise Exception('in main')
        self.start()

    def run(self):
        print 'A'
        raise Exception('in thread')            

if __name__ == "__main__":
    sys.excepthook = myexcepthook
    A()

So, how can I use my own excepthook in a thread?

解决方案

It looks like this bug is still present in (at least) 3.4, and one of the workarounds in the discussion Nadia Alramli linked seems to work in Python 3.4 too.

For convenience and documentation sake, I'll post the code for (in my opinion) the best workaround here. I updated the coding style and comments slightly to make it more PEP8 and Pythonic.

import sys
import threading

def setup_thread_excepthook():
    """
    Workaround for `sys.excepthook` thread bug from:
    http://bugs.python.org/issue1230540

    Call once from the main thread before creating any threads.
    """

    init_original = threading.Thread.__init__

    def init(self, *args, **kwargs):

        init_original(self, *args, **kwargs)
        run_original = self.run

        def run_with_except_hook(*args2, **kwargs2):
            try:
                run_original(*args2, **kwargs2)
            except Exception:
                sys.excepthook(*sys.exc_info())

        self.run = run_with_except_hook

    threading.Thread.__init__ = init

这篇关于'sys.excepthook'和线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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