método join: bloquea el hilo hasta que el hilo termine de ejecutarse
Por lo tanto, puede agregar una operación de tiempo de espera para unirse, unirse ([tiempo de espera]), más allá del tiempo establecido, ya no bloquea el hilo
Otra consecuencia de jion plus es que el subproceso secundario y el subproceso principal están unidos, y el subproceso secundario no se ejecuta hasta que el subproceso secundario haya terminado de ejecutarse.
El código tiene unirse:
#-*- 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>
Código: no unirse
#-*- 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>