Core programming of python Python program exits way Summary

This article describes the Python program exits summary way, with a certain reference value, need friends can understand the next.
For how to end a Python program in Python or operation to end a process and so on, Python itself gives several ways, and these ways there are also some differences, related to several methods looked at and practiced, but also record.

reference:

Core Python Programming (Second Edition) Chinese HD

  1. sys.exit()

Execute the statement will exit the program, which is also frequently used method, does not need to consider factors such as platforms, Python is generally the preferred method exits the program.

The method comprises a Status parameter, the default is 0, indicating a normal exit may be 1, indicating abnormal exit.

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

This method is triggered by a SystemExit anomaly (which is the only one that will not be considered an error exception), if not set catch this exception will exit the program execution, of course, you can catch this exception some other operations.

  1. os._exit ()

The effect is to exit, no exception is thrown, but their use is limited by platform, but we often do not have an impact on the Win32 platform and UNIX-based platforms.

It is known to have said that the call _exit C language () function (not verified) on peace

  1. os.kill()

Kill off process is generally used directly, but only valid on UNIX platforms.

Rationale: This function is a function of traditional UNIX analog signal to a process, which contains two parameters: a name is the process, i.e. the process to the received signal; a is an operation to be performed.

Operation (the second parameter) value of common:

SIGINT terminate process interrupt process

SIGTERM terminate process software termination signal

SIGKILL Terminate the process to kill the process

Alarm SIGALRM letter Example:

Opened in linux platform VLC video player, and then view the processes running: process ID number is 4497
! [** Here Insert Picture Description
and then do os.kill operation: Here Insert Picture Description
After execution can be found in VLC video player has been closed, but the process has also been Kill a.

4.Windows under Kill Process

Since it is possible to carry out such operations under Linux, then under Windows you can have related operations.

As used herein, is os.popen (), which is used to directly execute system commands, and in fact, use Windows taskkill to kill off the process, in its basic form is,

PID number taskkill / pid program

In the CMD window can try the direct command ...

Might want to open a calculator program, then use tasklist to see the program's pid, here is the 620, the corresponding Python code is:

mport os
if __name__ == "__main__":
pid = 620
os.popen('taskkill.exe /pid:'+str(pid))

We recommend the python learning sites to see how seniors are learning! From basic python script, reptiles, django, data mining, programming techniques, as well as to combat zero-based sorting data items, given to every love learning python small partner! Python veteran day have to explain the timing of technology, to share some of the ways to learn and need to pay attention to small details, click to join our [python学习者聚集地](https://jq.qq.com/?_wv=1027&k=5JIjRvv)
summary

That's all for this article about Python program exits summary of the way, we hope to help

Published 51 original articles · won praise 122 · views 80000 +

Guess you like

Origin blog.csdn.net/haoxun03/article/details/104348554