Home  >  Article  >  Backend Development  >  JOIN() method in Python THREADING module

JOIN() method in Python THREADING module

不言
不言Original
2018-04-17 15:29:321981browse

This article mainly introduces an in-depth understanding of the JOIN() method in the Python THREADING module. This article summarizes the understanding of the JOIN() method in concise and easy-to-understand language. Different from other articles, friends in need can refer to it.

I have benefited a lot from reading the two codes on oschina. Among them, I don’t understand the join() method. See the introduction of the Python official website document:
join([timeout]): Wait until the process ends. This will block the calling thread until the thread on which the join() method is called ends. (It’s so difficult to translate, this is what it should mean)

Haha, this is easy to understand.
Join method, if a thread or a function needs to call another thread during execution, and cannot continue execution until it is completed, then the join method of the called thread can be used when calling this thread.

Copy code The code is as follows:

#-*- encoding: gb2312 -*-
import string, threading, time
 
def thread_main(a):
    global count, mutex
    # 获得线程名
    threadname = threading.currentThread().getName()
 
    for x in xrange(0, int(a)):
        # 取得锁
        mutex.acquire()
        count = count + 1
        # 释放锁
        mutex.release()
        print threadname, x, count
        time.sleep(1)
 
def main(num):
    global count, mutex
    threads = []
 
    count = 1
    # 创建一个锁
    mutex = threading.Lock()
    # 先创建线程对象
    for x in xrange(0, num):
        threads.append(threading.Thread(target=thread_main, args=(10,)))
    # 启动所有线程
    for t in threads:
        t.start()
    # 主线程中等待所有子线程退出
    for t in threads:
        t.join()  
 
if __name__ == '__main__':
    num = 4
    # 创建4个线程
    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
    # 创建锁
    mutex = threading.Lock()
    # 创建线程对象
    for x in xrange(0, num):
        threads.append(Test(10))
    # 启动线程
    for t in threads:
        t.start()
    # 等待子线程结束
    for t in threads:
        t.join()

In the program, the last call to the join() method is clear. It is the main process that calls the child threads one by one. join() method. When all four threads have finished executing, the main thread will execute the following code, which means it will exit here.
Correspondingly, another method found on the Internet:
3. Daemon process

setDaemon()

This method is basically the opposite of join. When we execute a main thread while the program is running, if the main thread creates a sub-thread, the main thread and the sub-thread will be divided into two and run separately. Then when the main thread completes and wants to exit, it will check whether the sub-thread is completed. If the child thread is not completed, the main thread will wait for the child thread to complete before exiting. But sometimes what we need is that as long as the main thread is completed, no matter whether the child thread is completed or not, it must exit together with the main thread. In this case, we can use the setDaemon method

The above is the detailed content of JOIN() method in Python THREADING module. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn