python程序退出不报错_China

小弟是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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值