{"id":139891,"date":"2019-03-26T08:26:30","date_gmt":"2019-03-26T00:26:30","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=139891"},"modified":"2019-03-18T10:27:27","modified_gmt":"2019-03-18T02:27:27","slug":"python-exit-exit","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-exit-exit.html","title":{"rendered":"python\u4e2d os._exit() \u548c sys.exit(), exit(0)\u7684\u7528\u6cd5\u548c\u533a\u522b"},"content":{"rendered":"
os._exit() \u548c sys.exit()<\/strong><\/div>\n

os._exit() vs sys.exit()<\/p>\n

\u6982\u8ff0<\/strong><\/span><\/div>\n

Python\u7684\u7a0b\u5e8f\u6709\u4e24\u4e2d\u9000\u51fa\u65b9\u5f0f\uff1aos._exit()\uff0c sys.exit()\u3002\u672c\u6587\u4ecb\u7ecd\u8fd9\u4e24\u79cd\u65b9\u5f0f\u7684\u533a\u522b\u548c\u9009\u62e9\u3002
\nos._exit()\u4f1a\u76f4\u63a5\u5c06python\u7a0b\u5e8f\u7ec8\u6b62\uff0c\u4e4b\u540e\u7684\u6240\u6709\u4ee3\u7801\u90fd\u4e0d\u4f1a\u7ee7\u7eed\u6267\u884c\u3002
\nsys.exit()\u4f1a\u5f15\u53d1\u4e00\u4e2a\u5f02\u5e38\uff1aSystemExit\uff0c\u5982\u679c\u8fd9\u4e2a\u5f02\u5e38\u6ca1\u6709\u88ab\u6355\u83b7\uff0c\u90a3\u4e48python\u89e3\u91ca\u5668\u5c06\u4f1a\u9000\u51fa\u3002\u5982\u679c\u6709\u6355\u83b7\u6b64\u5f02\u5e38\u7684\u4ee3\u7801\uff0c\u90a3\u4e48\u8fd9\u4e9b\u4ee3\u7801\u8fd8\u662f\u4f1a\u6267\u884c\u3002\u6355\u83b7\u8fd9\u4e2a\u5f02\u5e38\u53ef\u4ee5\u505a\u4e00\u4e9b\u989d\u5916\u7684\u6e05\u7406\u5de5\u4f5c\u30020\u4e3a\u6b63\u5e38\u9000\u51fa\uff0c\u5176\u4ed6\u6570\u503c\uff081-127\uff09\u4e3a\u4e0d\u6b63\u5e38\uff0c\u53ef\u629b\u5f02\u5e38\u4e8b\u4ef6\u4f9b\u6355\u83b7\u3002<\/p>\n

\u4e3e\u4f8b\u8bf4\u660e<\/p>\n

1 import os\r\n2 \r\n3 try:\r\n4     os._exit(0)\r\n5 except:\r\n6     print 'die.'<\/span><\/span><\/span><\/code><\/pre>\n

\u6b64\u5904\u4e0d\u4f1a\u6253\u51fa\u201dgoing to die\u201d<\/p>\n

import sys\r\n\r\ntry:\r\n    sys.exit(0)\r\nexcept:\r\n    print 'die'\r\nfinally:\r\n    print 'cleanup'<\/span><\/span><\/span><\/code><\/pre>\n

\u8f93\u51fa\uff1a<\/p>\n

die\r\ncleanup<\/span><\/code><\/pre>\n
\u533a\u522b<\/strong><\/span><\/div>\n

\u7efc\u4e0a\uff0csys.exit()\u7684\u9000\u51fa\u6bd4\u8f83\u4f18\u96c5\uff0c\u8c03\u7528\u540e\u4f1a\u5f15\u53d1SystemExit\u5f02\u5e38\uff0c\u53ef\u4ee5\u6355\u83b7\u6b64\u5f02\u5e38\u505a\u6e05\u7406\u5de5\u4f5c\u3002os._exit()\u76f4\u63a5\u5c06python\u89e3\u91ca\u5668\u9000\u51fa\uff0c\u4f59\u4e0b\u7684\u8bed\u53e5\u4e0d\u4f1a\u6267\u884c\u3002<\/p>\n

\u4e00\u822c\u60c5\u51b5\u4e0b\u4f7f\u7528sys.exit()\u5373\u53ef\uff0c\u4e00\u822c\u5728fork\u51fa\u6765\u7684\u5b50\u8fdb\u7a0b\u4e2d\u4f7f\u7528os._exit()<\/p>\n

\u4e00\u822c\u6765\u8bf4os._exit() \u7528\u4e8e\u5728\u7ebf\u7a0b\u4e2d\u9000\u51fa<\/strong>
\nsys.exit() \u7528\u4e8e\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u9000\u51fa\u3002<\/strong><\/p>\n

exit() \u8ddf C \u8bed\u8a00\u7b49\u5176\u4ed6\u8bed\u8a00\u7684 exit() \u5e94\u8be5\u662f\u4e00\u6837\u7684\u3002
\nos._exit() \u8c03\u7528 C \u8bed\u8a00\u7684 _exit() \u51fd\u6570\u3002<\/p>\n

builtin<\/strong>.exit \u662f\u4e00\u4e2a Quitter \u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u7684\u00a0call<\/strong>\u00a0\u65b9\u6cd5\u4f1a\u629b\u51fa\u4e00\u4e2a SystemExit \u5f02\u5e38\u3002<\/p>\n


\n
exit(0)\u548cexit(1)<\/strong><\/div>\n

exit(0)\uff1a\u65e0\u9519\u8bef\u9000\u51fa
\nexit(1)\uff1a\u6709\u9519\u8bef\u9000\u51fa
\n\u9000\u51fa\u4ee3\u7801\u662f\u544a\u8bc9\u89e3\u91ca\u5668\u7684\uff08\u6216\u64cd\u4f5c\u7cfb\u7edf\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"

os._exit() vs sys.exit() Python\u7684\u7a0b\u5e8f\u6709\u4e24\u4e2d\u9000\u51fa\u65b9\u5f0f\uff1aos._exit()\uff0c s […]<\/p>\n","protected":false},"author":643,"featured_media":139900,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-139891","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/139891","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/643"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=139891"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/139891\/revisions"}],"predecessor-version":[{"id":139987,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/139891\/revisions\/139987"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/139900"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=139891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=139891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=139891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}