Multi-process | Teach You to Get Started With Python One Hundred and Four

Multi-process | Teach You to Get Started With Python One Hundred and Four

Multi-process.The multiprocessing module is a cross-platform version of the multi-process module. It provides a Process class to represent a process object. This object can be understood as an independent process that can perform other things.
Use of multiple processes

Multi-process.process


Program: For example, xxx.py is a program, which is a static one.
Process: After a program runs, the code + the resources used is called a process, which is the basic unit for the operating system to allocate resources.

Multi-process.Not only can multitasking be done through threads, but processes can too.
state of the process
In work, the number of tasks is often larger than the number of cores of the CPU , that is, some tasks must be executing, while other tasks are waiting for the CPU to execute, thus resulting in different states.
•Ready state: The running conditions have been met and are waiting for the CPU to execute.
•Execution state: The cpu is performing its function.
•Waiting state: Waiting for certain conditions to be met, such as a program sleep, it is in the waiting state at this time.
create process
The multiprocessing module is a cross-platform version of the multi-process module. It provides a Process class to represent a process object. This object can be understood as an independent process that can perform other things.

Multi-process.When creating a child process, you only need to pass in an execution function and function parameters, create a Process instance, and start it with the start() method.
import multiprocessing, time , os

def dance(n):
for i in range(n):
time.sleep ( 0.5 ) _
print ( 'Dancing{}, pid ={}' . format ( i , os . getpid ()))

def sing(m):
for i in range(m):
time.sleep ( 0.5 ) _
print ( 'Singing {}, pid ={}' . format ( i , os . getpid ()))

if __name__ == '__main__' :
print ( ' pid ={}' of main process.format ( os.getpid ( ) ) )
# create two processes
# target is used to indicate the task to be executed
# args is used to pass parameters, the type is a tuple
p1 = multiprocessing.Process (target=dance, args =( 100 ,))
p2 = multiprocessing.Process (target=sing, args =( 100 ,))

p1.start ( )
p 2.start ()
Method description
Process( target [, name [, args [, kwargs ]]])
•target: If a reference to the function is passed, the subprocess can be tasked to execute the code here
•args : Parameters passed to the function specified by target, passed as a tuple
•kwargs : pass named arguments to the function specified by target
•name: Set a name for the process, you can leave it blank
Common methods of instance objects created by Process:
•start(): start a child process instance (create a child process)
•is_alive (): Determine whether the process child process is still alive
•join([timeout]): whether to wait for the end of the execution of the child process, or how many seconds to wait
•terminate(): terminates the child process immediately, regardless of whether the task is completed or not
Common properties of instance objects created by Process:
•name: the alias of the current process, the default is Process-N, N is an integer starting from 1
•pid : the pid (process number) of the current process

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00