Python multi-process operation instance and python process instance
Due to GIL restrictions in CPython implementation, multithreading in python is not actually a real multi-thread. to fully use the resources of multi-core CPUs, we need to use multiple processes in most cases in python. This may be the reason why the multi-process class library in python is so simple and easy to use. In python, you can use multiple processes as easily as multithreading.
I. Multi-Process
Process member variables and methods:
> The definition of class multiprocessing. Process ([group [, target [, name [, args [, kwargs]) is similar to threading. Thread. Target indicates the function run by the process, and args and kwargs indicate the target parameter.
> Name, pid
Process name and process id.
> Daemon Member
The daemon flag is a Boolean variable that must be set before start () is called. The initial daemon value is inherited from the parent process. When a process ends, it tries to end all its daemon sub-processes.
Note:
Daemon processes are not allowed to create sub-processes. Otherwise, when the daemon process ends, its child process cannot end.
Here, daemon is not a Unix daemon process. When the parent process ends, all daemon sub-processes will also be terminated (for non-daemon Processes, the parent process does not wait for non-daemon purple sub-processes, unless the join () method is explicitly used for non-daemon sub-processes ).
> Exitcode
If the process has not exited, it is None. If the exit is correct, it is 0. If there is an error, it is an error code greater than 0. if the process is terminated, it is-1 * singal.
> Start (), is_live (), terminate ()
Start () is used to start the process, is_live () is used to view the process status, and terminate () is used to terminate the process.
> Run ()
You can overload the run () method in the process subclass to set the task of the process. Reload process is another way to construct a new process. To some extent, it is equivalent to the target parameter of process.
Static multiprcessing method:
> Multiprocessing. cpu_count ()
Used to obtain the number of CPU cores and set the number of sub-processes.
> Multiprocessing. active_children ()
Used to obtain all the current sub-processes, including daemon and non-daemon sub-processes.
Instance:
Copy codeThe Code is as follows:
Import multiprocessing
Import time
Import sys
Def worker (num ):
P = multiprocessing. current_process ()
Print ('starting: '+ p. name + ":" + str (p. pid ))
Print (str (num ))
Sys. stdout. flush ()
Print ('exiting: '+ p. name + ":" + str (p. pid ))
Sys. stdout. flush ()
Def daemon ():
P = multiprocessing. current_process ()
Print ('starting: '+ p. name + ":" + str (p. pid ))
Sys. stdout. flush ()
Time. sleep (10)
Print ('exiting: '+ p. name + ":" + str (p. pid ))
Sys. stdout. flush ()
Def non_daemon ():
P = multiprocessing. current_process ()
Print ('starting: '+ p. name + ":" + str (p. pid ))
Sys. stdout. flush ()
Time. sleep (20)
Print ('exiting: '+ p. name + ":" + str (p. pid ))
Sys. stdout. flush ()
If _ name _ = '_ main __':
W = multiprocessing. Process (name = 'worker', target = worker, args = (100 ,))
D = multiprocessing. Process (name = 'daemon', target = daemon)
D. daemon = True
Nd = multiprocessing. Process (name = 'non-daemon', target = non_daemon)
W. start ()
D. start ()
Nd. start ()
Print ("the number of CPU is" + str (multiprocessing. cpu_count ()))
Print ("All children processes :")
For p in multiprocessing. active_children ():
Print ("child:" + p. name + ":" + str (p. pid ))
Print ()
W. join ()
# D. join ()
Running result:
As shown in the preceding example, no non-daemon sub-processes use the join () method. As a result, the parent process exits after the non-daemon process ends.