Tutorial on using the join method in Python thread

  • 2020-05-07 19:58:42
  • OfStack

join method: blocks a thread until it completes execution

So  , you can add a timeout operation to join, join([timeout]), and when the set time is exceeded, the thread is no longer blocked

Another consequence of jion plus is that the child thread is bound to the main thread at 1 and does not start executing the child thread until the child thread has finished running.


The code has join:

View the code slice on CODE derived to my code slice


  #-*- coding: UTF-8 -*-  
   
   
  import threading 
  from time import sleep 
   
  def fun(): 

View the code slice on CODE to derive my code slice


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

View the code slice on CODE that is derived to my code slice


  <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) 

View the code slice on CODE that is derived to my code slice

      output: < pre name="code" class="python" > 111111 input after the output < span style="font-family: Arial, Helvetica, sans-serif;" > aaaaaaa < /span >  

View the code slice on CODE to derive my code slice

         

Code: no join

View the code slice on CODE to derive my code slice


  #-*- 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) 

View the code slice on CODE that is derived to my code slice

      < pre name="code" class="python" style="font-size:18px;" > 111111 and < span style="font-family: Arial, Helvetica, sans-serif;" > aaaaaaa   interval output < /span >  


Related articles: