Home  >  Article  >  Backend Development  >  What are the ways to end a process in Python?

What are the ways to end a process in Python?

silencement
silencementOriginal
2019-06-11 15:26:137795browse

What are the ways to end a process in Python?

How python exits the program

Python program exits [sys.exit() os._exit() os.kill() os .popen(...)]

1. sys.exit()

Executing this statement will directly exit the program. This is also a commonly used method, and there is no need to consider factors such as the platform. Impact, generally the preferred method to exit a Python program.

This method contains a parameter status, which defaults to 0, indicating a normal exit, or 1, indicating an abnormal exit.

import sys
sys.exit()
sys.exit(0)
sys.exit(1)

This The method raises a SystemExit exception (this is the only exception that is not considered an error). If this exception is not set to be caught, the program execution will be exited directly. Of course, this exception can also be caught to perform other operations.

2. os._exit()

The effect is to exit directly without throwing an exception, but its use will be restricted by the platform, but our commonly used Win32 platform and UNIX-based platform will not have any effect. Influence.

3. os.kill()

is generally used to kill the process directly, but it is only effective on UNIX platforms.

Basic principle: This function simulates the traditional UNIX function to send a signal to the process, which contains two parameters: one is the process name, that is, the process to receive the signal; the other is the operation to be performed.

python

The above is the detailed content of What are the ways to end a process in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn