Python Multi-process

Source: Internet
Author: User

Haven't updated blog content for a long time, have been studying python recently, just know the multi-process then we will know about the multi-process together. (Write to yourself for fear of forgetting ...) )

1. What is a process?

Process refers to the execution of a program, while a program refers to a file, the executable code on the disk.

Process runtime needs to load programs such as memory, process has a certain life cycle, process creation is controlled by the operating system, process occupies the resources of the system

2, the classification of the process

IO-intensive: IO operations are much larger than compute operations, because CPU performance is much higher than IO read and write operations, so such programs generally CPU is waiting for IO to run the state.

CPU Intensive: More operations in the program, less IO, high CPU usage

3. Process Control block (PCB)

Refers to the space in memory to store process information. View process-related information under Linux: Ps-aux

Each memory has a separate 4G address space ======= stored in the virtual memory address of the computer.

4, the status of the process:

Three states:

Ready: The process has a running condition and waits for the system to allocate resources
Execute: Process occupies CPU start run
Wait: Also known as a blocking state or sleep state, the process waits for a condition, or waits for the completion of an event

  

Five states (added on 3-state basis)

NEW: Create a process that directly manifests as creating a program, or creating a child process

Termination: Completion of program execution, complete aftercare, or zombie status

Process Status ID:

D: Waiting state
S: Waiting state
T: Waiting state
R: Run state, ready state
Z: Zombie State process
N: Low-priority processes
<: High-priority
+: Foreground process

  

Process-related commands under Linux

Ps-aux displaying process information

Ps-ef

Ps-ajx

Pstree Viewing the process tree structure

Processes in the top detection system

Process priority: Linux process priority range -20---19 number smaller priority higher

    

Nice-9./while indicates that a program is running with a priority of 9

Renice n PID changes the PID process priority to n

Kill-sig PID to the PID process to send a sig signal

./while.py & Running the program in the background

5. Create a process:
Fork to create a new process (Linux and UNIX platforms)

Import OS

Fork ()
Function: Creates a child process for the current process
Parameters: None
return value:

< 0 indicates that the creation process failed
= 0 Returns a return value in the newly created process
> 0 Returns a return value in the parent process

Parent-Child processes:

Child processes inherit almost all of the code snippets from the parent process
The child process has its own independent information identity

  

  * Child and parent processes are independent and do not affect each other on the run
* Using fork return values in parent-child processes to perform different content is a fixed method of multitasking

* The return value of fork in parent process is the PID number of the child process created
* The parent-child process has almost the same resources, including the content defined before the fork

  

1 ImportOS2  fromTimeImportSleep3 4A = 105 #Create Child process6PID =os.fork ()7 8 ifPID <0:9     Print('Create process failed')Ten elifPID = =0: OneA = 1000 A     Print("This is the child process") - Else: -Sleep (2) the     Print('The pid =', PID) -  -     Print("This is the parent process") -  + Print('A =', a) - Print("**********the end***********")
View Code

Import OS

Os.getpid () Gets the PID number of the current process
Os.getppid () Gets the PID number of the current process parent process

Process Exit Function
Import OS
Os._exit ([status])

Import Sys
Sys.exit ([status])

Function: Quit a process
    

1 ImportOS2 ImportSYS3 4A =05  whileA < 10:6     Print('A =', a)7A + = 18     Try:9         ifA = = 5:Ten             #Sys.exit (3) One os._exit (0) A     exceptSystemexit as E: -         Print(e) -  the Print("+++++++++++++++++")

Parameters: Indicates the exit status of the process, 0 indicates that the process exited normally, and not 0 indicates an exception exit. Sys.exit () can pass in a string and print a string on exit

    Difference: _exit () exit directly
Exit () throws Systemexit if an exception is handled

Zombie Process: A child process exits before the parent process, and the parent process does not handle the exit state of the child process, at which point the child process becomes a zombie process

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.