python3 常用线程
# -*- coding: utf-8 -*- import time from threading import Thread def test(i): while True: print("i",i+1) time.sleep(1) i+=1 if i == 5: break print(1) t = Thread(target=test, args=(0,))
#t.setDaemon(True)
t.start()
print("---",2)
主线程一直运行,遇到循环耗时操作分出子线程,主线程运行到最后等待子线程结束,再进行关闭
python2.7 thread方法
# -*- coding: utf-8 -*- import time # from threading import Thread import thread def test(i): while True: print "i",i+1 time.sleep(1) i+=1 if i == 5: break print 1 thread.start_new_thread(test, (0,)) # t = Thread(target=test, args=(0,)) # t.start() print "---",2
主线程运行到最后就结束,相当于python3中设置了守护进行,如上注释部分所示