This article mainly introduces the usage of the join function of the threading module in Python, and analyzes in detail the functions and usage of the join function in the form of instances, for more information about how to use the join function of the threading module in Python, see the example in this article. Share it with you for your reference. The specific analysis is as follows:
The role of join is well known, blocking the process until the thread execution is complete. A common practice is to start a batch of threads and end the join operation. For example:
For I in range (10): t = ThreadTest (I) thread_arr.append (t) for I in range (10): thread_arr [I]. start () for I in range (10): thread_arr [I]. join ()
Here, the principle of join is to check whether the threads in the thread pool are terminated in sequence. If the thread is terminated, it is blocked until the thread ends. If the thread ends, the join function of the next thread is executed.
Another special feature of the py join function is that timeout can be set, as shown below:
Thread. join ([timeout])
Wait until the thread terminates. This blocks the calling thread until the thread whose join () method is called terminates-either normally or through an unhandled exception-or until the optional timeout occurs.
That is, timeout is set by passing a parameter to join, that is, the join process is not blocked if the specified time is exceeded. In the actual application test, it is found that not all threads end in the timeout period, but the sequential execution test is performed to check whether time_out times out. For example, the timeout value is set to 2 s. If the previous thread is not completed, the subsequent thread executes the join operation and then sets the timeout value of 2 s from the end time of the previous thread.
I hope this article will help you with Python programming.