擅长:python、mysql、java
<p>辅助线程能够在主线程中可靠地引发的唯一异常是<code>KeyboardInterrupt</code>:辅助线程的方法是调用函数<a href="https://docs.python.org/3/library/_thread.html#_thread.interrupt_main" rel="nofollow noreferrer">^{<cd2>}</a>。无法将额外的信息(关于异常原因)与引发的异常对象关联起来——后者总是一个简单的<code>KeyboardInterrupt</code>。因此,您需要将这些信息保存在其他地方,例如在<a href="http://docs.python.org/library/queue.html?highlight=queue#Queue.Queue" rel="nofollow noreferrer">Queue.Queue</a>的一个专用实例上——这些信息可能包括辅助线程通过<code>sys.exc_info()</code>可以获得的结果,以及您发现有用的任何其他信息。</p>
<p>主线程将需要恢复额外的信息(并考虑到如果键盘中断实际上是由于用户点击control-C或类似命令造成的,则队列将为空,因此,请使用<code>get_nowait</code>,并准备好处理<code>Queue.Empty</code>异常),然后根据需要格式化它,和terminate(如果所有次线程都是<a href="http://docs.python.org/library/threading.html?highlight=thread%20main#threading.Thread.daemon" rel="nofollow noreferrer">daemon</a>s,则整个进程在主线程终止时终止)。</p>