Python basic grammar 26-process operation

1. Two methods of creating a process:
multiprocessing is similar to using the Thread class to create a multi-thread method. There are also the following two ways to use the Process class to create a multi-process:
Method 1: Directly create an instance object of the Process class, so that a new one can be created The process;
method 2: Create an instance object by inheriting the subclass of the Process class, or create a new process.
(1). Creating a process through the Process class
is very similar to using the thread class to create a child thread. Using the Process class to create an instantiated object is essentially to call the constructor of the class to create a new process. The format of the construction method of the Process class is as follows:​​def __init__(self,group=None,target=None,name=None,args=(),kwargs={})​​ Among them, the meaning of each parameter is: group
: This parameter is not implemented, no need to pass parameters;
target: specify the execution task for the new process, that is, specify a function;
name: set the name for the new process;
args: pass non-keyword parameters for the parameters specified by the target parameter;
kwargs: Pass keyword arguments for the arguments specified by the target parameter.
Example:
from multiprocessing import Process
import os,time
print('Current process ID:',os.getpid())
#Define a function to be used as the target parameter of the new process
def action(name,*add):
    print(name)
    for arc in add:
        print(

Guess you like

Origin blog.csdn.net/a316495442/article/details/128477312
Recommended