【问题标题】:Can not catch exception in threaded program无法在线程程序中捕获异常
【发布时间】:2013-11-05 11:06:57
【问题描述】:

下面的 Python 程序启动一个线程,然后继续在主线程中执行操作。我将整个主线程包装在一个 try-except 块中,这样如果发生异常,我可以关闭所有正在运行的线程。

当我使用 Python 2.7.5 运行脚本并在程序执行期间在随机点调用 KeyboardInterrupt 时,异常被触发但未被捕获。程序继续运行。

$ python test.py 
Running server ...
Searching for servers ...
^CTraceback (most recent call last):
  File "test.py", line 50, in <module>
    main()
  File "test.py", line 40, in main
    app_main()
  File "test.py", line 35, in app_main
    searchservers()
  File "test.py", line 26, in searchservers
    time.sleep(0.0005)
KeyboardInterrupt

当发生异常时,我错过了输出中的一行,该行在main() 中打印。


代码

import time
import threading

thread_pool = []
running = False

def stop():
    global running
    running = False

def runserver():
    print "Running server ..."

    global running
    running = True

    while running:
        time.sleep(0.07)

def searchservers():
    print "Searching for servers ..."

    for i in xrange(256):
        for j in xrange(256):
            time.sleep(0.0005)

def app_main():
    server = threading.Thread(target=runserver)
    thread_pool.append(server)
    server.start()

    time.sleep(0.1)

    searchservers()
    stop()

def main():
    try:
        app_main()
    except Exception as exc:
        stop()
        print "%s occured, joining all threads..." % exc.__class__.__name__
        for thread in thread_pool:
            thread.join()

        raise exc

if __name__ == "__main__":
    main()

为什么没有捕获到 KeyboardInterrupt?在线程程序中捕获异常并拆除整个过程的正确方法是什么?

【问题讨论】:

  • 这是一个非常好的问题,我期待着阅读适当的答案。

标签: python multithreading unix exception-handling python-2.x


【解决方案1】:

KeyboardInterrupt 是一个特殊异常;像 MemoryErrorGeneratorExitSystemExit,它确实不是派生自 Exception 基类。

因此仅捕获Exception 是不够的;你通常会明确地抓住它:

except (Exception, KeyboardInterrupt) as exc:

但是,您也试图在 threads 中捕获异常;线程有自己独立的栈;您不能只去捕获主线程中那些堆栈中抛出的异常。您必须在该线程中捕获异常

def runserver():
    print "Running server ..."

    global running
    running = True

    try:    
        while running:
            time.sleep(0.07)
    except (Exception, KeyboardInterrupt) as exc:
        print "Error in the runserver thread"

要以通用方式处理此问题并将异常“传递”到主线程,您需要某种线程间通信。有关该问题的完整解决方案,请参阅 Catch a thread's exception in the caller thread in Python

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 2015-08-31
    相关资源
    最近更新 更多