程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Multiprocess in Python

編輯:Python

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 :

  • Multiprocessor , That is, a computer with multiple central processors .
  • Multicore processor , That is, there are two or more independent actual processing units ( be called “ kernel ”) A single computing component of .

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 :

  • To import a multi process module , We need to :


import multiprocessing

  • To create a process , We created one process Class object . It requires the following parameters :

    • The goal is : The function to be executed by the process
    • args: Parameters to be passed to the objective function

    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 :


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved