Example analysis of Join function usage of threading module in Python

Source: Internet
Author: User
Tags join terminates in python

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.

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.