Maison > développement back-end > Tutoriel Python > Python线程中对join方法的运用的教程

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Libérer: 2016-06-06 11:25:01
original
1486 Les gens l'ont consulté

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

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

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


代码 有join:

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

1

2

3

4

5

6

7

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

  

  

import threading

from time import sleep

  

def fun():

Copier après la connexion

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

1

2

3

4

<span style="white-space:pre">  </span>i= 5

  while i > 0:

    print(111111)

    sleep(10)

Copier après la connexion

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

1

2

3

4

5

6

7

8

9

10

11

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

Copier après la connexion

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

输出:

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

Copier après la connexion

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

   

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

    
Copier après la connexion
Étiquettes associées:
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal