最简单的状况是:html
import threading import time def fuction(i): print("我是第%s个进程,开始时间是%s"%(i+1,time.ctime())) time.sleep(2) #子程序等2秒。。 print("我是第%s个进程,结束时间是%s"%(i+1,time.ctime())) threads=[threading.Thread(target=fuction,args=(i,) ) for i in range(5)] for t in threads: t.start() print("程序结束,%s:"%(time.ctime())) #输出为------------------- 我是第1个进程,开始时间是Sun Oct 22 17:45:37 2017 我是第2个进程,开始时间是Sun Oct 22 17:45:37 2017 我是第3个进程,开始时间是Sun Oct 22 17:45:37 2017 我是第4个进程,开始时间是Sun Oct 22 17:45:37 2017 我是第5个进程,开始时间是Sun Oct 22 17:45:37 2017 **程序结束,Sun Oct 22 17:45:37 2017:** #主线程继续执行,不等待,也不杀死子线程 我是第1个进程,结束时间是Sun Oct 22 17:45:39 2017 我是第2个进程,结束时间是Sun Oct 22 17:45:39 2017 我是第5个进程,结束时间是Sun Oct 22 17:45:39 2017 我是第3个进程,结束时间是Sun Oct 22 17:45:39 2017 我是第4个进程,结束时间是Sun Oct 22 17:45:39 2017
这里介绍另外两个函数:setDaemon()、join()
join:如在一个线程B中调用threada.join(),则threada结束后,线程B才会接着threada.join()日后运行。
setDaemon:主线程A启动了子线程B,调用b.setDaemaon(True),则主线程结束时,会把子线程B也杀死,与C/C++中得默认效果是同样的。python
好比我想让主程序等待子程序完成以后再运行,能够是用t.join()
其中t是指某个子进程,如t1,t2....而后join()能够有一个参数,主进程等待多少秒,如t1.join(2)指在t1子进程开始后,主进程等待2秒就继续执行。多线程
def fuction(i): print("我是第%s个进程,开始时间是%s"%(i+1,time.ctime())) time.sleep(i*2) #模拟子进程的运行时间,ID越大时间越长 print("我是第%s个进程,结束时间是%s"%(i+1,time.ctime())) threads=[threading.Thread(target=fuction,args=(i,) ) for i in range(5)] for t in threads: t.start() threads[1].join(1) #主进程在第二个子进程开始后阻塞1秒再运行 print("主进程:,%s:"%(time.ctime())) #输出--------------------- 我是第1个进程,开始时间是Sun Oct 22 18:12:14 2017 我是第1个进程,结束时间是Sun Oct 22 18:12:14 2017 我是第2个进程,开始时间是Sun Oct 22 18:12:14 2017 #第二个子进程14秒开始 我是第3个进程,开始时间是Sun Oct 22 18:12:14 2017 我是第4个进程,开始时间是Sun Oct 22 18:12:14 2017 我是第5个进程,开始时间是Sun Oct 22 18:12:14 2017 主进程:Sun Oct 22 18:12:15 2017: #主进程在阻塞一秒后的15秒开始启动运行 我是第2个进程,结束时间是Sun Oct 22 18:12:16 2017 我是第3个进程,结束时间是Sun Oct 22 18:12:18 2017 我是第4个进程,结束时间是Sun Oct 22 18:12:20 2017 我是第5个进程,结束时间是Sun Oct 22 18:12:22 2017
若想等全部的子进程都完成后,主进程在进行能够,简单状况能够用最慢的一个子进程来对主进程进行阻塞,这里最慢的是第5个子进程。能够:threads[4].join()。dom
import tensorflow as tf import numpy as np import random import threading import time def fuction(i): print("我是第%s个进程,开始时间是%s"%(i+1,time.ctime())) time.sleep((i+0.5)*2) #模拟子程序运行随机秒 print("我是第%s个进程,结束时间是%s"%(i+1,time.ctime())) threads=[threading.Thread(target=fuction,args=(i,) ) for i in range(5)] for t in threads: t.start() threads[4].join() print("主进程:%s:"%(time.ctime())) #输出----------------------- 我是第1个进程,开始时间是Sun Oct 22 18:29:28 2017 我是第2个进程,开始时间是Sun Oct 22 18:29:28 2017 我是第3个进程,开始时间是Sun Oct 22 18:29:28 2017 我是第4个进程,开始时间是Sun Oct 22 18:29:28 2017 我是第5个进程,开始时间是Sun Oct 22 18:29:28 2017 我是第1个进程,结束时间是Sun Oct 22 18:29:29 2017 我是第2个进程,结束时间是Sun Oct 22 18:29:31 2017 我是第3个进程,结束时间是Sun Oct 22 18:29:33 2017 我是第4个进程,结束时间是Sun Oct 22 18:29:35 2017 我是第5个进程,结束时间是Sun Oct 22 18:29:37 2017 主进程:Sun Oct 22 18:29:37 2017: #主进程等到最后一个子进程结束后才运行
另外一种状况是,好比我想让主程序运行完就马上结束,杀死子程序:能够用t.setDaemon(True)函数
def fuction(i): print("我是第%s个进程,开始时间是%s"%(i+1,time.ctime())) time.sleep((i+0.5)*2) #模拟子程序运行随机秒 print("我是第%s个进程,结束时间是%s"%(i+1,time.ctime())) threads=[threading.Thread(target=fuction,args=(i,) ) for i in range(5)] for t in threads: t.setDaemon(True) t.start() print("主进程:%s:"%(time.ctime())) #输出--------------------------------- 我是第1个进程,开始时间是Sun Oct 22 19:04:06 2017 我是第2个进程,开始时间是Sun Oct 22 19:04:06 2017 我是第3个进程,开始时间是Sun Oct 22 19:04:06 2017 我是第4个进程,开始时间是Sun Oct 22 19:04:06 2017 我是第5个进程,开始时间是Sun Oct 22 19:04:06 2017 主进程:Sun Oct 22 19:04:06 2017: #能够看到主线程完成后,后面就没有子线程了。
一种特殊状况请注意:当并不是像上面每一个子进程同样都设置t.setDaemon(True)(有的设置有的没有。。)
如:学习
threads[2].setDaemon(True) #只设置了第3个和第5个,1,2,4没有设置这只会kill掉第5个子线程 #我的猜想,当程序运行完主线程后则会检查剩余的子线程,将最后面的 threads[4].setDaemon(True) #且是setDaemon(True) 子进程删掉。第3个没有kill掉是由于4线程还在运行 for t in threads: #而且4是默认状态不能被kill的。这时主进程依然在等待4完成而不会 t.start() #杀掉剩余进程,当4完成了,就不等待了直接杀掉剩余进程
咱们能够这样验证,在线程4结束前看看线程5是否存活:线程
import tensorflow as tf import numpy as np import random import threading import time def fuction(i): print("我是第%s个进程,开始时间是%s"%(i+1,time.ctime())) time.sleep((i+0.5)*2) #模拟子程序运行随机秒 print("我是第%s个进程,结束时间是%s"%(i+1,time.ctime())) if i ==3: print("我是第4个进程:结束前进程5的状态是:" ,threads[4].is_alive()) #加这一句判断现成物 #的存活状况 threads=[threading.Thread(target=fuction,args=(i,) ) for i in range(5)] #threads[0].setDaemon(True) #threads[1].setDaemon(True) threads[2].setDaemon(True) #threads[3].setDaemon(True) threads[4].setDaemon(True) for t in threads: t.start() print("主进程:%s:"%(time.ctime())) #输出---------------------------------- 我是第1个进程,开始时间是Sun Oct 22 19:22:36 2017 我是第2个进程,开始时间是Sun Oct 22 19:22:36 2017 我是第3个进程,开始时间是Sun Oct 22 19:22:36 2017 我是第4个进程,开始时间是Sun Oct 22 19:22:36 2017 我是第5个进程,开始时间是Sun Oct 22 19:22:36 2017 主进程:Sun Oct 22 19:22:36 2017: 我是第1个进程,结束时间是Sun Oct 22 19:22:37 2017 我是第2个进程,结束时间是Sun Oct 22 19:22:39 2017 我是第3个进程,结束时间是Sun Oct 22 19:22:41 2017 我是第4个进程,结束时间是Sun Oct 22 19:22:43 2017 我是第4个进程:结束前进程5的状态是: True #能够看到,此时线程5仍是存活的,但4结束后5就消失了。
最后一种应用时,咱们但愿主程序等待某几个子线程先执行完后在运行,而且杀死剩余没有完成的程序,能够用setDaemon()、join() 组合的方式:
如:完整运行完第1,2,3线程,以后运行主线程,主线程完成后杀死剩余的子线程:code
def fuction(i): print("我是第%s个进程,开始时间是%s"%(i+1,time.ctime())) time.sleep((i+0.5)*2) #模拟子程序运行随机秒 print("我是第%s个进程,结束时间是%s"%(i+1,time.ctime())) if i ==3: print("我是第4个进程:结束前进程5的状态是:" ,threads[4].is_alive()) threads=[threading.Thread(target=fuction,args=(i,) ) for i in range(5)] #threads[0].setDaemon(True) #threads[1].setDaemon(True) #threads[2].setDaemon(True) threads[3].setDaemon(True) #设置第4和5子线程为能够kill的线程 threads[4].setDaemon(True) for t in threads: t.start() threads[0].join() #设置第1,2,3线程阻塞主线程的子线程 threads[1].join() threads[2].join() print("主进程:%s:"%(time.ctime())) #输出-------------------------- 我是第1个进程,开始时间是Sun Oct 22 19:32:24 2017 我是第2个进程,开始时间是Sun Oct 22 19:32:24 2017 我是第3个进程,开始时间是Sun Oct 22 19:32:24 2017 我是第4个进程,开始时间是Sun Oct 22 19:32:24 2017 我是第5个进程,开始时间是Sun Oct 22 19:32:24 2017 我是第1个进程,结束时间是Sun Oct 22 19:32:25 2017 我是第2个进程,结束时间是Sun Oct 22 19:32:27 2017 我是第3个进程,结束时间是Sun Oct 22 19:32:29 2017 主进程:Sun Oct 22 19:32:29 2017: #咱们能够看到只完成了第1,2,3线程,而后最后是主线程
这篇文章就到这里,python中子线程的暂停,阻塞,关闭,和唤醒,本人都不是很明确,若有好方法但愿你们一块儿交流。我先写在这里。对于tensorflow中的读取数据线程控制,等我整理在下一篇博客。htm