. Python Multi-process

Source: Internet
Author: User

Multi-process concept:

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 used to complete various functions of the operating system is the system process, which is the operating system itself; The user process does not have to tell me more, all the processes you start are user processes. A process is the unit in which the operating system allocates resources.

Its ideas are briefly described as follows: Under the management of the operating system, all running processes take turns using the CPU, each process is allowed to take up CPU time is very short (such as 10 milliseconds), so that the user does not feel that the CPU is in turn to serve 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.

#多进程允许我们充分的利用cpu

The difference between multi-process and multithreading:

Multi-process uses multiple cores of the CPU and is suitable for operation-intensive

Make full use of multiple CPU running processes, the system execution rate will be higher

The scope of the process is larger than the scope of the thread, the process is independent of each other, a process hangs, and does not affect the use of other processes.

Multithreading uses a core of the CPU and is suitable for IO-intensive

Threads are part of a process, and shared resources exist between threads, and when a thread hangs, it causes the associated thread to hang.

Component:

Multiprocess Module

Support to create sub-processes, communicate, share data, perform different forms of synchronization, provide process (create sub-process), pipe (pipeline [default is bidirectional, can one-way]), lock (Process lock [when a process reads a file, will lock not allow other processes to read, when the process is read, will unlock the lock, Allow other processes to read files]) and other components.


Multi-Process Multiprocess module method:

Import?multiprocessingp?=?multiprocessing.cpu_count () #统计cpu的个数m? =?multiprocessing.active_children () # Statistics the existence of the child process print? (p) print? (m)


Common process methods:

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:

Name process names

The PID of PID process

Import?multiprocessingimport?timedef?worker_1 (interval):???? Time.sleep (interval)???? Print? ("Hello?world")???? if?__name__?==? " __main__ ":???? C?=?multiprocessing. Process (target=worker_1,?args= (5,))??????????? #target? Represents the target function name, without parentheses??????????? #args? Represents the required parameters for worker_1, and args receives a tuple of the parameter type, and a single element with a comma???? C.start ()?????????????????? #启动进程???? Print? (C.is_alive ())??????? #确定进程是否存活???? C.join (timeout=1)?????????? #等待子进程执行完毕, or time out???? Print? ("End?main")



Multi-Process instances:

Import?timeimport?multiprocessingdef?worker (name,?interval):???? Print? ("Work?") {0}?start. Format (name)???? Time.sleep (interval)???? Print? ("Work?") {0}?end. Format (name)???? if?__name__?==? " __main__ ":???? Print? ("Main?start")???? Print? ("Statistics CPU has {0} cores"). Format (Multiprocessing.cpu_count ())???? P1?=?multiprocessing. Process (target=worker,?args= ("Worker1",? 2))???? P2?=?multiprocessing. Process (target=worker,?args= ("Worker2",? 3))???? P3?=?multiprocessing. Process (target=worker,?args= ("Worker3",? 4))???? P1.start ()???? P2.start ()???? P3.start ()???? For?p?in?multiprocessing.active_children ():???????? Print? ("pid name {0},pid {1}". Format (p.name,?p.pid))???? Print ("Main?end")

Printing results:

Main start

Statistics CPU has 4 cores

PID name Process-1,pid No. 6388

PID name Process-3,pid No. 3176

PID name Process-2,pid No. 1268

Main end

Work Worker1 Start

Work Worker3 start???????????????? # # P1,P2,P3 After the start is independent operation, successively can not order, some start fast, some start slow

Work Worker2 Start

Work Worker1 End

Work Worker2 End

Work Worker3 End????????????????? # # The shutdown process must be p1,p2,p3 in the order in which it was started.


. Python Multi-process

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.