{"id":1172627,"date":"2025-01-15T16:52:03","date_gmt":"2025-01-15T08:52:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1172627.html"},"modified":"2025-01-15T16:52:07","modified_gmt":"2025-01-15T08:52:07","slug":"python%e5%a6%82%e4%bd%95%e5%bc%ba%e5%88%b6%e5%85%b3%e9%97%ad%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1172627.html","title":{"rendered":"python\u5982\u4f55\u5f3a\u5236\u5173\u95ed\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26074635\/132e2a32-085d-418a-9a69-d2151e501735.webp\" alt=\"python\u5982\u4f55\u5f3a\u5236\u5173\u95ed\u7a0b\u5e8f\" \/><\/p>\n<p><p> <strong>Python\u5f3a\u5236\u5173\u95ed\u7a0b\u5e8f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528<code>sys.exit()<\/code>\u3001<code>os._exit()<\/code>\u3001<code>signal<\/code>\u6a21\u5757\u3001<code>threading<\/code>\u6a21\u5757\u548c\u8fdb\u7a0b\u7ba1\u7406\u5de5\u5177\u7b49<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u662f\u901a\u8fc7<code>sys.exit()<\/code>\u51fd\u6570\u6765\u9000\u51fa\u5f53\u524d\u7a0b\u5e8f\u3002<strong><code>sys.exit()<\/code>\u80fd\u5e72\u51c0\u5730\u5173\u95ed\u7a0b\u5e8f\u3001<code>os._exit()<\/code>\u4f1a\u76f4\u63a5\u7ec8\u6b62\u8fdb\u7a0b\u3001\u4f7f\u7528\u4fe1\u53f7\u548c\u7ebf\u7a0b\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u7ba1\u7406\u7a0b\u5e8f\u9000\u51fa<\/strong>\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u5f3a\u5236\u5173\u95edPython\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>sys.exit()<\/code><\/h3>\n<\/p>\n<p><p><code>sys.exit()<\/code>\u662f\u4e00\u4e2a\u6807\u51c6\u5e93\u51fd\u6570\uff0c\u7528\u4e8e\u9000\u51fa\u5f53\u524d\u7a0b\u5e8f\u5e76\u8fd4\u56de\u4e00\u4e2a\u72b6\u6001\u7801\u3002\u5b83\u80fd\u591f\u5e72\u51c0\u5730\u5173\u95ed\u7a0b\u5e8f\uff0c\u6267\u884c\u6e05\u7406\u64cd\u4f5c\uff0c\u5982\u5173\u95ed\u6587\u4ef6\u3001\u91ca\u653e\u8d44\u6e90\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    print(&quot;Starting program&quot;)<\/p>\n<p>    sys.exit(0)  # 0\u8868\u793a\u6b63\u5e38\u9000\u51fa<\/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\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u6253\u5370\u201cStarting program\u201d\uff0c\u7136\u540e\u901a\u8fc7<code>sys.exit(0)<\/code>\u9000\u51fa\u3002\u5982\u679c\u4f60\u4f20\u9012\u4e00\u4e2a\u975e\u96f6\u503c\u7ed9<code>sys.exit()<\/code>\uff0c\u5b83\u4f1a\u8868\u793a\u5f02\u5e38\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>os._exit()<\/code><\/h3>\n<\/p>\n<p><p><code>os._exit()<\/code>\u662f\u4e00\u4e2a\u4f4e\u7ea7\u522b\u7684\u9000\u51fa\u51fd\u6570\uff0c\u5b83\u76f4\u63a5\u7ec8\u6b62\u8fdb\u7a0b\uff0c\u4e0d\u4f1a\u6267\u884c\u4efb\u4f55\u6e05\u7406\u64cd\u4f5c\uff0c\u5982\u5173\u95ed\u6587\u4ef6\u3001\u5237\u65b0\u7f13\u51b2\u533a\u7b49\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u5728\u5b50\u8fdb\u7a0b\u4e2d\u5f3a\u5236\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def main():<\/p>\n<p>    print(&quot;Starting program&quot;)<\/p>\n<p>    os._exit(1)  # 1\u8868\u793a\u5f02\u5e38\u9000\u51fa<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5728\u9700\u8981\u7acb\u5373\u7ec8\u6b62\u7a0b\u5e8f\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\uff0c\u4f46\u5b83\u4e0d\u4f1a\u8fdb\u884c\u4efb\u4f55\u6e05\u7406\u64cd\u4f5c\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8d44\u6e90\u672a\u91ca\u653e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>signal<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>signal<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u5904\u7406\u4fe1\u53f7\u7684\u673a\u5236\uff0c\u53ef\u4ee5\u7528\u6765\u6355\u83b7\u4fe1\u53f7\u5e76\u6267\u884c\u76f8\u5e94\u7684\u9000\u51fa\u64cd\u4f5c\u3002\u5e38\u89c1\u7684\u4fe1\u53f7\u5305\u62ec<code>SIGINT<\/code>\uff08Ctrl+C\uff09\u3001<code>SIGTERM<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import signal<\/p>\n<p>import sys<\/p>\n<p>def signal_handler(signum, frame):<\/p>\n<p>    print(&quot;Signal received:&quot;, signum)<\/p>\n<p>    sys.exit(0)<\/p>\n<p>signal.signal(signal.SIGINT, signal_handler)<\/p>\n<p>print(&quot;Press Ctrl+C to exit&quot;)<\/p>\n<p>while True:<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4fe1\u53f7\u5904\u7406\u51fd\u6570<code>signal_handler<\/code>\uff0c\u5f53\u6536\u5230<code>SIGINT<\/code>\u4fe1\u53f7\u65f6\u8c03\u7528\u8be5\u51fd\u6570\u5e76\u9000\u51fa\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528<code>threading<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u63d0\u4f9b\u7684\u673a\u5236\u6765\u7ec8\u6b62\u7ebf\u7a0b\u548c\u7a0b\u5e8f\u3002\u867d\u7136Python\u4e0d\u63d0\u4f9b\u76f4\u63a5\u7ec8\u6b62\u7ebf\u7a0b\u7684\u65b9\u6cd5\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6807\u5fd7\u4f4d\u6765\u63a7\u5236\u7ebf\u7a0b\u7684\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>exit_flag = False<\/p>\n<p>def worker():<\/p>\n<p>    while not exit_flag:<\/p>\n<p>        print(&quot;Working...&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>thread = threading.Thread(target=worker)<\/p>\n<p>thread.start()<\/p>\n<p>time.sleep(5)<\/p>\n<p>exit_flag = True<\/p>\n<p>thread.join()<\/p>\n<p>print(&quot;Program exited&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u6807\u5fd7\u4f4d<code>exit_flag<\/code>\u6765\u63a7\u5236\u7ebf\u7a0b\u7684\u8fd0\u884c\uff0c\u5f53\u4e3b\u7ebf\u7a0b\u5c06<code>exit_flag<\/code>\u8bbe\u7f6e\u4e3a<code>True<\/code>\u65f6\uff0c\u5de5\u4f5c\u7ebf\u7a0b\u4f1a\u9000\u51fa\u5faa\u73af\u5e76\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u8fdb\u7a0b\u7ba1\u7406\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u591a\u8fdb\u7a0b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>multiprocessing<\/code>\u6a21\u5757\u63d0\u4f9b\u7684\u673a\u5236\u6765\u7ba1\u7406\u8fdb\u7a0b\u3002<code>multiprocessing<\/code>\u6a21\u5757\u5141\u8bb8\u4f60\u521b\u5efa\u548c\u7ba1\u7406\u591a\u4e2a\u8fdb\u7a0b\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\u6765\u7ec8\u6b62\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>import time<\/p>\n<p>def worker():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Working...&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>process = multiprocessing.Process(target=worker)<\/p>\n<p>process.start()<\/p>\n<p>time.sleep(5)<\/p>\n<p>process.terminate()<\/p>\n<p>process.join()<\/p>\n<p>print(&quot;Program exited&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5b50\u8fdb\u7a0b<code>process<\/code>\uff0c\u5e76\u57285\u79d2\u540e\u901a\u8fc7<code>terminate()<\/code>\u65b9\u6cd5\u7ec8\u6b62\u8be5\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5e94\u7528\u573a\u666f\u5206\u6790<\/h3>\n<\/p>\n<p><h4>1\u3001Web\u670d\u52a1\u5668\u4e2d\u7684\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u5728Web\u670d\u52a1\u5668\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5728\u7279\u5b9a\u60c5\u51b5\u4e0b\u5f3a\u5236\u5173\u95ed\u670d\u52a1\u3002\u4f8b\u5982\uff0c\u5f53\u670d\u52a1\u5668\u68c0\u6d4b\u5230\u4e25\u91cd\u9519\u8bef\u6216\u9700\u8981\u8fdb\u884c\u7d27\u6025\u7ef4\u62a4\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys.exit()<\/code>\u6216<code>os._exit()<\/code>\u6765\u5173\u95ed\u670d\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask<\/p>\n<p>import sys<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def index():<\/p>\n<p>    return &quot;Hello, World!&quot;<\/p>\n<p>@app.route(&#39;\/shutdown&#39;)<\/p>\n<p>def shutdown():<\/p>\n<p>    sys.exit(0)<\/p>\n<p>    return &quot;Server shutting down...&quot;<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    app.run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684Flask Web\u670d\u52a1\u5668\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>\/shutdown<\/code>\u8def\u7531\uff0c\u5f53\u8bbf\u95ee\u8be5\u8def\u7531\u65f6\uff0c\u670d\u52a1\u5668\u4f1a\u8c03\u7528<code>sys.exit(0)<\/code>\u5173\u95ed\u670d\u52a1\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u540e\u53f0\u4efb\u52a1\u7ba1\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u540e\u53f0\u4efb\u52a1\u7ba1\u7406\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5728\u7279\u5b9a\u60c5\u51b5\u4e0b\u7ec8\u6b62\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u5f53\u4efb\u52a1\u8fd0\u884c\u65f6\u95f4\u8fc7\u957f\u6216\u68c0\u6d4b\u5230\u5f02\u5e38\u60c5\u51b5\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys.exit()<\/code>\u6216<code>os._exit()<\/code>\u6765\u7ec8\u6b62\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import sys<\/p>\n<p>def long_running_task():<\/p>\n<p>    start_time = time.time()<\/p>\n<p>    while True:<\/p>\n<p>        elapsed_time = time.time() - start_time<\/p>\n<p>        if elapsed_time &gt; 10:  # \u8d85\u8fc710\u79d2\u7ec8\u6b62\u4efb\u52a1<\/p>\n<p>            sys.exit(1)<\/p>\n<p>        print(&quot;Task running...&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    long_running_task()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\uff0c\u5e76\u5728\u4efb\u52a1\u8fd0\u884c\u8d85\u8fc710\u79d2\u65f6\u901a\u8fc7<code>sys.exit(1)<\/code>\u7ec8\u6b62\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u6027\u80fd\u548c\u5b89\u5168\u6027\u8003\u8651<\/h3>\n<\/p>\n<p><h4>1\u3001\u6027\u80fd<\/h4>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5f3a\u5236\u5173\u95ed\u7a0b\u5e8f\u7684\u65b9\u6cd5\u65f6\uff0c\u6027\u80fd\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002<code>sys.exit()<\/code>\u548c<code>os._exit()<\/code>\u7684\u6027\u80fd\u5dee\u5f02\u4e3b\u8981\u5728\u4e8e\u6e05\u7406\u64cd\u4f5c\u7684\u6267\u884c\u3002<code>sys.exit()<\/code>\u4f1a\u6267\u884c\u4e00\u4e9b\u6e05\u7406\u64cd\u4f5c\uff0c\u5982\u5173\u95ed\u6587\u4ef6\u3001\u91ca\u653e\u8d44\u6e90\u7b49\uff0c\u800c<code>os._exit()<\/code>\u5219\u4e0d\u4f1a\u3002\u56e0\u6b64\uff0c\u5728\u9700\u8981\u5feb\u901f\u7ec8\u6b62\u7a0b\u5e8f\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u9009\u62e9<code>os._exit()<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5b89\u5168\u6027<\/h4>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5f3a\u5236\u5173\u95ed\u7a0b\u5e8f\u7684\u65b9\u6cd5\u65f6\uff0c\u5b89\u5168\u6027\u4e5f\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002<code>sys.exit()<\/code>\u4f1a\u6267\u884c\u6e05\u7406\u64cd\u4f5c\uff0c\u6709\u52a9\u4e8e\u786e\u4fdd\u8d44\u6e90\u7684\u6b63\u786e\u91ca\u653e\uff0c\u51cf\u5c11\u8d44\u6e90\u6cc4\u6f0f\u7684\u98ce\u9669\u3002\u800c<code>os._exit()<\/code>\u5219\u4e0d\u4f1a\u6267\u884c\u6e05\u7406\u64cd\u4f5c\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8d44\u6e90\u672a\u91ca\u653e\u3002\u56e0\u6b64\uff0c\u5728\u9700\u8981\u786e\u4fdd\u8d44\u6e90\u6b63\u786e\u91ca\u653e\u7684\u60c5\u51b5\u4e0b\uff0c\u5efa\u8bae\u9009\u62e9<code>sys.exit()<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f18\u96c5\u9000\u51fa<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4f18\u96c5\u9000\u51fa\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u4f18\u96c5\u9000\u51fa\u6307\u7684\u662f\u5728\u5173\u95ed\u7a0b\u5e8f\u65f6\uff0c\u80fd\u591f\u6b63\u786e\u91ca\u653e\u8d44\u6e90\u3001\u4fdd\u5b58\u72b6\u6001\u7b49\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u548c\u6570\u636e\u4e22\u5931\u3002\u4e3a\u5b9e\u73b0\u4f18\u96c5\u9000\u51fa\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u4fe1\u53f7\u5904\u7406\u3001<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a>\u7b49\u673a\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import signal<\/p>\n<p>import sys<\/p>\n<p>def cleanup():<\/p>\n<p>    print(&quot;Cleaning up resources...&quot;)<\/p>\n<p>def signal_handler(signum, frame):<\/p>\n<p>    print(&quot;Signal received:&quot;, signum)<\/p>\n<p>    cleanup()<\/p>\n<p>    sys.exit(0)<\/p>\n<p>signal.signal(signal.SIGINT, signal_handler)<\/p>\n<p>print(&quot;Press Ctrl+C to exit&quot;)<\/p>\n<p>while True:<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6e05\u7406\u51fd\u6570<code>cleanup<\/code>\uff0c\u5e76\u5728\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u4e2d\u8c03\u7528\u8be5\u6e05\u7406\u51fd\u6570\uff0c\u5b9e\u73b0\u4f18\u96c5\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u65e5\u5fd7\u8bb0\u5f55<\/h4>\n<\/p>\n<p><p>\u5728\u5f3a\u5236\u5173\u95ed\u7a0b\u5e8f\u65f6\uff0c\u8bb0\u5f55\u65e5\u5fd7\u6709\u52a9\u4e8e\u95ee\u9898\u7684\u6392\u67e5\u548c\u5206\u6790\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>logging<\/code>\u6a21\u5757\u8bb0\u5f55\u76f8\u5173\u65e5\u5fd7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>import sys<\/p>\n<p>logging.basicConfig(level=logging.INFO)<\/p>\n<p>def main():<\/p>\n<p>    logging.info(&quot;Starting program&quot;)<\/p>\n<p>    sys.exit(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\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u8bb0\u5f55\u7a0b\u5e8f\u7684\u542f\u52a8\u65e5\u5fd7\uff0c\u5e76\u5728\u5f3a\u5236\u5173\u95ed\u7a0b\u5e8f\u65f6\u8bb0\u5f55\u76f8\u5e94\u65e5\u5fd7\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u8d44\u6e90\u7ba1\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u5f3a\u5236\u5173\u95ed\u7a0b\u5e8f\u65f6\uff0c\u6b63\u786e\u7ba1\u7406\u8d44\u6e90\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u53ef\u4ee5\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff08context manager\uff09\u786e\u4fdd\u8d44\u6e90\u7684\u6b63\u786e\u91ca\u653e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>class Resource:<\/p>\n<p>    def __enter__(self):<\/p>\n<p>        print(&quot;Acquiring resource&quot;)<\/p>\n<p>        return self<\/p>\n<p>    def __exit__(self, exc_type, exc_value, traceback):<\/p>\n<p>        print(&quot;Releasing resource&quot;)<\/p>\n<p>def main():<\/p>\n<p>    with Resource():<\/p>\n<p>        print(&quot;Using resource&quot;)<\/p>\n<p>        sys.exit(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\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<code>Resource<\/code>\uff0c\u5e76\u5728<code>__exit__<\/code>\u65b9\u6cd5\u4e2d\u91ca\u653e\u8d44\u6e90\u3002\u5728\u5f3a\u5236\u5173\u95ed\u7a0b\u5e8f\u65f6\uff0c\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u4f1a\u786e\u4fdd\u8d44\u6e90\u7684\u6b63\u786e\u91ca\u653e\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5e38\u89c1\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b50\u7ebf\u7a0b\u672a\u80fd\u6b63\u786e\u9000\u51fa<\/h4>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u5b50\u7ebf\u7a0b\u672a\u80fd\u6b63\u786e\u9000\u51fa\u662f\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528\u6807\u5fd7\u4f4d\u6216\u4e8b\u4ef6\u5bf9\u8c61\u6765\u63a7\u5236\u5b50\u7ebf\u7a0b\u7684\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>exit_event = threading.Event()<\/p>\n<p>def worker():<\/p>\n<p>    while not exit_event.is_set():<\/p>\n<p>        print(&quot;Working...&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>thread = threading.Thread(target=worker)<\/p>\n<p>thread.start()<\/p>\n<p>time.sleep(5)<\/p>\n<p>exit_event.set()<\/p>\n<p>thread.join()<\/p>\n<p>print(&quot;Program exited&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>threading.Event<\/code>\u5bf9\u8c61\u63a7\u5236\u5b50\u7ebf\u7a0b\u7684\u8fd0\u884c\uff0c\u5e76\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u8bbe\u7f6e\u4e8b\u4ef6\uff0c\u901a\u77e5\u5b50\u7ebf\u7a0b\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8d44\u6e90\u672a\u80fd\u6b63\u786e\u91ca\u653e<\/h4>\n<\/p>\n<p><p>\u5728\u5f3a\u5236\u5173\u95ed\u7a0b\u5e8f\u65f6\uff0c\u8d44\u6e90\u672a\u80fd\u6b63\u786e\u91ca\u653e\u662f\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6216\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u786e\u4fdd\u8d44\u6e90\u7684\u6b63\u786e\u91ca\u653e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import signal<\/p>\n<p>import sys<\/p>\n<p>class Resource:<\/p>\n<p>    def __enter__(self):<\/p>\n<p>        print(&quot;Acquiring resource&quot;)<\/p>\n<p>        return self<\/p>\n<p>    def __exit__(self, exc_type, exc_value, traceback):<\/p>\n<p>        print(&quot;Releasing resource&quot;)<\/p>\n<p>def cleanup():<\/p>\n<p>    print(&quot;Cleaning up resources...&quot;)<\/p>\n<p>def signal_handler(signum, frame):<\/p>\n<p>    print(&quot;Signal received:&quot;, signum)<\/p>\n<p>    cleanup()<\/p>\n<p>    sys.exit(0)<\/p>\n<p>signal.signal(signal.SIGINT, signal_handler)<\/p>\n<p>print(&quot;Press Ctrl+C to exit&quot;)<\/p>\n<p>with Resource():<\/p>\n<p>    while True:<\/p>\n<p>        pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u7ed3\u5408\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u548c\u4fe1\u53f7\u5904\u7406\u51fd\u6570\uff0c\u786e\u4fdd\u8d44\u6e90\u5728\u5f3a\u5236\u5173\u95ed\u7a0b\u5e8f\u65f6\u80fd\u591f\u6b63\u786e\u91ca\u653e\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u591a\u79cd\u5f3a\u5236\u5173\u95edPython\u7a0b\u5e8f\u7684\u65b9\u6cd5\uff0c\u5305\u62ec<code>sys.exit()<\/code>\u3001<code>os._exit()<\/code>\u3001<code>signal<\/code>\u6a21\u5757\u3001<code>threading<\/code>\u6a21\u5757\u548c\u8fdb\u7a0b\u7ba1\u7406\u5de5\u5177\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u4f18\u96c5\u9000\u51fa\u3001\u65e5\u5fd7\u8bb0\u5f55\u3001\u8d44\u6e90\u7ba1\u7406\u7b49\u6700\u4f73\u5b9e\u8df5\uff0c\u786e\u4fdd\u7a0b\u5e8f\u80fd\u591f\u6b63\u786e\u5173\u95ed\u548c\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u5168\u5730\u7ec8\u6b62\u4e00\u4e2a\u6b63\u5728\u8fd0\u884c\u7684\u7a0b\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys.exit()<\/code>\u51fd\u6570\u5b89\u5168\u5730\u7ec8\u6b62\u7a0b\u5e8f\u3002\u8be5\u51fd\u6570\u4f1a\u5f15\u53d1\u4e00\u4e2a<code>SystemExit<\/code>\u5f02\u5e38\uff0c\u7a0b\u5e8f\u5728\u6355\u83b7\u8fd9\u4e2a\u5f02\u5e38\u540e\u4f1a\u8fdb\u884c\u9002\u5f53\u7684\u6e05\u7406\u5de5\u4f5c\u5e76\u9000\u51fa\u3002\u6b64\u5916\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>os._exit()<\/code>\u76f4\u63a5\u9000\u51fa\uff0c\u4f46\u8fd9\u4e0d\u4f1a\u6267\u884c\u4efb\u4f55\u6e05\u7406\u64cd\u4f5c\uff0c\u5efa\u8bae\u4ec5\u5728\u7279\u6b8a\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728Python\u4e2d\u901a\u8fc7\u4efb\u52a1\u7ba1\u7406\u5668\u5f3a\u5236\u5173\u95ed\u7a0b\u5e8f\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u5982\u679c\u4f60\u7684Python\u7a0b\u5e8f\u672a\u54cd\u5e94\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4efb\u52a1\u7ba1\u7406\u5668\u5f3a\u5236\u5173\u95ed\u5b83\u3002\u5728Windows\u4e2d\uff0c\u6309\u4e0bCtrl + Shift + Esc\u6253\u5f00\u4efb\u52a1\u7ba1\u7406\u5668\uff0c\u627e\u5230\u4f60\u7684Python\u8fdb\u7a0b\uff0c\u53f3\u952e\u70b9\u51fb\u5e76\u9009\u62e9\u201c\u7ed3\u675f\u4efb\u52a1\u201d\u3002\u5728Mac\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u201c\u6d3b\u52a8\u76d1\u89c6\u5668\u201d\u627e\u5230\u5e76\u7ed3\u675fPython\u8fdb\u7a0b\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u672a\u54cd\u5e94\u7684\u7a0b\u5e8f\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u53d8\u5f97\u65e0\u54cd\u5e94\uff0c\u53ef\u4ee5\u8003\u8651\u5728\u4ee3\u7801\u4e2d\u5b9e\u73b0\u8d85\u65f6\u673a\u5236\u6216\u4f7f\u7528\u591a\u7ebf\u7a0b\/\u591a\u8fdb\u7a0b\u6765\u5904\u7406\u8017\u65f6\u64cd\u4f5c\u3002\u901a\u8fc7\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6355\u83b7\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u6302\u8d77\u7684\u90e8\u5206\uff0c\u80fd\u591f\u6709\u6548\u5730\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u5e76\u786e\u4fdd\u7a0b\u5e8f\u80fd\u591f\u987a\u5229\u9000\u51fa\u6216\u6062\u590d\u8fd0\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5f3a\u5236\u5173\u95ed\u7a0b\u5e8f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528sys.exit()\u3001os._exit()\u3001signal\u6a21\u5757\u3001t [&hellip;]","protected":false},"author":3,"featured_media":1172630,"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\/1172627"}],"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=1172627"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172627\/revisions"}],"predecessor-version":[{"id":1172631,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172627\/revisions\/1172631"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1172630"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1172627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1172627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1172627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}