Because of the Gil restrictions in the CPython implementation, multithreading in Python is not really multi-threaded, and if you want to fully utilize the resources of multi-core CPUs, we need to use multiple processes in most cases in Python. This may be why the multi-process class library in Python is so simple and easy to use. You can use multiple processes as simple as multithreading in Python.
I. Multi-process
member variables and methods for the process:
>>class multiprocessing. Process ([group[, target[, name[, args[, Kwargs]]] is defined as similar to threading. Thread. Target represents the function that this process runs, and args and Kwargs represent the parameters of target.
>>name, PID
Represents the name of the process, the process ID, respectively.
>> Daemon Members
The daemon flag bit bool variable needs to be set before the start () call. The initial value of the daemon is inherited from the parent process. When a process ends, it attempts to end all of its daemon subprocess.
Attention:
The daemon process does not allow the creation of child processes. Otherwise, its subprocess cannot be ended when the daemon process ends.
The daemon here is not a UNIX daemon process, and all daemon are terminated when the parent process is finished (for non-daemon processes, the parent process does not wait for the non-daemon purple child process, unless the join () method is displayed for the non-daemon subprocess).
>> ExitCode
None if the process has not exited, if the correct exit is 0, or if there is a >0 error code, or -1*singal if the process is terminated.
>> start (), Is_live (), Terminate ()
Start () is used to start the process, is_live () to view the status of the process, terminate () to terminate the process.
>> Run ()
You can overload the run () method in subclasses of the process to set the task of the procedure. Overloaded process is another way to construct a new process, which is equivalent to the target parameter of a procedure.
static method for multiprcessing:
>> Multiprocessing.cpu_count ()
Used to obtain the current number of cores for the CPU, which can be used to set the number of subsequent child processes.
>> Multiprocessing.active_children ()
Used to get all of the current child processes, including daemon and non-daemon subprocess.
Instance:
Copy Code code 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 ()
Run Result:
You can see from the example above that there is no more than Daemon subprocess using the Join () method, and the resulting parent process exits without waiting for the daemon process to end.