Python3 System Programming Process

Source: Internet
Author: User

    • Creation of the process-fork

Process vs Program

The finished code is called a program when it is not running.

The code that is running is called a process. The process is different from the program, except that it contains the code and the environment that needs to be run.

Fork ()

Python's OS module encapsulates common system calls, including fork, which makes it easy to create child processes in a Python program :

Description

When the program executes to Os.fork (), the operating system creates a new process (the child process), and then copies all the information from the parent process into the child process.

Then both the parent and child processes get a return value from the fork () function, which must be 0 in the child process, and the parent process is the ID number of the child process.

In the Unix/linux operating system, a fork () system function is provided, which is very special. The normal function is called once, but the fork () is called once, and is 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 returns zero forever, 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 can get the ID of the parent process simply by calling Getppid ().

Getpid () Getppid ()

    • Multi-process Modify global variables

Summarize:

In a multi-process, all data (including global variables) for each process has its own, complementary effect.

    • Multiple fork Problems

If there are two fork function calls in a program, will there be three processes/

Description

Multiprocessing

The multiprocessing module is a cross-platform version of a multi-process module.

Description

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 ().

The join () method can wait for the child process to end before continuing to run, typically for inter-process synchronization.

The process syntax structure is as follows:

Process ([Group[,targer[,name[,args[,kwargs]]])

Target: Represents the object called by this process instance;

Args: Represents the positional parameter tuple of the calling object;

Kwargs: A dictionary of keyword parameters that represents the calling object;

Name: Alias for the current process instance;

Group: Most of the cases are not available;

Common methods of the process class:

Is_alive (): Determines whether the process instance is still executing;

Join ([timeout]): Whether to wait for the process instance execution to end, or wait a few seconds;

Start (): Start the process instance (Create child process);

Run (): If no target parameter is given, the run () method in the object is executed when the start () method is called on the object;

Terminate (): terminates immediately, regardless of whether the task is completed or not.

Common properties of the process class:

Name: The current process instance alias, which defaults to Process-n,n as an integer incrementing from the beginning;

PID: The PID value of the current process instance;

Example 1

Example 2

Python3 System Programming 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.