python 线程join
Python Thread.join()方法 (Python Thread.join() Method)
Thread.join() method is an inbuilt method of the Thread class of the threading module in Python. Whenever this method is called for any Thread object, it blocks the calling thread till the time the thread whose join() method is called terminates, either normally or through an unhandled exception.
Thread.join()方法是Python中线程模块的Thread类的内置方法。 每当为任何Thread对象调用此方法时,它都会阻塞调用线程,直到调用join()方法的线程正常终止或通过未处理的异常终止为止。
Module:
模块:
from threading import Thread
Syntax:
句法:
join(timeout=None)
Parameter(s):
参数:
timeout: It is an optional parameter, which specifies a timeout for the operation in seconds. it should be a floating-point number. When a timeout argument is missing, the operation will block until the thread terminates.
timeout :这是一个可选参数,以秒为单位指定操作的超时时间。 它应该是一个浮点数。 当缺少超时参数时,该操作将阻塞直到线程终止。
Return value:
返回值:
The return type of this method is <class 'NoneType'>, it returns nothing.
此方法的返回类型为<class'NoneType'> ,它什么也不返回。
Example:
例:
# Python program to explain the
# use of join() method in Thread class
import time
import threading
def thread_1(i):
time.sleep(2)
print('Value by Thread 1:', i)
def thread_2(i):
time.sleep(5)
print('Value by Thread 2:', i)
def thread_3(i):
print('Value by Thread 3:', i)
# Creating three sample threads
thread1 = threading.Thread(target=thread_1, args=(1,))
thread2 = threading.Thread(target=thread_2, args=(2,))
thread3 = threading.Thread(target=thread_3, args=(3,))
# Running three thread object
thread1.start()
thread1.join()
thread2.start()
thread2.join()
thread3.start()
thread3.join()
print()
# Creating another 3 threads
thread4 = threading.Thread(target=thread_1, args=(1,))
thread5 = threading.Thread(target=thread_2, args=(2,))
thread6 = threading.Thread(target=thread_3, args=(3,))
thread4.start()
thread5.start()
thread6.start()
thread4.join()
thread5.join()
thread6.join()
Output
输出量
Value by Thread 1: 1
Value by Thread 2: 2
Value by Thread 3: 3
Value by Thread 3: 3
Value by Thread 1: 1
Value by Thread 2: 2
In the initial three Thread objects, we first created a thread and waited for it to execute and then joined to the main thread. So they are printed in the same order as called.
在最初的三个Thread对象中,我们首先创建一个线程,等待它执行,然后加入主线程。 因此它们以与调用相同的顺序打印。
In the next three Thread objects, they are running simultaneously, so they get printed according to the time they should be executed,
在接下来的三个Thread对象中,它们同时运行,因此根据执行时间将它们打印出来,
time(thread3)<time(thread2)<time(thread1)
Example:
例:
# Python program to explain the
# use of join() method in Thread class
# with timeout parameter defined
import time
import threading
def thread_1(i):
time.sleep(2)
print('Value by Thread 1:', i)
def thread_2(i):
time.sleep(5)
print('Value by Thread 2:', i)
# Creating three sample threads
thread1 = threading.Thread(target=thread_1, args=(1,))
thread2 = threading.Thread(target=thread_2, args=(2,))
# Running three thread object
thread1.start()
thread1.join(timeout=5)
thread2.start()
thread2.join()
Output
输出量
Value by Thread 1: 1
Value by Thread 2: 2
翻译自: https://www.includehelp.com/python/thread-join-method-with-example.aspx
python 线程join