python3.6sysos_python 中 os._exit(), sys.exit(), exit() 的区别是什么?-程序员宅基地

技术标签: python3.6sysos  

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

回答最下面有运行效果和源代码

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

static PyObject *

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;

}

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

Exit from Python. This is implemented by raising the

The optional argument arg can 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, None is equivalent to passing zero, and any other object is printed to sys.exit("some errormessage") is a quick way to exit a program when an error occurs.

Since

Changed in version 3.6: If an error occurs in the cleanup after the Python interpreter has caught

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

Note

The standard way to exit is sys.exit(n).

The following exit codes are defined and can be used with

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()

#代码如下,可以运行一下看效果

import os,sys

#%% os._exit(0)

try:

print('run: os._exit(0) # os._exit(n), 直接退出, 不抛异常, 不执行相关清理工作. 常用在子进程的退出')

os._exit(0) # os._exit(n), 直接退出, 不抛异常, 不执行相关清理工作. 常用在子进程的退出

except:

print('except:xiaoyao_os._exit(0)_知乎:小尧') # 不运行

finally:

print('xiaoyao_os._exit(0)quit_知乎:小尧') # 不运行

#%% sys.exit(n)

try:# 如果在 shell 里面用 try/except 包住这样写,实际上 shell 不会退出,因为 SystemExit 被 catch 了

sys.exit('run:xiaoyao_sys.exit(4)_知乎:小尧') # sys.exit(n) ,sys.exit(0)最终效果一样。sys.exit() 可以sys.exit("sorry, goodbye!"); 一般主程序中使用此退出.

except SystemExit:# 如果没有 except SystemExit ,程序就能正常退出。这边为了演示,加上 except SystemExit

print('except:xiaoyao_sys.exit(4)_知乎:小尧')

finally:

print('xiaoyao_sys.exit(4)quit_知乎:小尧')

#%% exit()/quit()

try:# 如果在 shell 里面用 try/except 包住这样写,实际上 shell 不会退出,因为 SystemExit 被 catch 了

quit() # exit()/quit(), 跑出SystemExit异常. 一般在交互式shell中退出时使用.

except SystemExit:# 如果没有 except SystemExit ,程序就能正常退出。这边为了演示,加上 except SystemExit

print('except:xiaoyao_exit()_知乎:小尧')

finally:

print('xiaoyao_exit()quit_知乎:小尧')

#%%

print('xiaoyao_能执行到这里?知乎小尧觉得不可能吧?')

'''python3运行环境:Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24)[Clang 6.0 (clang-600.0.57)] on darwin,知乎回答 written by 小尧Type "help", "copyright", "credits" or "license" for more information.退出代码是告诉解释器的(或操作系统)'''

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_39957318/article/details/110080709

智能推荐

bzoj3298-程序员宅基地

文章浏览阅读151次。分析:。。美国人民脑洞大。。。注意格式。。具体题解%http://www.bubuko.com/infodetail-740656.html#include#include#include#include#define fo(i,a,b) for(int i=a;i<=b;i++)using namespace s

L Machining Disc Rotors_l. machining disc rotors-程序员宅基地

文章浏览阅读242次。L Machining Disc Rotors题意:圆心为(0,0)半径为R的圆,现在被被n个互不相交的圆切割(圆心和半径会给出),保证这n个彼此之间不会交叉,保证n个圆中不会有某个包含整个大圆的情况。问切割后大圆剩余部分的直径(即两点的最远距离)题解:圆上的最远距离就是半径,如果存在一个点没被切割,然后求其关于圆心作对称点,如果对称点也存在就说明构成了一条没有被切掉的直径。如果直径不存在,答案就是两个交点之间的最大距离(即图中情况)这个题最难的是写代码。。。头大,计算几何一看就头大代码:_l. machining disc rotors

python 插入clickhouse数据报错_cannot parse json string: expected opening quote:-程序员宅基地

文章浏览阅读2.2k次。python 插入clickhouse报错 clickhouse : Cannot parse JSON string: expected opening quote: (while read the value of key data): (at row 1): While executing SourceFromInputStream 最近工作需要使用clickhouse_cannot parse json string: expected opening quote:

