This article briefly and concisely introduces Python In programming language Multi process .
What is multi process ?
Multiprocessing refers to the ability of a system to support multiple processors at the same time . Applications in multiprocess systems are broken down into smaller routines that run independently . The operating system assigns these threads to the processor , To improve the performance of the system .
Why choose multiple processes ?
Consider a computer system with a single processor . If multiple processes are assigned to it at the same time , It will have to interrupt each task and switch briefly to another task , To keep all processes running .
It's like a chef working alone in the kitchen . He has to do several tasks , Such as baking , stir , Knead dough, etc .
therefore , The point is : The more tasks you have to accomplish at the same time , The harder it is to track all the tasks , And keeping the right time becomes more challenging .
This is where the concept of multiprocessing comes into being !
Multi process systems can have :
ad locum ,CPU You can easily perform multiple tasks at once , Each task uses its own processor .
This is just like the chef in the previous case with the help of his assistant . Now? , They can assign tasks among themselves , The chef doesn't have to switch between his tasks .
Python Multiple processes in
stay Python in , Multi process The module includes a very simple and intuitive API, Used to divide work between multiple processes .
Let's refer to a simple example of using a multiprocessing module :
# Import multiprocess module
import multiprocessing
def print_cube(num):
""" A function for printing a cube of a given number """
print("Cube: {}".format(num * num * num))
def print_square(num):
""" Function prints the square of a given number """
print("Square: {}".format(num * num))
if __name__ == "__main__":
# Create a process
p1 = multiprocessing.Process(target=print_square, args=(10,))
p2 = multiprocessing.Process(target=print_cube, args=(10,))
# Start the process 1
p1.start()
# Start the process 2
p2.start()
# Waiting process 1 complete
p1.join()
# Waiting process 2 complete
p2.join()
# All process tasks are completed
print("Done!")
Running results :
Let's try to understand the above code :
import multiprocessing
To create a process , We created one process Class object . It requires the following parameters :
Be careful : technological process Constructors also take many other arguments , We will discuss later . In the example above , We created 2 Processes with different objective functions :
p1 = multiprocessing.Process(target=print_square, args=(10, ))
p2 = multiprocessing.Process(target=print_cube, args=(10, ))
To start a process , We use process Class start-up Method .
p1.start()
p2.start()
After the process starts , The current program will also continue . To stop executing the current program before the process completes , We use join Method .
p1.join()
p2.join()
therefore , The current program will first wait p1 Completion , And then wait p2 Of complete . Once they're done , The next statement of the current program will be executed .
Let's consider another program to understand in the same python The concept of different processes running on a script . In the following example , We print the of the process running the objective function ID:
# Import multiprocess module
import multiprocessing
import os
def worker1():
# Printing process id
print("ID of process running worker1: {}".format(os.getpid()))
def worker2():
# Printing process id
print("ID of process running worker2: {}".format(os.getpid()))
if __name__ == "__main__":
# Printing main program process id
print("ID of main process: {}".format(os.getpid()))
# Create a process
p1 = multiprocessing.Process(target=worker1)
p2 = multiprocessing.Process(target=worker2)
# Start the process
p1.start()
p2.start()
# process ID
print("ID of process p1: {}".format(p1.pid))
print("ID of process p2: {}".format(p2.pid))
# Wait for the process to complete
p1.join()
p2.join()
# Both processes have been completed
print("Both processes finished execution!")
# Check if the process is active
print("Process p1 is alive: {}".format(p1.is_alive()))
print("Process p2 is alive: {}".format(p2.is_alive()))
Running results :
Lord python Scripts have different processes ID, When we create a process object p1 and p2 when , A multiprocessing module will generate a with different processes ID The new process . In the above procedure , We use os.getpid() Function to get the of the process running the current target function ID.
Please note that , It works with us Process Class pid Property p1 and p2 Of process ID matching .
Each process runs independently , And has its own memory space .
Once the execution of the objective function is completed , The process will terminate . The program above in , We use is_alive Method Process Class to check if the process is still active .
Consider the image below , Understand the new process and the main Python The difference between scripts :