python 中关于多线程的问题
# coding=gbk
import time
import thread
def timer(no,interval): #自己写的线程函数
while True:
print 'Thread :(%d) Time:%s'%(no,time.ctime())
time.sleep(0)
def test():#使用thread.start_new_thread()来产生2个新的线程
thread.start_new_thread(timer,(2,3))
thread.start_new_thread(timer,(1,1))
if __name__=='__main__':
test()
为什么我的输出了一行信息就结束了?
输出的信息是:Thread :(2) Time:Mon Feb 18 14:02:32 2008
[解决办法]
你的主线程退出了,程序就推出了,从线程还没执行呢。
def test(): thread.start_new_thread(timer,(2,3)) thread.start_new_thread(timer,(1,1)) time.sleep(1)#加上试试