Python Multi-process

Source: Internet
Author: User

1. Process VS Program

Code that has been written is called a program when it is not running.

Running code, it becomes a process

Process, in addition to containing code, there is a need to run the environment, etc., so there is a difference with the program

FORK

#Coding=utf-8ImportOSImportTimenum=0#Note that the fork function, which runs only on Unix/linux/mac, does not allow WindowsPID =Os.fork () #相当于创建了2个进程

ifPID = =0:num+=1Print('haha 1---num=%d'%num)Else: Time.sleep (1) Num+=1Print('haha 2---num=%d'%num)

Operation Result:

Summarize:
    • In multiple processes, all data in each process (including global variables) has one copy, each of which does not affect
    • Can only be executed in Linux/unix, no fork in Windows
    • Fork produces a child process with a PID of 0 at execution time.

Results

Multiple fork Problems

If there are 2 fork function calls in a program, will there be 3 processes?

#Coding=utf-8ImportOSImport Time#Note that the fork function, which runs only on Unix/linux/mac, does not allow WindowsPID =os.fork ()ifPID = =0:Print('haha 1')Else:    Print('haha 2') PID=os.fork ()ifPID = =0:Print('haha 3')Else:    Print('haha 4') Time.sleep (1)

Operation Result:

So the if else above is divided into two processes, and two processes end up separating 2 processes in total four processes.

Order of execution of parent-child processes

The sequence of parent process and sub-process execution is not regular, it depends entirely on the operation system scheduling algorithm

# here is the fork bomb, do not believe you can.   while Ture:        os.fork

Multiprocessing

If you are going to write a multi-process service program, Unix/linux is undoubtedly the right choice. Because Windows does not have a fork call, isn't it possible to write multi-process programs in Python on Windows?

Because Python is cross-platform, nature should also provide a cross-platform, multi-process support. The multiprocessing module is a multi-process module with cross-platform versions.

The multiprocessing module provides a process class to represent a processing object, and the following example demonstrates starting a child process and waiting for it to end:

 fromMultiprocessingImportProcess#code to be executed by the child processdefTest (name):Print('child process running, name=%s, pid=%d...ppid=%d'%(name, Os.getpid (), Os.getppid ()))if __name__=='__main__':    Print('parent process%d.'%os.getpid ()) P= Process (Target=test, args= ('Test',)) Print('The child process is going to execute') P.start ()#let the process begin executing the code in the test functionP.join ()# wait for the child process to end    Print('child process has ended')

Operation Result:

Process
    • Target: Represents the object called by this process instance
    • Args: The parameter tuple that represents the calling object
    • Name: Alias for the current process instance
Process Common methods:
    1. Is_alive (): Determines whether the process instance is still executing;
    2. Join ("timeout"): whether to wait for process instance execution to end, timeout: time-out
    3. Start (): Start the process instance (Create child process)
    4. Run (): When the Start () method is called on this object without a given target parameter, the Run method of the object is executed
    5. Terminate (): Terminates immediately regardless of whether the task (process) has ended

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.