开源软件如何赚钱?_b) 公司如何从“免费”的开源软件中赚钱?-程序员宅基地

文章浏览阅读1.4w次,点赞9次,收藏13次。所谓开源就是开放源代码。源代码是软件的本质,所有程序都有源代码,就像人类的语言一样,有词汇和语法。源代码可以说是一个作者的主要命脉了。一般软件作者将软件的源代码开放出来,以保障软件用户自由使用及接触源代码的权利。这同时也保障了用户自行修改、复制以及_b) 公司如何从“免费”的开源软件中赚钱?

使用标准库:文本查询程序_用txt文档制作查询程序-程序员宅基地

文章浏览阅读168次。最近打算学习c++11 中智能指针的使用,写了一个文本查询程序。功能有读入一个“.txt”文件,在其中找单词“london”,具体格式见图://TextQuery.cpp#include "TextQuery.h"#include&lt;iostream&gt;#include&lt;fstream&gt;using namespace std;void runQueries(s..._用txt文档制作查询程序

单行文本省略和多行文本省略_实现单行文本缩略和多行文本省略-程序员宅基地

文章浏览阅读262次。单行文本省略overflow: hidden;text-overflow:ellipsis;white-space: nowrap;多行文本省略display: -webkit-box;-webkit-box-orient: vertical;-webkit-line-clamp: 3;overflow: hidden;_实现单行文本缩略和多行文本省略

随便推点

求某个节点的所有父节点_sqlserver根据子节点获取它对应的所有父节点-程序员宅基地

文章浏览阅读1.1k次。package com.fh.service.xtgl;import com.fh.util.PageData; import org.springframework.stereotype.Service;import javax.annotation.Resource; import java.util.ArrayList; import java.util.List;//根据_sqlserver根据子节点获取它对应的所有父节点

C# 自定义控件制作和使用实例_c# vs2017 自定义控件 开发流程-程序员宅基地

文章浏览阅读3.8w次,点赞16次,收藏75次。C# 自定义用户控件xiongxuanwen 上篇:控件制作 本例是制作一个简单的自定义控件,然后用一个简单的测试程序,对于初学者来说,本例子比较简单,只能起到抛石引玉的效果。我也是在学习当中,今后会将自己所学的逐步写出来和大家交流共享。 第一步:新建一个控件库项目:myControl 第二步:从工具箱里面拖动1个PictureBox、1个Button、6_c# vs2017 自定义控件 开发流程

maven五:查找jar包坐标,选择jar包版本_在一个maven工程中,如何快速判断一个jar的版本是在哪里定义的?-程序员宅基地

文章浏览阅读1.5w次,点赞9次,收藏14次。查找jar包坐标以spring core的jar包为例,访问http://www.mvnrepository.com/ 在最上方中间,输入spring core,点击Search。搜索结果第一个就是,点击spring core有很多版本,这里点击4.3.5.RELEASE点击maven栏里面的内容,允许访问,会复制到剪贴板然后直接粘贴到pom.xml文件的_在一个maven工程中,如何快速判断一个jar的版本是在哪里定义的?

linux卸载图形逻辑卷界面,使用lvremove命令在Linux系统中删除LVM卷(逻辑卷)-程序员宅基地

文章浏览阅读948次。如果Linux系统上的LVM不再需要使用LVM卷(逻辑卷),您可以使用lvremove命令按照以下步骤删除它。但是请确保LVM卷不包含任何数据,如果是,请确保在继续删除LVM之前备份该数据。为了说明这一点,我们将从卷组“vg01”中删除“lv001”,LV安装在挂载点/lvmtest上。参考lvremove命令_Linux lvremove命令使用详解:删除指定LVM逻辑卷。实施的方法使用df命令..._lvremove

最适合物联网LOT的开源数据库_lot 数据库有哪里-程序员宅基地

文章浏览阅读2.4k次。最适合物联网的开源数据库https://blog.csdn.net/shnbiot/article/details/80693520_lot 数据库有哪里

H - Fence-程序员宅基地

文章浏览阅读106次。H - FenceThere is a fence in front of Polycarpus’s home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights.Fence for n = 7

推荐文章

热门文章

相关标签