Join function of python process

Python multi-process join function

Join makes the main process wait for the end of the child process, and then execute the main process.
For example:

from multiprocessing import Process
import time

def task(name):
    print(f"{name} is running")
    time.sleep(2)
    print(f"{name} is gone")

if __name__ == "__main__":
    p = Process(target=task,args=("常辛",))  # 创建一个进程对象
    p.start()
    # p.join()
    print(111)
    print("主开始")
    
111
主开始
常辛 is running
常辛 is gone

from multiprocessing import Process
import time

def task(name):
    print(f"{name} is running")
    time.sleep(2)
    print(f"{name} is gone")

if __name__ == "__main__":
    p = Process(target=task,args=("常辛",))  # 创建一个进程对象
    p.start()
    p.join()
    print(111)
    print("主开始")
    
常辛 is running
常辛 is gone
111
主开始

Generally speaking, if there is a single print sentence in the main program, the print statement will be executed first (if there are enough statements to be executed, the execution of the child process can be seen). Let me give you an example:

from multiprocessing import Process
import time

def task(name):
    print(f"{name} is running")
    time.sleep(2)
    print(f"{name} is gone")

if __name__ == "__main__":
    p = Process(target=task,args=("常辛",))  # 创建一个进程对象
    p.start()
    # p.join()
    print(111)
    time.sleep(0.5)
    print("主开始")

111
常辛 is running
主开始
常辛 is gone

If there are multiple join functions in the program, only the first join will work. For example, the following is an example (multiple child processes use the join function):

from multiprocessing import Process
import time

def task(name,sec):
    print(f"{name} is running")
    time.sleep(sec)
    print(f"{name} is gone")

if __name__ == "__main__":
    start_time = time.time()
    p = Process(target=task,args=("常",3))  # 创建一个进程对象
    p1 = Process(target=task,args=("辛",2))  # 创建一个进程对象
    p2 = Process(target=task,args=("王",5))  # 创建一个进程对象

    p.start()
    p1.start()
    p2.start()

    # join只针对主进程,如果join下面多次join他是不阻塞的
    # p.join()
    # p1.join()
    # p2.join()

    print(f"主{time.time()-start_time}")  # 0.02这只是主进程结束的时间0.019910097122192383is running
辛 is running
王 is running
辛 is gone
常 is gone
王 is gone

from multiprocessing import Process
import time

def task(name,sec):
    print(f"{name} is running")
    time.sleep(sec)
    print(f"{name} is gone")

if __name__ == "__main__":
    start_time = time.time()
    p = Process(target=task,args=("常",3))  # 创建一个进程对象
    p1 = Process(target=task,args=("辛",2))  # 创建一个进程对象
    p2 = Process(target=task,args=("王",5))  # 创建一个进程对象

    p.start()
    p1.start()
    p2.start()

    # join只针对主进程,如果join下面多次join他是不阻塞的
    p.join()
    p1.join()
    p2.join()

    print(f"主{time.time()-start_time}")is running
辛 is running
王 is running
辛 is gone
常 is gone
王 is gone
主5.110209226608276

# 注:如果不是连续多个,比如这样:

from multiprocessing import Process
import time

def task(name,sec):
    print(f"{name} is running")
    time.sleep(sec)
    print(f"{name} is gone")

if __name__ == "__main__":
    start_time = time.time()
    p = Process(target=task,args=("常",3))  # 创建一个进程对象
    p1 = Process(target=task,args=("辛",2))  # 创建一个进程对象
    p2 = Process(target=task,args=("王",5))  # 创建一个进程对象

    p.start()
    p.join()
    p1.start()
    p1.join()
    p2.start()
    p2.join()

    print(f"主{time.time()-start_time}")is running
常 is gone
辛 is running
辛 is gone
王 is running
王 is gone
主10.23875904083252
# 这样就会先执行p进程,再执行p1进程,再执行p2进程

If I comment out p1 and p2 here, after p.join() is executed, the result will be executed print(f"main {time.time()-start_time}"):

from multiprocessing import Process
import time

def task(name,sec):
    print(f"{name} is running")
    time.sleep(sec)
    print(f"{name} is gone")

if __name__ == "__main__":
    start_time = time.time()
    p = Process(target=task,args=("常",3))  # 创建一个进程对象
    p1 = Process(target=task,args=("辛",2))  # 创建一个进程对象
    p2 = Process(target=task,args=("王",5))  # 创建一个进程对象

    p.start()
    p1.start()
    p2.start()

    # join只针对主进程,如果join下面多次join他是不阻塞的
    p.join()
    # p1.join()
    # p2.join()

    print(f"主{time.time()-start_time}")is running
辛 is running
王 is running
辛 is gone
常 is gone
主3.0900182723999023is gone

You may have questions. Why did p1 not execute join before printing?
Because when it is executed, the time of time.sleep is shorter than that of p, so it will be executed before p, so when printing is done before p is executed, it will also be printed first.
The summary is: join is blocking, the main process has join, the code below the main process is not executed until the process is executed, and then executed.

Because I took multiple processes as an example, what is the use of multiple processes here?
For example, if you boil water for five minutes, cook rice for ten minutes, and chop vegetables for five minutes, if you do it step by step, it will take 20 minutes, but if you boil the water, cook the rice, and then cut the vegetables with this effort , It only takes ten minutes at most, which saves us time. Let me give you an example:

from multiprocessing import Process
import time

def task(sec):
    print(f"is running")
    time.sleep(sec)
    print(f"is gone")

if __name__ == "__main__":
    start_time = time.time()

    # 错误示范
    for i in range(1,4):
        p = Process(target=task, args=(i,))  # 创建一个进程对象
        p.start()
        p.join()

    # 正确示范
    # l1 = []
    # for i in range(1, 4):
    #     p = Process(target=task, args=(i,))  # 创建一个进程对象
    #     l1.append(p)
    #     p.start()
    # for i in l1:
    #     i.join()

    print(f"主{time.time()-start_time}")

is running
is gone
is running
is gone
is running
is gone
主6.3900392055511475
这里程序要执行6秒多钟,修改一下

from multiprocessing import Process
import time

def task(sec):
    print(f"is running")
    time.sleep(sec)
    print(f"is gone")

if __name__ == "__main__":
    start_time = time.time()

    # 错误示范
    # for i in range(1,4):
    #     p = Process(target=task, args=(i,))  # 创建一个进程对象
    #     p.start()
    #     p.join()

    # 正确示范
    l1 = []
    for i in range(1, 4):
        p = Process(target=task, args=(i,))  # 创建一个进程对象
        l1.append(p)
        p.start()
    for i in l1:
        i.join()

    print(f"主{time.time()-start_time}")

is running
is running
is running
is gone
is gone
is gone
主3.099172592163086
则程序只需要执行三秒多钟,时间成本上节约了将近一半!

Guess you like

Origin blog.csdn.net/m0_50481455/article/details/113748979