如何在线程中处理Python异常?
给定的代码被重写以捕获异常
import sys
import threading
import time
import Queue
def thread(args1, stop_event, queue_obj):
print "start thread"
stop_event.wait(12)
if not stop_event.is_set():
try:
raise Exception("boom!")
except Exception:
queue_obj.put(sys.exc_info())
pass
try:
queue_obj = Queue.Queue()
t_stop = threading.Event()
t = threading.Thread(target=thread, args=(1, t_stop, queue_obj))
t.start()
time.sleep(15)
print "停止线程!"
t_stop.set()
try:
exc = queue_obj.get(block=False)
except Queue.Empty:
pass
else:
exc_type, exc_obj, exc_trace = exc
print exc_obj
except Exception as e:
print "It took too long"输出值
C:/Users/nhooo1/~.py start thread 停止线程! boom!
热门推荐
8 敏字的祝福语简短
10 新年祝福语小知识简短
11 岁岁祝福语简短独特
12 道教生日祝福语简短大全
13 阳历新年祝福语大全 简短
14 写字生日祝福语简短独特
15 妈妈生日祝福语简短温暖
16 高考给舍友祝福语简短
17 给男生的简短祝福语
18 别人结婚舞台祝福语简短