1 python default parameters after the thread is created, regardless of whether the main thread finishes executing, it waits for the child thread to complete before exiting, with or without a join result
Examples are as follows:
import threadingimport timedef say(name): print(‘%s is start ‘ % name) time.sleep(3) print(‘%s is stop‘%name)print(‘___主线程开始___‘,time.time())t = threading.Thread(target=say,args=(‘eve‘,))t.start()t.join()print(‘___主线程结束___‘,time.time())
import threadingimport timedef say(name): print(‘%s is start ‘ % name) time.sleep(3) print(‘%s is stop‘%name)print(‘___主线程开始___‘,time.time())t = threading.Thread(target=say,args=(‘eve‘,))t.start()#t.join()print(‘___主线程结束___‘,time.time())
2 If a thread is created and daemon is set to true, that is, Thread.setdaemon (true), the main thread exits automatically after execution and does not wait for the result of the child thread to execute. And as the main thread exits, the child thread also dies.
Examples are as follows:
import threadingimport timedef say(name): print(‘%s is start ‘ % name) time.sleep(3) print(‘%s is stop‘%name)print(‘___主线程开始___‘)t = threading.Thread(target=say,args=(‘eve‘,))t.setDaemon(True)t.start()# t.join()print(‘___主线程结束___‘)
3 The Join method is blocking, waiting for the child thread to end, and the join method has a parameter of timeout, that is, if the main thread waits for timeout and the child thread has not ended, the main thread forces the end of the child thread.
Examples are as follows:
①. The main thread blocks until those time is less than the child thread run time, the main thread ends, regardless of whether the child thread ends or not.
import threadingimport timedef say(name): print(‘%s is start ‘ % name) time.sleep(3) print(‘%s is stop‘%name)print(‘___主线程开始___‘)t = threading.Thread(target=say,args=(‘eve‘,))t.setDaemon(True)t.start()t.join(2)print(‘___主线程结束___‘)
②. The main thread blocks until those time is greater than the child thread run time, the main thread waits for the child thread to end.
import threadingimport timedef say(name): print(‘%s is start ‘ % name) time.sleep(3) print(‘%s is stop‘%name)print(‘___主线程开始___‘)t = threading.Thread(target=say,args=(‘eve‘,))t.setDaemon(True)t.start()t.join(4)print(‘___主线程结束___‘)
4 The timeout parameter in join is invalid if the thread daemon property is false. The main thread waits for the child thread to end.
import threadingimport timedef say(name): print(‘%s is start ‘ % name) time.sleep(3) print(‘%s is stop‘%name)print(‘___主线程开始___‘)t = threading.Thread(target=say,args=(‘eve‘,))t.setDaemon(False)t.start()# t.join(4)print(‘___主线程结束___‘)
5 If the thread daemon property is true, the timeout parameter in the join is valid, and the main thread waits for timeout time to end the child thread. There is a pit here, that is, if you have N child thread join (timeout) at the same time, the main thread will actually wait for the maximum timeout of n * timeout, because each child thread's time-out starts at the end of the last child line blocks until those.
Examples are as follows:
import threadingimport timedef say(name): print(‘%s is start ‘ % name) time.sleep(3) print(‘%s is stop‘%name)print(‘___主线程开始___‘)t = threading.Thread(target=say,args=(‘eve‘,))t.setDaemon(True)t.start()t.join(4)print(‘___主线程结束___‘)
The role of joins in Python's multi-threading