Because of the Gil limitations of the CPython implementation, multithreading in Python is not really multi-threaded, and if you want to fully use the resources of multicore CPUs, most of the cases in Python we need to use multiple processes. This is probably why the multi-process libraries in Python are so simple and useful. In Python, you can use multiple processes as simple as multithreading.
One, multi-process
member variables and methods for process:
>>class multiprocessing. Process ([group[, target[, name[, args[, Kwargs]]]) is defined similar to threading. Thread. Target represents the function that this process runs, and args and Kwargs represent the parameters of the target.
>>name, PID
Represents the name of the process, the process ID, respectively.
>> Daemon Members
The daemon flag bit bool variable, which needs to be set before the start () call. The initial value of the daemon is inherited from the parent process. When a process is finished, it tries to end all of its daemon subprocess.
Attention:
The daemon process does not allow child processes to be created. Otherwise, when the daemon process ends, its child processes cannot be ended.
The daemon here is not a UNIX daemon process, and all daemon child processes will be terminated when the parent process ends (for non-daemon processes, the parent process does not wait for a non-daemon process, unless the join () method is used for non-daemon child processes in the display).
>> ExitCode
If the process has not exited, then none, if the correct exit is 0, if there is an error, the error code is >0, or -1*singal if the process is terminated.
>> start (), Is_live (), Terminate ()
Start () is used to start the process, is_live () is used to view the status of the process, and terminate () is used to terminate the process.
>> Run ()
You can overload the run () method in a subclass of the process to set the task for the processes. Overloaded process is another way to construct a new process, which is equivalent to the target parameter of the process in some degree.
Static methods for multiprcessing:
>> Multiprocessing.cpu_count ()
Used to obtain the current CPU's number of cores, which can be used to set the number of subsequent sub-processes.
>> Multiprocessing.active_children ()
Used to obtain all current child processes, including daemon and non-daemon child processes.
Instance:
The 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 ()
Operation Result:
You can see from the above example that there are no more non-daemon child processes using the join () method, resulting in the parent process exiting without waiting for the non-daemon process to end.