This example describes the use of the Threading module join function in Python. Share to everyone for your reference. The specific analysis is as follows:
Join is known to block the process until the thread is finished executing. It is common practice for us to start a batch of threads and finally join these threads to end, for example:
?
1 2 3 4 5 6 7 8 9 |
For I in Range (a): T = threadtest (i) thread_arr.append (t) for I in Range (Ten): Thread_arr[i].start () for I in range (10 ): Thread_arr[i].join () |
The join principle here is to verify that the thread in the thread pool ends, blocks until the thread ends without ending, and jumps to execute the join function of the next thread if it ends.
A special feature of the Join function for PY is that it can be set to timeout, as follows:
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 U Nhandled Exception–or until the optional timeout occurs.
That is, the timeout is set by passing a parameter to the join, which means that the join is not blocking the process over the specified time. In the actual application test found that not all of the threads in the timeout period ended, but in order to perform the check whether in Time_out time timeout, for example, the timeout is set to 2s, the previous thread in the absence of completion of the case, Subsequent thread execution join will set a timeout of 2s from the last thread end time.
I hope this article will help you with your Python programming.