小弟是Python的初学者,在学习多线程编程的时候碰到了一个问题。
代码如下:
# 1.py
# start
import sys, time, threading
def thread_recv():
while True:
time.sleep(0.001)
# do something then loop
def main():
thread_recv_ = threading.Thread(target=thread_recv)
thread_recv_.setDaemon(True)
thread_recv_.start()
# do someting then quit
for i in range(10000):
pass
if __name__ == "__main__":
main()
# end
这个程序经常在正常退出时报错:
Unhandled exception in thread started by
Error in sys.excepthook:
Original exception was:
好像是 thread_recv_ 比 main() 函数晚退出,访问了某些已经不存在的对象?
但我已经将其 setDaemon(True) 了呀,按理应该安静地随着main()一起退出才对。
还有如果强制退出是调用sys.exit()吗?如果用户主动关闭程序,应该怎么捕捉到这个事件?
恳请大家不吝赐教,谢谢!
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese