解决pycharm运行多线程程序报错"Unhandled exception in thread started by sys.excepthook is missing"
来源:互联网 发布:成都网上选号软件 编辑:程序博客网 时间:2024/06/04 23:33
运行报错实例代码:
import timeimport threaddef timer(no, interval): cnt = 0 while cnt < 3: print 'Thread:(%d) Time:%s\n' % (no, time.ctime()) time.sleep(interval) cnt += 1 thread.exit_thread()def test(): # Use thread.start_new_thread() to create 2 new threads thread.start_new_thread(timer, (1, 1)) thread.start_new_thread(timer, (2, 2))if __name__ == '__main__': test()
使用pyCharm运行后报错:
Unhandled exception in thread started by
sys.excepthook is missing
lost sys.stderr
问题原因:启动线程之后,须确保主线程等待所有子线程返回结果后再退出,如果主线程比子线程早结束,无论其子线程是否是后台线程,都将会中断,抛出这个异常 。
问题解决方法:
在test()后增加sleep,保证子线程结束后主线程才结束。
解决问题后代码:
import timeimport threaddef timer(no, interval): cnt = 0 while cnt < 3: print 'Thread:(%d) Time:%s\n' % (no, time.ctime()) time.sleep(interval) cnt += 1 thread.exit_thread()def test(): # Use thread.start_new_thread() to create 2 new threads thread.start_new_thread(timer, (1, 1)) thread.start_new_thread(timer, (2, 2))if __name__ == '__main__': test() time.sleep(30)
运行结果:
0 0
- 解决pycharm运行多线程程序报错"Unhandled exception in thread started by sys.excepthook is missing"
- Python报错: Unhandled exception in thread started by Error in sys.excepthook
- python错误Unhandled exception in thread started by Error in sys.excepthook
- windows下安装pywin32报错:close failed in file object destructor:sys.excepthook is missing lost sys.stderr
- Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x000001DB1D7557
- 尝试将Python程序用管道命令输出到cd时报错 - sys.excepthook is missing lost sys.stderr
- pycharm +scrapy 运行报错:[twisted] CRITICAL: Unhandled error in Deferred
- IDEA运行spark程序报错Exception in thread "main" java.net.BindException: Cannot
- Eclipse 运行弹出A Java Exception has occurred.并报错Exception in thread
- 运行包含package语句的HelloWord程序报Exception in thread "main" java.lang.NoClassDefFoundError
- Linux 下运行Java程序报“Exception in thread "main" java.lang.NoClassDefFoundError”
- 多线程报错 : Exception in thread "Thread-3" java.util.ConcurrentModificationException 并发修改异常
- nhandled exception in thread started by <function wrapper at 0x035BA3F0>
- 运行tomcat报Exception in thread "ContainerBackgroundProcessor[StandardEngine[Catalina]]"
- Eclipse右键点击run as Java Application .运行报错 Exception in thread "main"
- myeclipse运行tomcat报错Exception in thread "main" java.lang.OutOfMemoryError: PermGen space
- JAVA在ubuntu中运行报错---Exception in thread "main"
- 一个环境变量导致运行报错Exception in thread "main" java.lang.NoClassDefFoundError:
- Webstorm 2016.2 破解
- WIN32程序中线程的创建和使用
- POJ2632-Crashing Robots(模拟)
- Linux Shell 文本处理工具集锦
- lanmp_v3.1(wdcp面板)安装、卸载及问题处理
- 解决pycharm运行多线程程序报错"Unhandled exception in thread started by sys.excepthook is missing"
- 使用 Apache CXF 实现 Web Service 详解
- OrmLite框架 —— OrmLite 入门使用(一)
- fopen与Open区别
- Trie树
- SSH免密码登录
- 【精华】构建闪存系统和生态环境
- 数据库分库分表(sharding)系列(四) 多数据源的事务处理
- ZooKeeper注册中心