Example Analysis of the join function usage of the threading module in Python, pythonthreading

Source: Internet
Author: User

Example Analysis of the join function usage of the threading module in Python, pythonthreading

This example describes how to use the join function of the threading module in Python. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.