Home  >  Article  >  Backend Development  >  Python线程中对join方法的运用的教程

Python线程中对join方法的运用的教程

WBOY
WBOYOriginal
2016-06-06 11:25:011381browse

join 方法:阻塞线程 , 直到该线程执行完毕

因此  ,可以对join加一个超时操作 , join([timeout]),超过设置时间,就不再阻塞线程

jion加上还有一个后果就是, 子线程和主线程绑定在一起 , 直到子线程运行完毕,才开始执行子线程。


代码 有join:

在CODE上查看代码片派生到我的代码片

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

在CODE上查看代码片派生到我的代码片

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

在CODE上查看代码片派生到我的代码片

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

在CODE上查看代码片派生到我的代码片

    输出:

111111 输完之后, 才输出 <span style="font-family: Arial, Helvetica, sans-serif;">aaaaaaa </span> 
<p>在CODE上查看代码片派生到我的代码片</p>
<p>     </p>
<p>代码: 无join</p>
<p>在CODE上查看代码片派生到我的代码片</p>
<p>
<pre class="brush:py;">
  #-*- 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) 

在CODE上查看代码片派生到我的代码片

   

111111 和 <span style="font-family: Arial, Helvetica, sans-serif;">aaaaaaa  间隔输出</span>  <br>

    
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn