The JOIN of method in the Python THREADING module is well understood

  • 2020-04-02 14:35:33
  • OfStack

Read oschina on the two code, benefit. Where you don't understand the join() method, please refer to the introduction of python official website:
Join ([timeout]) : wait until the process ends. This blocks the calling thread until the thread on which the join() method is called ends. It is difficult to translate.

Haha, this is easy to understand.
Join method. If a thread or a function calls another thread during execution and does not execute again until it is complete, then the called thread's join method can be used when the thread is called.


#-*- encoding: gb2312 -*-
import string, threading, time
 
def thread_main(a):
    global count, mutex
    # Get the thread name
    threadname = threading.currentThread().getName()
 
    for x in xrange(0, int(a)):
        # In the lock
        mutex.acquire()
        count = count + 1
        # Release the lock
        mutex.release()
        print threadname, x, count
        time.sleep(1)
 
def main(num):
    global count, mutex
    threads = []
 
    count = 1
    # Create a lock
    mutex = threading.Lock()
    # Create the thread object first
    for x in xrange(0, num):
        threads.append(threading.Thread(target=thread_main, args=(10,)))
    # Start all threads
    for t in threads:
        t.start()
    # Waits for all child threads in the main thread to exit
    for t in threads:
        t.join() 
 
if __name__ == '__main__':
    num = 4
    # create 4 A thread
    main(4)
###################################################################
#-*- encoding: gb2312 -*-
import threading
import 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_num)):
            mutex.acquire()
            count = count + 1
            mutex.release()
            print threadname, x, count
            time.sleep(1)
 
if __name__ == '__main__':
    global count, mutex
    threads = []
    num = 4
    count = 1
    # Create a lock
    mutex = threading.Lock()
    # Create a thread object
    for x in xrange(0, num):
        threads.append(Test(10))
    # Starting a thread
    for t in threads:
        t.start()
    # Wait for the child thread to finish
    for t in threads:
        t.join()

In a program, the last call to the join() method is the main process calling the join() method of the child threads one by one. When all four threads are finished, the main thread executes the following code, which exits.
Another method found together on the Internet:
3. Daemons

SetDaemon ()

This method is basically the opposite of join. When we are in the program running, execute a main thread, if the main thread and create a child thread, the main thread and the child thread on the two separate, run separately, so when the main thread finish want to exit, will check whether the child thread finish. If the child thread does not complete, the main thread waits for the child to complete before exiting. But sometimes what we need is for the main thread to exit with the main thread as soon as it completes, whether or not it completes, and then we can use the setDaemon method


Related articles: