The role of join method in Python thread


join method: block the thread until the thread finishes executing

Therefore, you can add a timeout operation to join, join ([timeout]), beyond the set time, no longer block the thread

Another consequence of jion plus is that the child thread and the main thread are bound together, and the child thread is not executed until the child thread is finished running.


The code has join:

#-*- coding: UTF-8 -*- 


import threading
from time import sleep

def fun():
<span style="white-space:pre">	</span>i= 5
	while i > 0:
		print(111111)
		sleep(10)
<span style="white-space:pre">		</span>i--

if __name__ == '__main__':


	a = threading.Thread(target = fun)
	a.start()
	a.join()
	while True:
		print('aaaaaaa')
		sleep(1)
输出:<pre name="code" class="python">111111 输完之后, 才输出 <span style="font-family: Arial, Helvetica, sans-serif;">aaaaaaa </span>
 
 

Code: no join

#-*- coding: UTF-8 -*- 


import threading
from time import sleep

def fun():
	while True:
		print(111111)
		sleep(10)

if __name__ == '__main__':


	a = threading.Thread(target = fun)
	a.start()
	while True:
		print('aaaaaaa')
		sleep(1)
<pre name="code" class="python" style="font-size:18px;">111111 和 <span style="font-family: Arial, Helvetica, sans-serif;">aaaaaaa  间隔输出</span>

 
 


Published 190 original articles · 19 praises · 200,000+ views

Guess you like

Origin blog.csdn.net/zengchenacmer/article/details/44243805