Python process Programming

Source: Internet
Author: User
Tags signal handler

Python process Programming

1. There are two ways to achieve concurrency, one is to let each "task" or "process" work in a separate internal space, each with its own working memory area. However, although processes can be executed in a separate memory space, they are not actually "simultaneously" unless they are executed on a separate processor. The time slice of the processor is allocated to a process by the operating system. After the time slice is used up, the processor needs to exit and wait for the arrival of another time slice. Another way is to specify multiple "execution Threads" in the program so that they can work in the same memory space. This is called "multi-thread processing ". A thread is more effective than a process because the operating system does not have to create a separate memory space for each thread.

2. Create a process using the OS. fork function. However, it is only available on the POSIX system. In python of windows, the OS module does not define the OS. fork function. On the contrary, windows programmers use multi-threaded programming technology to complete concurrent tasks.

3. the OS. fork function creates a process like this. Each time a program is executed, the operating system creates a new process to run program commands. You can also call OS. fork to create a new process in the operating system. The parent process is the process that calls the OS. fork function. The process created by the parent process is a child process. Each process has a unique process ID. Or pid, which identifies the process. The child process is exactly the same as the parent process. The child process inherits copies of multiple values from the parent process, such as global variables and environment variables. The only difference between the two processes is the return value of fork. The child process receives the return value 0, while the parent process receives the pid of the child process as the return value.

4. The sub-process and parent process created by OS. fork are executed separately as asynchronous concurrent processes. Asynchronous means that they operate independently and do not synchronize with each other; concurrency means that they can be executed simultaneously. Therefore, we cannot know the relative speed of the child process and the parent process.

5. the OS. wait function is used to wait for the sub-process to end (only applicable to UNIX-compatible systems ). This function returns the tuples containing two elements, including the completed child process ID and the exit status of the child process. The return status is 0, indicating that the child process has completed successfully. The returned status is a positive integer indicating that the sub-process has an error at termination. If no sub-process exists, an OSError error is thrown. OS. wait requires the parent process to wait for any of its child processes to end and then wake up the parent process.

6. To instruct the parent process to wait for a specified child process to terminate, you can use the OS. waitpid function in the parent process (applicable only to unix-compatible systems ). It can wait for a specified process to end, and then return a dual-element tuples, including the Child process pid and the child process exit status. The function calls the pid as the first parameter, and uses an option as the second option. If the first parameter is greater than 0, the waitpid waits for the pid to end, if the first parameter is-1, the system waits for all sub-processes. same as wait.

7. Use the OS. system and OS .exe c Function Families to execute system commands and other programs. OS .systemuse shellto execute the system command. After the command is executed, the control right is returned to the original program OS .exe c function family. After the command is executed, the control is not returned to the calling process. It takes over the python process, and the pid remains unchanged. These two functions support unix and windows platforms.

8. The OS. popen () function can execute the command and obtain the stdout stream of the command. The function requires two parameters: one is the command to be executed, and the other is the mode used to call the function, such as the read-only mode of "r. The OS. popen2 () function executes the command and obtains the stdout stream and stdin stream of the command. The function returns a tuple containing two file objects. One object corresponds to the stdin stream and the other object corresponds to the stdout stream.

9. The process uses the IPC Mechanism to transmit information between processes. An IPC Mechanism is a "Pipeline", which is similar to a file object and provides one-way communication channels. A parent process can open a pipeline and branch a child process. The parent process writes (sends) Information to) the child process, and the child process uses the MPs queue to read information from the parent process. Use the OS. pipe function in python to create pipelines.

10. OS. _ exit () is similar to sys. exit (), but it does not perform any cleanup (such as refreshing the buffer ). Therefore, OS. _ exit () is especially suitable for exiting sub-processes. If the program uses sys. exit (), the operating system recycles resources that the parent process or other child processes may still need. The parameter passed to the OS. _ exit () function must be the exit status of the process. The exit status is 0, indicating normal termination.

11. processes can also communicate with each other using signals. The so-called "signal" refers to the message that the operating system sends to the program asynchronously. For example, CTRL + C transmits an "interruption signal", which usually causes the program to stop. However, the program can specify different actions to respond to any signal. In signal processing, the program receives the signal and takes an action based on the signal. Errors (for example, writing to a closed pipeline), events (for example, the timer turns to 0), and user input (for example, pressing ctrl + c) generate signals.

12. Each python program has a default signal processing program for each signal. For example, if the python interpreter receives a signal indicating that the program is trying to write data to a closed pipeline, or the user clicks a keyboard interrupt, python will cause an exception. When an exception occurs, you can use either the default or Custom Handler.

13. The signal. signal function registers a signal handler for the interrupt signal. The function requires two parameters: a signal and a function corresponding to the signal processing program.

14. In unix/linux systems, after the sub-process is terminated, it will be kept in the progress table to let the parent process know whether the sub-process is terminated normally. If you create large quantum processes but do not remove them from the Progress table after termination, the progress table will accumulate more and more dead processes, known as zombies ), the operation to remove zombie processes is called "reaping", which is implemented through the OS. wait and OS. waitpid function implementation.



18.2. Good Programming habits

1. The process should shut down unnecessary MPs queues because the operating system limits the number of file specifiers that can be opened at the same time.


18.3. Portability tips

1. Not all operating systems can create separate processes from a running program. Therefore, process management is one of the worst portability python features.

2. Each system defines a specific signal set. Signal is a platform-specific module that only contains the signals defined by the system.



Reference: http://blog.163.com/longsu2010@yeah/blog/static/1736123482011292132926/

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.