Complete parsing of Python Processes

Source: Internet
Author: User

Next, let's take a deeper and closer look at the Python process. First, let's take a look at what the Python process is and what issues you should pay attention to when processing the Python process. Next, I will introduce and learn for you only.

However, although processes can be executed in a separate memory space, these Python processes do not run 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. 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.

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 and the parent process are identical. 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.

The child process and parent process created using 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.

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 sub-process ID pid. And the exit status of the sub-process. The return status is 0, indicating that the sub-process is successfully completed. 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. 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. Use one option as the second option. If the first parameter is greater than 0, waitpid will wait for the pid to end. If the first parameter is-1, it will wait for all sub-processes, and OS. same as wait.

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.

The OS. popen () function can execute commands 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. For example, "r" read-only mode. 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.

Processes use the IPC Mechanism to transmit information between processes. An IPC Mechanism is a "Pipeline", which is a file-like object that 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.

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.

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. For each signal, each python program has a default signal processing program, not all operating systems.

You can create separate processes from a running program. Therefore, process management is the least portable Python process feature. Each system defines a specific signal set. Signal is a platform-specific module that only contains the signals defined by the system.

  1. Python source code compilation skills
  2. Simple and Easy-to-use Python tools
  3. Introduction to the Python application field
  4. Python Android Object-Oriented Programming-Python applications
  5. How to Use the Python module to parse the configuration file?

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.