'sys.excepthook'和线程

Seb*_*ian 18 python multithreading exception-handling exception

我正在使用Python 2.5并尝试excepthook在我的程序中使用自定义.在主线程中,它完美地运行.但是在一个以线程模块开始的线程中,通常excepthook会调用它.

这是一个显示问题的示例.取消注释注释会显示所需的行为.

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()
Run Code Online (Sandbox Code Playgroud)

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

git*_*rik 18

看起来这个bug仍然存在于(至少)3.4中,Nadia Alramli所讨论的讨论中的一个解决方案似乎也适用于Python 3.4.

为方便起见,我会在这里发布代码(在我看来)最好的解决方法.我稍微更新了编码风格和注释,使其更加PEP8和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
Run Code Online (Sandbox Code Playgroud)

  • 该错误从 python 3.8 开始已修复。 (3认同)

Nad*_*mli 11

看起来这里报告一个相关的错误,有变通方法.建议的hacks基本上是在try/catch中运行,然后调用sys.excepthook(*sys.exc_info())


joh*_*son 8

我只是偶然发现了这个问题,事实证明,现在是时候这样做了。

3.8 新版功能:threading.excepthook

处理 Thread.run() 引发的未捕获异常。

args 参数具有以下属性:

exc_type:异常类型。
exc_value:异常值,可以为None。
exc_traceback:异常回溯,可以为None。
线程:引发异常的线程,可以是无。

我不知道为什么,但请注意,与 不同sys.excepthookthreading.excepthook将参数作为 anamedtuple而不是多个参数接收。

  • 这个答案现在应该得到更高的评价。如果您希望处理主线程之外的异常,请简单地设置“threading.excepthook”以及“sys.excepthook” (4认同)