{"id":1157523,"date":"2025-01-13T18:29:40","date_gmt":"2025-01-13T10:29:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1157523.html"},"modified":"2025-01-13T18:29:43","modified_gmt":"2025-01-13T10:29:43","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e7%bb%88%e6%ad%a2%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1157523.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u7ec8\u6b62\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25195706\/744d64df-e4e4-442b-8a52-6c9ffca86625.webp\" alt=\"python\u4e2d\u5982\u4f55\u7ec8\u6b62\u7a0b\u5e8f\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u7ec8\u6b62\u7a0b\u5e8f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528sys.exit()\u3001os._exit()\u3001r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se SystemExit\u3001\u4ee5\u53ca\u952e\u76d8\u4e2d\u65ad\u7b49\u3002\u5176\u4e2d\uff0csys.exit()\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002<\/strong> sys.exit()\u51fd\u6570\u4f1a\u629b\u51fa\u4e00\u4e2aSystemExit\u5f02\u5e38\uff0c\u53ef\u4ee5\u5728\u811a\u672c\u4e2d\u4efb\u4f55\u5730\u65b9\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u6765\u7ed3\u675f\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><p>sys.exit()\u65b9\u6cd5\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<p>sys.exit()\u51fd\u6570\u662fPython\u7684sys\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u9000\u51fa\u7a0b\u5e8f\u5e76\u8fd4\u56de\u6307\u5b9a\u7684\u72b6\u6001\u7801\u3002sys.exit()\u51fd\u6570\u901a\u8fc7\u5f15\u53d1SystemExit\u5f02\u5e38\u6765\u7ec8\u6b62\u7a0b\u5e8f\u7684\u6267\u884c\u3002\u8fd9\u4e2a\u5f02\u5e38\u53ef\u4ee5\u88ab\u6355\u83b7\u548c\u5904\u7406\uff0c\u4ece\u800c\u63d0\u4f9b\u66f4\u5927\u7684\u7075\u6d3b\u6027\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5728\u6355\u83b7\u5230\u8fd9\u4e2a\u5f02\u5e38\u65f6\u6267\u884c\u4e00\u4e9b\u6e05\u7406\u64cd\u4f5c\uff0c\u7136\u540e\u518d\u7ed3\u675f\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>def exit_program():<\/p>\n<p>    print(&quot;Exiting the program...&quot;)<\/p>\n<p>    sys.exit(0)<\/p>\n<p>try:<\/p>\n<p>    exit_program()<\/p>\n<p>except SystemExit:<\/p>\n<p>    print(&quot;SystemExit exception caught!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u8c03\u7528sys.exit(0)\u4f1a\u5f15\u53d1\u4e00\u4e2aSystemExit\u5f02\u5e38\uff0c\u7a0b\u5e8f\u4f1a\u6b63\u5e38\u9000\u51fa\u5e76\u8fd4\u56de\u72b6\u6001\u78010\u3002\u5982\u679c\u6355\u83b7\u5230\u8fd9\u4e2a\u5f02\u5e38\uff0c\u53ef\u4ee5\u5728\u6355\u83b7\u5230\u5f02\u5e38\u65f6\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001sys.exit()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>sys.exit()\u65b9\u6cd5\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ec8\u6b62\u7a0b\u5e8f\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u5728\u7a0b\u5e8f\u7684\u4efb\u4f55\u5730\u65b9\u8c03\u7528\uff0c\u5e76\u4e14\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u9000\u51fa\u72b6\u6001\u7801\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0csys.exit()\u4f1a\u8fd4\u56de\u72b6\u6001\u78010\uff0c\u8868\u793a\u7a0b\u5e8f\u6b63\u5e38\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>def main():<\/p>\n<p>    print(&quot;Running the main program...&quot;)<\/p>\n<p>    # Some logic here<\/p>\n<p>    print(&quot;Exiting the program...&quot;)<\/p>\n<p>    sys.exit(0)  # Exit with status code 0<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u8c03\u7528sys.exit(0)\u4f1a\u4f7f\u7a0b\u5e8f\u5728\u6267\u884c\u5230\u8fd9\u4e00\u884c\u65f6\u7ec8\u6b62\uff0c\u5e76\u8fd4\u56de\u72b6\u6001\u78010\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001os._exit()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>os._exit()\u65b9\u6cd5\u662fos\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u4f4e\u7ea7\u522b\u9000\u51fa\u51fd\u6570\u3002\u4e0esys.exit()\u4e0d\u540c\uff0cos._exit()\u4e0d\u4f1a\u5f15\u53d1SystemExit\u5f02\u5e38\uff0c\u800c\u662f\u76f4\u63a5\u7ec8\u6b62\u8fdb\u7a0b\u3002\u8fd9\u610f\u5473\u7740os._exit()\u4e0d\u4f1a\u6267\u884ctry&#8230;finally\u5757\u4e2d\u7684\u6e05\u7406\u64cd\u4f5c\uff0c\u4e5f\u4e0d\u4f1a\u8c03\u7528\u4efb\u4f55\u5df2\u6ce8\u518c\u7684atexit\u5904\u7406\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def main():<\/p>\n<p>    print(&quot;Running the main program...&quot;)<\/p>\n<p>    # Some logic here<\/p>\n<p>    print(&quot;Exiting the program...&quot;)<\/p>\n<p>    os._exit(0)  # Exit with status code 0<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u8c03\u7528os._exit(0)\u4f1a\u76f4\u63a5\u7ec8\u6b62\u8fdb\u7a0b\uff0c\u4e0d\u4f1a\u6267\u884c\u4efb\u4f55\u6e05\u7406\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001raise SystemExit<\/h3>\n<\/p>\n<p><p>raise SystemExit\u4e5f\u662f\u4e00\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u4e0esys.exit()\u7c7b\u4f3c\uff0craise SystemExit\u4f1a\u5f15\u53d1\u4e00\u4e2aSystemExit\u5f02\u5e38\uff0c\u53ef\u4ee5\u88ab\u6355\u83b7\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def main():<\/p>\n<p>    print(&quot;Running the main program...&quot;)<\/p>\n<p>    # Some logic here<\/p>\n<p>    print(&quot;Exiting the program...&quot;)<\/p>\n<p>    raise SystemExit(0)  # Exit with status code 0<\/p>\n<p>try:<\/p>\n<p>    main()<\/p>\n<p>except SystemExit:<\/p>\n<p>    print(&quot;SystemExit exception caught!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u8c03\u7528raise SystemExit(0)\u4f1a\u5f15\u53d1\u4e00\u4e2aSystemExit\u5f02\u5e38\uff0c\u7a0b\u5e8f\u4f1a\u6b63\u5e38\u9000\u51fa\u5e76\u8fd4\u56de\u72b6\u6001\u78010\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001KeyboardInterrupt<\/h3>\n<\/p>\n<p><p>\u5728\u4ea4\u4e92\u5f0f\u73af\u5883\u6216\u547d\u4ee4\u884c\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u952e\u76d8\u4e2d\u65ad\uff08\u901a\u5e38\u662fCtrl+C\uff09\u6765\u7ec8\u6b62\u7a0b\u5e8f\u3002\u8fd9\u4f1a\u5f15\u53d1\u4e00\u4e2aKeyboardInterrupt\u5f02\u5e38\uff0c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u6355\u83b7\u5e76\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def main():<\/p>\n<p>    try:<\/p>\n<p>        while True:<\/p>\n<p>            print(&quot;Running the main program... Press Ctrl+C to exit&quot;)<\/p>\n<p>    except KeyboardInterrupt:<\/p>\n<p>        print(&quot;KeyboardInterrupt caught! Exiting the program...&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u6309\u4e0bCtrl+C\u6765\u5f15\u53d1\u4e00\u4e2aKeyboardInterrupt\u5f02\u5e38\uff0c\u4ece\u800c\u7ec8\u6b62\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001atexit\u6a21\u5757<\/h3>\n<\/p>\n<p><p>atexit\u6a21\u5757\u5141\u8bb8\u4f60\u5728\u7a0b\u5e8f\u7ec8\u6b62\u65f6\u6267\u884c\u4e00\u4e9b\u6e05\u7406\u64cd\u4f5c\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528atexit.register()\u51fd\u6570\u6ce8\u518c\u4e00\u4e2a\u6e05\u7406\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u4f1a\u5728\u7a0b\u5e8f\u7ec8\u6b62\u65f6\u81ea\u52a8\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import atexit<\/p>\n<p>def cleanup():<\/p>\n<p>    print(&quot;Performing cleanup operations...&quot;)<\/p>\n<p>def main():<\/p>\n<p>    atexit.register(cleanup)<\/p>\n<p>    print(&quot;Running the main program...&quot;)<\/p>\n<p>    # Some logic here<\/p>\n<p>    print(&quot;Exiting the program...&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0ccleanup\u51fd\u6570\u4f1a\u5728\u7a0b\u5e8f\u7ec8\u6b62\u65f6\u81ea\u52a8\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7ebf\u7a0b\u4e2d\u7684\u7a0b\u5e8f\u7ec8\u6b62<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u4e2d\uff0c\u7ec8\u6b62\u4e3b\u7ebf\u7a0b\u4e0d\u4f1a\u81ea\u52a8\u7ec8\u6b62\u5176\u4ed6\u7ebf\u7a0b\u3002\u4f60\u9700\u8981\u663e\u5f0f\u5730\u7ec8\u6b62\u6240\u6709\u7ebf\u7a0b\uff0c\u6216\u8005\u7b49\u5f85\u5b83\u4eec\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def worker():<\/p>\n<p>    print(&quot;Worker thread is running...&quot;)<\/p>\n<p>    time.sleep(5)<\/p>\n<p>    print(&quot;Worker thread is exiting...&quot;)<\/p>\n<p>def main():<\/p>\n<p>    thread = threading.Thread(target=worker)<\/p>\n<p>    thread.start()<\/p>\n<p>    print(&quot;Main program is exiting...&quot;)<\/p>\n<p>    thread.join()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4e3b\u7ebf\u7a0b\u4f1a\u7b49\u5f85worker\u7ebf\u7a0b\u5b8c\u6210\u540e\u518d\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u5f02\u5e38\u7c7b\u6765\u7ec8\u6b62\u7a0b\u5e8f\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u63d0\u4f9b\u66f4\u597d\u7684\u53ef\u8bfb\u6027\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">class TerminateProgram(Exception):<\/p>\n<p>    pass<\/p>\n<p>def main():<\/p>\n<p>    try:<\/p>\n<p>        print(&quot;Running the main program...&quot;)<\/p>\n<p>        # Some logic here<\/p>\n<p>        raise TerminateProgram(&quot;Terminating the program...&quot;)<\/p>\n<p>    except TerminateProgram as e:<\/p>\n<p>        print(e)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7bTerminateProgram\uff0c\u5e76\u5728\u4e3b\u7a0b\u5e8f\u4e2d\u5f15\u53d1\u8be5\u5f02\u5e38\u6765\u7ec8\u6b62\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u4fe1\u53f7\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5904\u7406\u64cd\u4f5c\u7cfb\u7edf\u4fe1\u53f7\uff08\u5982SIGTERM\u6216SIGINT\uff09\u6765\u7ec8\u6b62\u7a0b\u5e8f\u3002\u53ef\u4ee5\u4f7f\u7528signal\u6a21\u5757\u6765\u6355\u83b7\u5e76\u5904\u7406\u8fd9\u4e9b\u4fe1\u53f7\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import signal<\/p>\n<p>import sys<\/p>\n<p>def signal_handler(sig, frame):<\/p>\n<p>    print(&quot;Signal received: &quot;, sig)<\/p>\n<p>    sys.exit(0)<\/p>\n<p>def main():<\/p>\n<p>    signal.signal(signal.SIGINT, signal_handler)<\/p>\n<p>    signal.signal(signal.SIGTERM, signal_handler)<\/p>\n<p>    print(&quot;Running the main program... Press Ctrl+C to exit&quot;)<\/p>\n<p>    while True:<\/p>\n<p>        pass<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4fe1\u53f7\u5904\u7406\u7a0b\u5e8fsignal_handler\u4f1a\u5728\u63a5\u6536\u5230SIGINT\u6216SIGTERM\u4fe1\u53f7\u65f6\u7ec8\u6b62\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u9000\u51faGUI\u5e94\u7528\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u5728GUI\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7279\u5b9a\u7684\u5e93\u51fd\u6570\u6765\u7ec8\u6b62\u7a0b\u5e8f\u3002\u4f8b\u5982\uff0c\u5728Tkinter\u4e2d\uff0c\u4f60\u53ef\u4ee5\u8c03\u7528root.quit()\u6765\u7ec8\u6b62\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff08Tkinter\uff09\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def exit_program():<\/p>\n<p>    print(&quot;Exiting the program...&quot;)<\/p>\n<p>    root.quit()<\/p>\n<p>root = tk.Tk()<\/p>\n<p>button = tk.Button(root, text=&quot;Exit&quot;, command=exit_program)<\/p>\n<p>button.pack()<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u70b9\u51fb\u6309\u94ae\u4f1a\u8c03\u7528exit_program\u51fd\u6570\uff0c\u4ece\u800c\u7ec8\u6b62\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u9000\u51faWeb\u5e94\u7528\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u5728Web\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u7ec8\u6b62\u7a0b\u5e8f\u901a\u5e38\u6d89\u53ca\u505c\u6b62Web\u670d\u52a1\u5668\u3002\u4f8b\u5982\uff0c\u5728Flask\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u6765\u505c\u6b62\u670d\u52a1\u5668\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff08Flask\uff09\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def hello():<\/p>\n<p>    return &quot;Hello, World!&quot;<\/p>\n<p>@app.route(&#39;\/shutdown&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def shutdown():<\/p>\n<p>    shutdown_server()<\/p>\n<p>    return &quot;Server shutting down...&quot;<\/p>\n<p>def shutdown_server():<\/p>\n<p>    func = request.environ.get(&#39;werkzeug.server.shutdown&#39;)<\/p>\n<p>    if func is None:<\/p>\n<p>        raise RuntimeError(&quot;Not running with the Werkzeug Server&quot;)<\/p>\n<p>    func()<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u8bbf\u95ee\/shutdown\u8def\u7531\u4f1a\u8c03\u7528shutdown_server\u51fd\u6570\uff0c\u4ece\u800c\u505c\u6b62\u670d\u52a1\u5668\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u9000\u51fa\u811a\u672c\u5e76\u8fd4\u56de\u72b6\u6001\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5728\u9000\u51fa\u811a\u672c\u65f6\u8fd4\u56de\u7279\u5b9a\u7684\u72b6\u6001\u7801\u3002\u53ef\u4ee5\u4f7f\u7528sys.exit()\u6216os._exit()\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>def main():<\/p>\n<p>    print(&quot;Running the main program...&quot;)<\/p>\n<p>    # Some logic here<\/p>\n<p>    print(&quot;Exiting the program with status code 1...&quot;)<\/p>\n<p>    sys.exit(1)  # Exit with status code 1<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u8c03\u7528sys.exit(1)\u4f1a\u4f7f\u7a0b\u5e8f\u5728\u6267\u884c\u5230\u8fd9\u4e00\u884c\u65f6\u7ec8\u6b62\uff0c\u5e76\u8fd4\u56de\u72b6\u6001\u78011\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u9000\u51fa\u5b50\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u8fdb\u7a0b\u7a0b\u5e8f\u4e2d\uff0c\u7ec8\u6b62\u4e3b\u8fdb\u7a0b\u4e0d\u4f1a\u81ea\u52a8\u7ec8\u6b62\u5b50\u8fdb\u7a0b\u3002\u4f60\u9700\u8981\u663e\u5f0f\u5730\u7ec8\u6b62\u6240\u6709\u5b50\u8fdb\u7a0b\uff0c\u6216\u8005\u7b49\u5f85\u5b83\u4eec\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>import time<\/p>\n<p>def worker():<\/p>\n<p>    print(&quot;Worker process is running...&quot;)<\/p>\n<p>    time.sleep(5)<\/p>\n<p>    print(&quot;Worker process is exiting...&quot;)<\/p>\n<p>def main():<\/p>\n<p>    process = multiprocessing.Process(target=worker)<\/p>\n<p>    process.start()<\/p>\n<p>    print(&quot;Main program is exiting...&quot;)<\/p>\n<p>    process.join()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4e3b\u8fdb\u7a0b\u4f1a\u7b49\u5f85worker\u5b50\u8fdb\u7a0b\u5b8c\u6210\u540e\u518d\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u9000\u51fa\u5f02\u6b65\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u5728\u5f02\u6b65\u7a0b\u5e8f\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528asyncio\u6a21\u5757\u63d0\u4f9b\u7684\u65b9\u6cd5\u6765\u7ec8\u6b62\u4e8b\u4ef6\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def main():<\/p>\n<p>    print(&quot;Running the main program...&quot;)<\/p>\n<p>    await asyncio.sleep(1)<\/p>\n<p>    print(&quot;Exiting the program...&quot;)<\/p>\n<p>    loop = asyncio.get_running_loop()<\/p>\n<p>    loop.stop()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u8c03\u7528loop.stop()\u4f1a\u7ec8\u6b62\u4e8b\u4ef6\u5faa\u73af\uff0c\u4ece\u800c\u7ec8\u6b62\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u4f7f\u7528\u9000\u51fa\u6807\u5fd7<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5168\u5c40\u6807\u5fd7\u6765\u63a7\u5236\u7a0b\u5e8f\u7684\u9000\u51fa\u3002\u8fd9\u79cd\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u9700\u8981\u5728\u591a\u4e2a\u5730\u65b9\u68c0\u67e5\u9000\u51fa\u6761\u4ef6\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">exit_flag = False<\/p>\n<p>def main():<\/p>\n<p>    global exit_flag<\/p>\n<p>    while not exit_flag:<\/p>\n<p>        print(&quot;Running the main program... Press Ctrl+C to exit&quot;)<\/p>\n<p>        try:<\/p>\n<p>            time.sleep(1)<\/p>\n<p>        except KeyboardInterrupt:<\/p>\n<p>            exit_flag = True<\/p>\n<p>    print(&quot;Exiting the program...&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cexit_flag\u7528\u4e8e\u63a7\u5236\u7a0b\u5e8f\u7684\u9000\u51fa\uff0c\u5f53\u7528\u6237\u6309\u4e0bCtrl+C\u65f6\uff0c\u5c06exit_flag\u8bbe\u7f6e\u4e3aTrue\uff0c\u4ece\u800c\u7ec8\u6b62\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u9000\u51fa\u5e26\u6709\u540e\u53f0\u4efb\u52a1\u7684\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u5728\u5e26\u6709\u540e\u53f0\u4efb\u52a1\u7684\u7a0b\u5e8f\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u786e\u4fdd\u6240\u6709\u540e\u53f0\u4efb\u52a1\u5728\u7a0b\u5e8f\u9000\u51fa\u524d\u6b63\u786e\u5730\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def background_task():<\/p>\n<p>    while not exit_flag:<\/p>\n<p>        print(&quot;Background task is running...&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>    print(&quot;Background task is exiting...&quot;)<\/p>\n<p>exit_flag = False<\/p>\n<p>def main():<\/p>\n<p>    global exit_flag<\/p>\n<p>    thread = threading.Thread(target=background_task)<\/p>\n<p>    thread.start()<\/p>\n<p>    try:<\/p>\n<p>        while not exit_flag:<\/p>\n<p>            print(&quot;Main program is running... Press Ctrl+C to exit&quot;)<\/p>\n<p>            time.sleep(1)<\/p>\n<p>    except KeyboardInterrupt:<\/p>\n<p>        exit_flag = True<\/p>\n<p>    thread.join()<\/p>\n<p>    print(&quot;Exiting the program...&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cexit_flag\u7528\u4e8e\u63a7\u5236\u7a0b\u5e8f\u7684\u9000\u51fa\uff0c\u5e76\u786e\u4fdd\u6240\u6709\u540e\u53f0\u4efb\u52a1\u5728\u7a0b\u5e8f\u9000\u51fa\u524d\u6b63\u786e\u5730\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<p>Python \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u7ec8\u6b62\u7a0b\u5e8f\uff0c\u5305\u62ec\u4f7f\u7528sys.exit()\u3001os._exit()\u3001raise SystemExit\u3001\u6355\u83b7KeyboardInterrupt\u5f02\u5e38\u3001\u4ee5\u53ca\u4f7f\u7528atexit\u6a21\u5757\u7b49\u3002\u6839\u636e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u7ec8\u6b62\u7a0b\u5e8f\u3002\u5728\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u6216\u5f02\u6b65\u7a0b\u5e8f\u4e2d\uff0c\u8fd8\u9700\u8981\u8003\u8651\u5982\u4f55\u6b63\u786e\u5730\u7ec8\u6b62\u6240\u6709\u5b50\u7ebf\u7a0b\u3001\u5b50\u8fdb\u7a0b\u6216\u5f02\u6b65\u4efb\u52a1\u3002\u901a\u8fc7\u5408\u7406\u5730\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u5728\u9700\u8981\u65f6\u6b63\u786e\u5730\u7ec8\u6b62\uff0c\u5e76\u6267\u884c\u5fc5\u8981\u7684\u6e05\u7406\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f18\u96c5\u5730\u7ec8\u6b62\u7a0b\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f18\u96c5\u5730\u7ec8\u6b62\u7a0b\u5e8f\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>sys.exit()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u5f15\u53d1\u4e00\u4e2a<code>SystemExit<\/code>\u5f02\u5e38\uff0c\u4f7f\u7a0b\u5e8f\u53ef\u4ee5\u5728\u9002\u5f53\u7684\u65f6\u673a\u6e05\u7406\u8d44\u6e90\u5e76\u9000\u51fa\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528<code>try...except<\/code>\u7ed3\u6784\u6765\u6355\u83b7\u5f02\u5e38\uff0c\u5e76\u5728\u9700\u8981\u65f6\u5b89\u5168\u5730\u7ec8\u6b62\u7a0b\u5e8f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u662f\u5426\u53ef\u4ee5\u5f3a\u5236\u7ec8\u6b62\u6b63\u5728\u8fd0\u884c\u7684\u7a0b\u5e8f\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u63d0\u4f9b\u4e86<code>os._exit()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5f3a\u5236\u7ec8\u6b62\u7a0b\u5e8f\u7684\u6267\u884c\u3002\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u65f6\uff0c\u7a0b\u5e8f\u4e0d\u4f1a\u6267\u884c\u4efb\u4f55\u6e05\u7406\u64cd\u4f5c\uff0c\u56e0\u6b64\u901a\u5e38\u4e0d\u5efa\u8bae\u5728\u6b63\u5e38\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002\u52a1\u5fc5\u8c28\u614e\uff0c\u56e0\u4e3a\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8d44\u6e90\u672a\u91ca\u653e\u6216\u6587\u4ef6\u672a\u4fdd\u5b58\u7b49\u95ee\u9898\u3002<\/p>\n<p><strong>\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u6211\u5e94\u8be5\u8003\u8651\u4f7f\u7528<code>raise SystemExit<\/code>\u6765\u7ed3\u675f\u7a0b\u5e8f\uff1f<\/strong><br \/>\u5f53\u9700\u8981\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u6216\u9519\u8bef\u60c5\u51b5\u4e3b\u52a8\u7ec8\u6b62\u7a0b\u5e8f\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>raise SystemExit<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u4f60\u5728\u7a0b\u5e8f\u4e2d\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u5e76\u63d0\u4f9b\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u9000\u51fa\u72b6\u6001\u7801\uff0c\u8fd9\u6837\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u7a0b\u5e8f\u7684\u9000\u51fa\u884c\u4e3a\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u5728\u5f02\u5e38\u5904\u7406\u673a\u5236\u4e2d\u5904\u7406\u6e05\u7406\u64cd\u4f5c\u3002<\/p>\n<p><strong>Python\u7a0b\u5e8f\u5728\u9047\u5230\u9519\u8bef\u65f6\uff0c\u5982\u4f55\u81ea\u52a8\u7ec8\u6b62\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u7ed3\u6784\u6765\u6355\u83b7\u5f02\u5e38\uff0c\u5e76\u5728\u6355\u83b7\u5230\u7279\u5b9a\u5f02\u5e38\u65f6\u8c03\u7528<code>sys.exit()<\/code>\u6765\u7ec8\u6b62\u7a0b\u5e8f\u3002\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u4f60\u5728\u6355\u83b7\u9519\u8bef\u7684\u540c\u65f6\uff0c\u63d0\u4f9b\u9002\u5f53\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u786e\u4fdd\u7528\u6237\u4e86\u89e3\u7a0b\u5e8f\u4e3a\u4f55\u7ec8\u6b62\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u7ec8\u6b62\u7a0b\u5e8f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528sys.exit()\u3001os._exit()\u3001rAIse Sys [&hellip;]","protected":false},"author":3,"featured_media":1157532,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1157523"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1157523"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1157523\/revisions"}],"predecessor-version":[{"id":1157533,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1157523\/revisions\/1157533"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1157532"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1157523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1157523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1157523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}