The role of python multi-process kind join() function

  1. Runtime when using multiple processes without blocking with the join function
import os
from multiprocessing import Process


"""
 The following is the result of the run. We can see that the main process does not
 After the child process finishes running, it ends, and each program runs.
 Mutual interference
Parent process 10996
Finished
Child process 0 (984)
Child process 2 (13632)
Child process 1 (8308)
Child process 3 (8640)
Child process 4 (8412)
"""
def run_proc(name):
    print("Child process %s (%s)" % (name, os.getpid()))


if __name__ == "__main__":
    print("Parent process %s" % (os.getpid()))
    for i in range(5):
        p = Process(target=run_proc, args=(str(i),))
        p.start()
         The role of the # join function is to achieve process synchronization,
         # and it specifically blocks the current process, that is, the main process until the process of calling the join function is executed.
         # Continue to execute the current process.
    print("Finished")

2, when only one join is called, the main process that is called will wait for the child process to continue to execute downward after the end of the run.

import os
import time
from multiprocessing import Process

"""
The following is the result of the run. We can see that the main process does not end directly.
 Instead, it blocks and waits for the process started by p01 to finish executing.
Parent process 14208
Child process 1 (19392)
 A total of 0 seconds of sleep
 I have slept for 1 second.
 A total of 2 seconds of sleep
 A total of 3 seconds of sleep
 A total of 4 seconds of sleep
Child process 2 (17348)
Finished

"""
def run_proc(name):
    print("Child process %s (%s)" % (name, os.getpid()))

def run_proc01(name):
    for i in range(5):
        time.sleep(1)
                 Print("A total sleep of %s seconds" %i)
    print("Child process %s (%s)" % (name, os.getpid()))


if __name__ == "__main__":
    print("Parent process %s" % (os.getpid()))
    p = Process(target=run_proc, args=(str(1),))
    p01 = Process(target=run_proc01, args=(str(2),))
    p.start()
    p01.start()
    p01.join()
    print("Finished")

3, when only calling a join function in front of a process's start function, then because the main process is blocked to the join position will not start executing the next line of code until the process of calling join is executed, so the start time is blocked. After time.


import os
from multiprocessing import Process


"""
 The following is the result of the run. We can see that the for loop is first entered in the main process.
 Start the first child process and then block the main process by calling the join function until
 After the first child process finishes executing, it starts to cancel the blocking and continues to execute the main process.
 Open the second child process and then call the join and block the main process because the second child process also
 Until the end of the cycle. 
Parent process 8028
Child process 0 (13792)
Child process 1 (11456)
Child process 2 (16948)
Child process 3 (16240)
Child process 4 (10544)
Finished
"""
def run_proc(name):
    print("Child process %s (%s)" % (name, os.getpid()))


if __name__ == "__main__":
    print("Parent process %s" % (os.getpid()))
    for i in range(5):
        p = Process(target=run_proc, args=(str(i),))
        p.start()
        p.join()
         The role of the # join function is to achieve process synchronization,
         # and it specifically blocks the current process, that is, the main process until the process of calling the join function is executed.
         # Continue to execute the current process.
    print("Finished")

4, therefore, we can see from the above that if multiple processes are not well written, it is not as good as a single process because it takes time to restart the process and block the process. Therefore the join function should be after all start functions (generally).

import os
from multiprocessing import Process


"""
 In this case, no matter how the main process runs, it waits for all the child processes to complete and continues to execute downwards.
Parent process 7764
Child process 0 (12884)
Child process 2 (15644)
Child process 1 (3936)
Child process 4 (6876)
Child process 3 (16904)
Finished

"""
def run_proc(name):
    print("Child process %s (%s)" % (name, os.getpid()))


if __name__ == "__main__":
    print("Parent process %s" % (os.getpid()))
    list1 =[]
    for i in range(5):
        p = Process(target=run_proc, args=(str(i),))
        list1.append(p)
        p.start()
    for i in list1:
        i.join()
         The role of the # join function is to achieve process synchronization,
         # and it specifically blocks the current process, that is, the main process until the process of calling the join function is executed.
         # Continue to execute the current process.
    print("Finished")

5, below to share with you a bit of a problem code can be seen, you can leave a message or private chat about its problems.

import os
import time
import random
from multiprocessing import Process

"""
 The following is the result of the operation
Parent process 2432
Child process 0 (2416)
Child process 2 (8972)
Child process 1 (16916)
Child process 3 (13144)
Child process 4 (15608)
Finished
"""


def run_proc(name):
    print("Child process %s (%s)" % (name, os.getpid()))


def run_proc01(name):
    i = random.random()
    time.sleep(i)
    print("Child process %s (%s)" % (name, os.getpid()))


if __name__ == "__main__":
    print("Parent process %s" % (os.getpid()))
    for i in range(5):
        p = Process(target=run_proc, args=(str(i),))
        p.start()
         The role of the # join function is to achieve process synchronization,
         # and it specifically blocks the current process, that is, the main process until the process of calling the join function is executed.
         # Continue to execute the current process.
    p.join()
    print("Finished")


Intelligent Recommendation

Python multi-thread (2) join function

Write a multi-threaded program first operation result: It can be found that all done is output first, and then T1 finish is output. What if you want to wait for the T1 thread to finish executing and t...

The role of if __name__=="__main__" statement in Python in the process of calling multi-process Process

February 27, 2018 Yu C515    introduction Recently I am going to learn how to use multiple processes in Python. When I looked through related books and online materials, I found that all the code cont...

Usage of Python multi-thread and multi-process Join and daemon

Daemon process: The main process code is finished, and the daemon process ends (the main process is the daemon) Daemon thread: The non-daemon thread code finishes running, and the daemon thread ends (...

Python multi-threaded and multi-process Join () and daemon properties

Use the join () method main process waiting for the sub-process to end. No Join () method, the main process ends, the child process continues until the end. DAEMO is called True as a daemon, the main ...

Python multi-process understanding multiprocessing Process join run

multiprocessingModule. When the multiprocessing module is on windowsSimulate fork effect, Can be cross-platform, so most of them use multiprocessing. Here is a simple code to demonstrate the creation ...

More Recommendation

The role of if __name__=="__main__" statement in Python in calling multi-process Process

There is no fork function in unix/linux in windows system to provide multi-process functions, but multiprocessing module is provided in Python to complete multi-process functions. In all the sample co...

Java database multi-table join query cross-connection, inner join. left outer join, right outer join, foreign key constraint role and establishment, statistical (aggregate) function, sub-query, joint query

The fifteenth day of learning java Cross connection Internal connection Left outer join Right outer join Example of a many-to-many relationship association query: The role and establishment of foreign...

Python multi-process and random function

Different multi-process (Windows and Linux) on the platform Liao Xuefeng achieved on multi-process Python on different platforms:link Important: Linux Fork achieved through multi-process, Windows mult...

Python multi-process run function

Python multi-process run function...

Python function programming + multi-process

Summary: Functional programming avoids the FOR cycle structure, which is conducive to Debug only pays only core code. In addition, the functional type programming + multi-process efficiency is higher ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top