Python program Exit mode (Sys.exit () os._exit () Os.kill () Os.popen (...)

Source: Internet
Author: User

Python itself gives several methods for how to end a Python program or to end a process with Python, and there are some differences in how these methods are viewed and practiced, and also recorded.

Reference:

Python Core Programming (second edition)

http://www.zhihu.com/question/21187839

1. Sys.exit ()

Executing the statement exits the program, which is often used, and does not need to consider the impact of factors such as platforms, and is generally the preferred method of exiting a Python program.

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

1 Import SYS 2 sys.exit () 3 sys.exit (0) 4 sys.exit (1)

This method throws a Systemexit exception (which is the only exception that will not be considered an error), and when no setting catches the exception it will exit the program execution directly, and of course it can catch the exception for some other operation.

2. Os._exit ()

The effect is to exit directly without throwing an exception, but its use will be limited by the platform, but our common Win32 platform and Unix-based platform will not be affected.

It is known that the _exit () function (not verified) of the C language has been called.

3. Os.kill ()

Typically used to kill the process directly, but only on Unix platforms .

Rationale: The function is to simulate the traditional UNIX function signaling to the process , which contains two parameters: one is the process name, which is the process to receive the signal, and one is the operation to be performed.

Common values for operations (the second parameter) are:

SIGINT terminating process Interrupt process

SIGTERM terminating process software termination signal

SIGKILL Terminate process Kill process

SIGALRM Alarm Clock signal

Cases:

Open the VLC video player on the Linux platform and view the running process: The process number is 4497

Then perform the Os.kill operation:

After execution, you can find that VLC video player has been shut down and the process has been killed.

4. Windows Kill Process

Since this can be done under Linux, there is also a related operation under Windows.

Here is the os.popen (), which is used to execute the system commands directly , and in Windows is actually using Taskkill to kill the process, the basic form is that

PID number of the Taskkill/pid program

You can try this command directly under the CMD window ....

You can open a calculator program first, and then use Tasklist to view the program's PID, here is 620, so the corresponding Python code is:

1 Import OS 2 if __name__ " __main__ " : 3     PID = 6204     os.popen ('taskkill.exe/pid:'+str (PID))

Ok

Python program Exit mode (Sys.exit () os._exit () Os.kill () Os.popen (...)

Related Article

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.