The join () method in the Python threading module

Source: Internet
Author: User
This article mainly introduced the Python threading module in the Join () method in-depth understanding, this article summarizes the join () method in the concise and understandable language understanding, differs from other articles, the need friend can refer to the next

See the Oschina on the two code, benefited. where the Join () method does not understand, see the Python official website document introduction:
Join ([timeout]): Wait until the process is finished. This will block the thread that is being called until the thread that is called the Join () method ends. (It is difficult to translate, it should be the meaning)

Haha, this is easy to understand.
Join method, if a thread or a function calls another thread during execution, and then executes after it is complete, the calling thread's join method can be used when invoking the thread.

Copy the Code code as follows:

#-*-encoding:gb2312-*-import String, threading, Time Def Thread_main (a): Global count, Mutex # get thread name Threadnam  E = Threading.currentthread (). GetName () for X in xrange (0, Int (a)): # Get Lock Mutex.acquire () Count = Count + 1 # Release lock Mutex.release () print ThreadName, X, Count Time.sleep (1) def main (num): GL Obal count, Mutex threads = [] Count = 1 # Creates a lock mutex = threading. Lock () # first creates a thread object for x in xrange (0, num): Threads.append (Threading. Thread (Target=thread_main, args= (10,))) # starts all threads for T in Threads:t.start () # Waiting for all child threads in the main thread to exit for T in T Hreads:t.join () if __name__ = = ' __main__ ': num = 4 # Create 4 threads Main (4) #################################### ################################-*-encoding:gb2312-*-import Threadingimport Time Class Test (threading. Thread): Def __init__ (self, num): Threading.    Thread.__init__ (self) self._run_num = num def run (self):    Global count, Mutex threadname = Threading.currentthread (). GetName () for X in xrange (0, Int (self._run_n UM): Mutex.acquire () Count = count + 1 mutex.release () print threadname, X, C    Ount Time.sleep (1) if __name__ = = ' __main__ ': Global count, Mutex threads = [] num = 4 Count = 1 # Create Lock Mutex = Threading. Lock () # Create thread object for x in xrange (0, num): Threads.append (Test (10)) # Start thread for T in Threads:t.sta RT () # Waits for a child thread to end for T in Threads:t.join ()

In the program, the invocation of the last Join () method is clear, which is the join () method where the main process calls the child thread. When all four threads are executed, the main thread executes the following code, where it exits.
correspond to another method found on the Web:
3. Daemon Process

Setdaemon ()

This method is basically the opposite of join. When we are running the program, executing a main thread, if the main thread creates a sub-thread, the main thread and the child thread are suited two ways, running separately, then when the main thread finishes trying to exit, it verifies that the child thread is complete. If the child thread is not completed, the main thread waits for the child thread to complete before exiting. But sometimes we need to, as long as the main thread is complete, regardless of whether the child thread is completed, and the main thread to exit, then you can use the Setdaemon method

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.