今天在学线程,结果第一个程序就杯具了,报了这个错
Unhandled exception in thread started by sys.excepthook is missing lost sys.stderr
发现原来是主进程启了两个线程后居然自己退出了(靠)
# -*- coding: utf-8 -*- import time import thread def run(name,start): while 1: if start > 20: break start += 1 print name+' num='+str(start) time.sleep(2) thread.start_new_thread(run,('thread1',2)) thread.start_new_thread(run,('thread2',1))
看来这样写是不行的哈
如果只是解决这个问题倒很简单,不让主进程退出就可以了
# -*- coding: utf-8 -*- import time import thread def run(name,start): while 1: if start > 20: break start += 1 print name+' num='+str(start) time.sleep(2) thread.start_new_thread(run,('thread1',2)) thread.start_new_thread(run,('thread2',1)) time.sleep(200)