Python concurrent Programming Multi-process 1.1 multiprocessing module
Multithreading in Python cannot take advantage of multicore resources, and if you want to fully use the resources of multicore CPUs, most of the situations in Python require multiple processes. Python provides us with a very useful multi-process package multiprocessing!
OS.CPU_COUTN ()
The multiprocessing module is used to open sub-processes and perform our customized tasks (such as functions, implementation functions, etc.) in the module.
The multiprocessing module has many functions:
?支持子进程?通信和共享数据?执行不同形式的同步?提供了 Process,Queue,Pipe,Lock 等组件
Unlike threads, processes do not have any shared state, and process-modified data is restricted to the city.
Introduction to the 1.2 process class to create a class for processes
Process([group [, target [, name [, args [, kwargs]]]]]),由该类实例化得到的对象,表示一个子进程中的任务(尚未启动)
Emphasis: 1. You need to use a keyword to specify parameter 2. args Specifies the positional parameter to be passed to the target function, which is a tuple form and must have a comma
Parameter introduction
- The group parameter is not used, and the value is always None (not understood, does not need to understand)
- Target represents the calling object, the task to be performed by the child process.
- Args represents the positional parameter tuple of the calling object, args = ("A ', ' a ', ' B ', ' C ': ' FF ')
- Kwargs represents the dictionary of the calling object Kwargs = {' A ': 1, ' B ': 2, ' C ': 3}
- Name is the child process
Method Introduction
- P.start () Start: Starts the process and calls the P.run () in the child process
- P.run () Call: The method that runs at the start of a process, is exactly what it calls the function specified by target, and we must implement the method in the class of our custom class.
- P.terminate () terminate the process: Force terminate the process p, no cleanup operation, if p creates a child process, the child process is a zombie process, using this method requires special care in this case. If P also holds a lock then it will not be released, resulting in deadlocks.
- P.is_alive (): Returns True if P is still running.
- P.join ([timeout]) waits for termination: The main thread waits for p to terminate (emphasizing that the main thread is in the same state, and P is in the running state). Timeout is an optional time-out, and it needs to be emphasized that P.join can only join the start-open process, not Jo In the process that the run opens.
Properties Introduction
- P.daemon: The default value is False, if set to True, represents the daemon that P is running in the background, when P's parent process terminates, p also terminates with it, and set to True, p cannot create its own new process and must be set before P.start ()
- P.name: The name of the process
- P.pid: PID of the process
- P.exitcode: The process is None at run time, and if-n means the end of signal N (understanding part).
- P.authkey: The authentication key for the process, by default, is a 32-character string that is randomly generated by os.urandom (). The purpose of this key is to provide security for the underlying interprocess communication that involves a network connection, which can only succeed if you have the same authentication key (see section).
Use of the 1.3 Process class
First step: Two ways to create and open a child process
Under Windows Process () must be placed under ifname = = 'mian: '
#开进程的方法详见我的 pycharm??
Concurrent programming Multi-process process process