day35

An introduction to the multiprocessing module

    Multi-threading in python cannot take advantage of multi-core. If you want to fully use the resources of multi-core CPU (os.cpu_count() view), you need to use multi-process in most cases in python. Python provides multiprocessing.
    The multiprocessing module is used to start a child process and execute our customized tasks (such as functions) in the child process. This module is similar to the programming interface of the multithreading module threading.

  The multiprocessing module has many functions: supports subprocesses, communicates and shares data, performs different forms of synchronization, and provides components such as Process, Queue, Pipe, and Lock.

    One point that needs to be emphasized again is that unlike threads, processes do not have any shared state, and the data modified by a process is only changed within the process.

Introduction of the second Process class

    The class that creates the process :

Process([group [, target [, name [, args [, kwargs]]]]]), an object instantiated by this class, representing a task in a child process (not yet started)

emphasize:
1. You need to use keywords to specify parameters
2. args specifies the positional parameter passed to the target function, which is in the form of a tuple and must have commas

    Parameter introduction:

copy code
1 The group parameter is not used, the value is always None
2
3 target represents the calling object, that is, the task to be executed by the child process
4
5 args represents the positional parameter tuple of the calling object, args=(1,2,'egon',)
6
7 kwargs represents the dictionary of the calling object, kwargs={'name':'egon','age':18}
8
9 name is the name of the child process
copy code

  Method introduction:

copy code
1 p.start(): start the process and call p.run() in the child process
 2 p.run(): The method that runs when the process starts, it is to call the function specified by the target. This method must be implemented in the class of our custom class  
 3
 4 p.terminate(): Forcibly terminate the process p without any cleanup operation. If p creates a child process, the child process will become a zombie process. Use this method with special care. If p also holds a lock, it will not be released, resulting in a deadlock
 5 p.is_alive(): Return True if p is still running
 6
 7 p.join([timeout]): The main thread waits for p to terminate (emphasis: the main thread is in a waiting state, and p is in a running state). timeout is an optional timeout. It should be emphasized that p.join can only join the process started by start, but not the process started by run.  
copy code

    Attribute introduction:

copy code
1 p.daemon: The default value is False. If it is set to True, it means that p is a daemon process running in the background. When the parent process of p terminates, p also terminates, and after it is set to True, p cannot create its own. New process, must be set before p.start()
2
3 p.name: the name of the process
4
5 p.pid: the pid of the process
6
7 p.exitcode: The process is None when it is running. If it is -N, it means that it is terminated by signal N (just understand)
8
9 p.authkey: The authentication key of the process, the default is a 32-character string randomly generated by os.urandom(). The purpose of this key is to provide security for low-level inter-process communication involving network connections that can only succeed if they have the same authentication key (just know)
copy code

The use of three Process classes

Note: In Windows, Process() must be placed under # if __name__ == '__main__':

copy code
Since Windows has no fork, the multiprocessing module starts a new Python process and imports the calling module.
If Process() gets called upon import, then this sets off an infinite succession of new processes (or until your machine runs out of resources).
This is the reason for hiding calls to Process() inside

if __name__ == "__main__"
since statements inside this if-statement will not get called upon import.
Since Windows doesn't have a fork, the multiprocessing module starts a new Python process and imports the calling module.
If Process() is called on import, then this will start new processes with infinite inheritance (or until the machine runs out of resources).
This is what hides the internal calls to Process(), with if __name__ == "__main__", the statements inside this if statement will not be called on import.

 

Reprinted in: https://www.cnblogs.com/whnbky/p/11508066.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326560067&siteId=291194637
Recommended