首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

python中os._exit,sys.exit,exit 的区别是什么?

退出方式不一样( exit()和sys.exit()会raiseSystemExit(code), os._exit()直接退出。exit()基本只是在IDLE和命令行中使用)。

在sysmodule.c中源代码是这样的(注意PyErr_SetObject(PyExc_SystemExit, exit_code);,这个操作相当于raiseSystemExit(code):

sys_exit(PyObject *self, PyObject *args)

{

PyObject *exit_code = 0;

if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code))

return NULL;

/* Raise SystemExit so callers may catch it or clean up. */

PyErr_SetObject(PyExc_SystemExit, exit_code);

return NULL;

}

再来看看官方英文版原版解释:

sys.exit([arg])

Exit from Python. This is implemented by raising the exception, so cleanup actions specified by finally clauses of statements are honored, and it is possible to intercept the exit attempt at an outer level.

The optional argumentargcan be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered “successful termination” and any nonzero value is considered “abnormal termination” by shells and the like. Most systems require it to be in the range 0–127, and produce undefined results otherwise. Some systems have a convention for assigning specific meanings to specific exit codes, but these are generally underdeveloped; Unix programs generally use 2 for command line syntax errors and 1 for all other kind of errors. If another type of object is passed, is equivalent to passing zero, and any other object is printed to and results in an exit code of 1. In particular, is a quick way to exit a program when an error occurs.

Since ultimately “only” raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.

Changed in version 3.6:If an error occurs in the cleanup after the Python interpreter has caught (such as an error flushing buffered data in the standard streams), the exit status is changed to 120.

os._exit(n)

Exit the process with statusn, without calling cleanup handlers, flushing stdio buffers, etc.

Note

The standard way to exit is . should normally only be used in the child process after a .

The following exit codes are defined and can be used with , although they are not required. These are typically used for system programs written in Python, such as a mail server’s external command delivery program.

Note

Some of these may not be available on all Unix platforms, since there is some variation. These constants are defined where they are defined by the underlying platform.

直接运行一下看效果(因为运行时把SystemExit给 catch 了,所以 Python 不会真正退出):

第一个:os._exit(0) ,os._exit()直接将python解释器退出,余下的语句不会执行。os._exit() 调用 C 语言的 _exit() 函数。相当于强制退出。

os._exit(0)

第二个:sys.exit(n) ,调用后会引发SystemExit异常,可以捕获此异常做清理工作。甚至可以阻止程序退出。

sys.exit(n)

第三个:exit()/quit(),这种实际上和sys.exit(n) 没有什么区别

exit()/quit()

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20190108G0S7LM00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券