Python class library 32 [multi-process]

Source: Internet
Author: User

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.

 

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 process exits correctly, it is 0. If an error exists, it is an error greater than 0.Code-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 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:

 

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.

 

Complete!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.