Python multi-process (i)

Source: Internet
Author: User

Operating system processes


The Unix/linux operating system provides a fork () system call, which is very special. A normal function call, called once, is returned once, but the fork () is called once and returned two times because the operating system automatically copies the current process (called the parent process), which is then returned within the parent and child processes, respectively. The child process always returns 0, and the parent process returns the ID of the child process. The reason for this is that a parent process can fork out many child processes, so the parent process has to note the ID of each child process, and the child process only needs to call Getppid () to get the ID of the parent process.
A process is an execution activity on a computer by a program. When you run a program, you start a process. Obviously, the program is Dead (static), and the process is alive (dynamic). Processes can be divided into system processes and user processes. The process that is used to complete the various functions of the operating system is the system process, which is the operating system itself in the running state. All processes that are initiated by you are user processes.
In layman's terms, under the management of the operating system, all running processes take turns using the CPU, and each process allows for a very short CPU time (for example, 10 milliseconds), so that the user simply does not feel that the CPU is in turn serving multiple processes, as if all the processes are running continuously. But there is actually only one process that occupies the CPU at any one time.

Multi-process


The difference between multi-process and multithreading
Multithreading uses a core of the CPU and is suitable for IO-intensive.
Multi-process uses multiple cores of the CPU and is suitable for operation-intensive.

Multiprocessing supports sub-processes, communicates, shares data, performs different forms of synchronization, and provides components such as Process,pipe, lock, and so on.

Process

Create a Process Object

p = multiprocessing. Process (target=worker_1, args= (2,))

target = function name
args = parameters required by the function, passed in as a tuple
Note: The representation of a tuple of a single element (element,) has a comma

Two methods used by multprocessing
Cpu_count () Count the total number of CPUs
Active_children () Get all child processes

Common methods for Process objects

Is_alive () Determine if the process is alive
Run () Start process
Start () starts the process and automatically calls the Run method, a common
Join (timeout) waits for the process to end or until it expires

Common Properties of Process

Name process names
The PID of PID process

Related code Examples
ImportMultiprocessingImport Timedefworker (args, interval):Print("start worker {0}". Format (args)) time.sleep (interval)Print("end worker {0}". Format (args)defMain ():Print("Start main") P1= multiprocessing. Process (Target=worker, args= (1, 1)) P2= multiprocessing. Process (Target=worker, args= (2, 2)) P3= multiprocessing. Process (Target=worker, args= (3, 3)) P1.start () P2.start () P3.start ()Print("End Main")if __name__=='__main__': Main () Result: Start mainend mainstart worker1start worker3start worker2End worker1End worker2End worker3

p = multiprocessing. Process (target=, args=)
Target specifies the function that needs to be executed when the process executes
Args is the parameter that needs to be passed to the function when the process executes
Note: args must be a tuple, especially if the function needs to pass in a parameter (1,)
P stands for a multi-process
P.is_alive () Determine if the process is alive
P.run () Start process
P.start () starts the process, he will automatically call the Run method, it is recommended to use the start
P.join (timeout) waits for the child process to finish or to continue execution after the timeout period
P.terminate () forcing the child process to exit
Name of the p.name process
PID of the P.pid process

ImportMultiprocessingImport Timedefworker (args, interval):Print("start worker {0}". Format (args)) time.sleep (interval)Print("end worker {0}". Format (args)defMain ():Print("Start main") P1= multiprocessing. Process (Target=worker, args= (1, 1)) P2= multiprocessing. Process (Target=worker, args= (2, 2)) P3= multiprocessing. Process (Target=worker, args= (3, 3) ) P1.start () P1.join (timeout=0.5) P2.start () P3.start ()Print("The number of the CPU is: {0}". Format (Multiprocessing.cpu_count ())) forPinchMultiprocessing.active_children ():Print("the name of active children is: {0}, PID was: {1} is alive". Format (p.name, p.pid))Print("End Main")if __name__=='__main__': Main () Result: Start Mainstart worker1The number of the CPU is: 4The name of the active children is: Process-3, PID is: 9056 isalivethe Name of active children is: Process-2, PID is: 5844 isalivethe Name of active children is: Process-1, PID is: 8428 isaliveend Mainstart worker2start worker3End worker1End worker2End worker3

When you create a child process, you only need to pass in a parameter that executes functions and functions, create a process instance, and start with the start () method, so that the creation process is simpler than fork ().

Lock component


When we use a multi-process to read and write files, if a process is to write files, a process is to read the file, if two files at the same time, it is certainly not, it must be the end of the file write, you can read the operation. Or when multiple processes share some resources, and only one process can access them, there is a lock mechanism to control.

When multiple processes require access to shared resources, lock can be used to avoid conflicting access. The main use of Lock.acquire () and Lock.release ()

Import TimeImportMultiprocessingdefadd1 (lock, value, number): With Lock:Print("start add1 number= {0}". Format (number) forIinchRange (1, 5): number+=Value Time.sleep (0.3)            Print("Number = {0}". Format (number)defadd3 (lock, value, number): Lock.acquire ()Print("start add3 number= {0}". Format (number)Try:         forIinchRange (1, 5): number+=Value Time.sleep (0.3)            Print("Number = {0}". Format (number)exceptException as E:Raiseefinally: Lock.release ()Passif __name__=='__main__':    Print("Start main") number=0 Lock=multiprocessing. Lock () P1= multiprocessing. Process (TARGET=ADD1, args= (lock, 1, number)) P3= multiprocessing. Process (TARGET=ADD3, args= (lock, 3, number)) P1.start () P3.start ( )Print("End Main") Results: Start mainend mainstart add3 number=0number= 3 Number= 6 Number= 9 Number= 12Start add1 number=0number= 1 Number= 2 Number= 3 Number= 4

Shared memory


Python's multiprocessing module also provides us with shared memory operations.
General variables are not able to communicate between processes, multiprocessing provides us with the value and array modules, they can be used in a non-pass process, both value and array need to set the type of the value stored in it, D is a double type, I is the int type.

Import TimeImportMultiprocessing fromMultiprocessingImportValue, Array, ManagerdefAdd1 (value, number):Print("start add1 number= {0}". Format (number.value)) forIinchRange (1, 5): Number.value+=valuePrint("Number = {0}". Format (number.value))defadd3 (value, number):Print("start add3 number= {0}". Format (number.value))Try:         forIinchRange (1, 5): Number.value+=valuePrint("Number = {0}". Format (number.value))exceptException as E:Raiseeif __name__=='__main__':    Print("Start main") number= Value ('D', 0) P1= multiprocessing. Process (TARGET=ADD1, args= (1, number)) P3= multiprocessing. Process (TARGET=ADD3, args= (3, number)) P1.start () P3.start ( )Print("End Main") Results: Start mainend mainstart add1 number= 0.0 Number= 1.0 Number= 2.0 Number= 3.0 Number= 4.0Start add3 number= 4.0 Number= 7.0 Number= 10.0 Number= 13.0 Number= 16.0

Python multi-process (i)

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.