{"id":1095070,"date":"2025-01-08T14:47:28","date_gmt":"2025-01-08T06:47:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1095070.html"},"modified":"2025-01-08T14:47:31","modified_gmt":"2025-01-08T06:47:31","slug":"%e5%a6%82%e4%bd%95%e7%bb%88%e6%ad%a2%e6%ad%a3%e5%9c%a8%e8%bf%90%e8%a1%8c%e7%9a%84python%e7%a8%8b%e5%ba%8f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1095070.html","title":{"rendered":"\u5982\u4f55\u7ec8\u6b62\u6b63\u5728\u8fd0\u884c\u7684python\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210853\/9c61343f-f04f-4363-9854-f0d15a91ae82.webp\" alt=\"\u5982\u4f55\u7ec8\u6b62\u6b63\u5728\u8fd0\u884c\u7684python\u7a0b\u5e8f\" \/><\/p>\n<p><p> <strong>\u7ec8\u6b62\u6b63\u5728\u8fd0\u884c\u7684python\u7a0b\u5e8f<\/strong>\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff1a\u4f7f\u7528<code>KeyboardInterrupt<\/code>\u3001\u8c03\u7528<code>sys.exit()<\/code>\u51fd\u6570\u3001\u4f7f\u7528<code>os._exit()<\/code>\u51fd\u6570\u3001\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u4f7f\u7528\u7ebf\u7a0b\u7ba1\u7406\u3001\u4f7f\u7528\u4fe1\u53f7\u5904\u7406\u7b49\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528<code>KeyboardInterrupt<\/code>\u3002<\/p>\n<\/p>\n<p><p><strong>KeyboardInterrupt<\/strong>\u662f\u7528\u6237\u901a\u8fc7\u624b\u52a8\u64cd\u4f5c\u7ec8\u6b62\u6b63\u5728\u8fd0\u884c\u7684Python\u7a0b\u5e8f\u7684\u5e38\u7528\u65b9\u6cd5\u3002\u5f53\u7528\u6237\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u884c\u4e2d\u8fd0\u884cPython\u7a0b\u5e8f\u65f6\uff0c\u6309\u4e0b<code>Ctrl+C<\/code>\u7ec4\u5408\u952e\u53ef\u4ee5\u89e6\u53d1<code>KeyboardInterrupt<\/code>\u5f02\u5e38\uff0c\u7a0b\u5e8f\u4f1a\u6355\u83b7\u8be5\u5f02\u5e38\u5e76\u7ec8\u6b62\u6267\u884c\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528KeyboardInterrupt\u7ec8\u6b62\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u6309\u4e0b<code>Ctrl+C<\/code>\u952e\u76d8\u7ec4\u5408\u952e\u6765\u7ec8\u6b62\u6b63\u5728\u8fd0\u884c\u7684Python\u7a0b\u5e8f\u3002\u8fd9\u4e2a\u7ec4\u5408\u952e\u4f1a\u5411\u6b63\u5728\u8fd0\u884c\u7684\u7a0b\u5e8f\u53d1\u9001\u4e00\u4e2a<code>KeyboardInterrupt<\/code>\u4fe1\u53f7\uff0c\u7a0b\u5e8f\u4f1a\u6355\u83b7\u8fd9\u4e2a\u4fe1\u53f7\u5e76\u505c\u6b62\u6267\u884c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Program is running...&quot;)<\/p>\n<p>except KeyboardInterrupt:<\/p>\n<p>    print(&quot;Program terminated by user.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u8fdb\u5165\u4e86\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u4e0d\u65ad\u8f93\u51fa\u201cProgram is running&#8230;\u201d\u3002\u5f53\u7528\u6237\u6309\u4e0b<code>Ctrl+C<\/code>\u7ec4\u5408\u952e\u65f6\uff0c\u7a0b\u5e8f\u6355\u83b7\u5230<code>KeyboardInterrupt<\/code>\u5f02\u5e38\uff0c\u5e76\u8f93\u51fa\u201cProgram terminated by user.\u201d\uff0c\u7136\u540e\u7ec8\u6b62\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528sys.exit()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>sys.exit()<\/code>\u51fd\u6570\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u4efb\u4f55\u4f4d\u7f6e\u8c03\u7528\uff0c\u7528\u4e8e\u5b89\u5168\u5730\u9000\u51fa\u7a0b\u5e8f\u3002\u6b64\u51fd\u6570\u4f1a\u5f15\u53d1<code>SystemExit<\/code>\u5f02\u5e38\uff0c\u7a0b\u5e8f\u4f1a\u6355\u83b7\u8be5\u5f02\u5e38\u5e76\u505c\u6b62\u6267\u884c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(&quot;Program is running...&quot;)<\/p>\n<p>sys.exit()<\/p>\n<p>print(&quot;This line will not be executed.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u5728\u8f93\u51fa\u201cProgram is running&#8230;\u201d\u4e4b\u540e\uff0c\u8c03\u7528<code>sys.exit()<\/code>\u51fd\u6570\uff0c\u7a0b\u5e8f\u4f1a\u7acb\u5373\u7ec8\u6b62\uff0c\u4e4b\u540e\u7684\u4ee3\u7801\u4e0d\u4f1a\u88ab\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528os._exit()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>os._exit()<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u66f4\u5e95\u5c42\u7684\u9000\u51fa\u65b9\u5f0f\uff0c\u5b83\u4f1a\u7acb\u5373\u7ec8\u6b62\u7a0b\u5e8f\u800c\u4e0d\u5f15\u53d1\u4efb\u4f55\u5f02\u5e38\u3002\u8fd9\u4e2a\u51fd\u6570\u76f4\u63a5\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u9000\u51fa\u51fd\u6570\uff0c\u9002\u7528\u4e8e\u9700\u8981\u7acb\u5373\u505c\u6b62\u7a0b\u5e8f\u7684\u60c5\u51b5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>print(&quot;Program is running...&quot;)<\/p>\n<p>os._exit(0)<\/p>\n<p>print(&quot;This line will not be executed.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u5728\u8f93\u51fa\u201cProgram is running&#8230;\u201d\u4e4b\u540e\uff0c\u8c03\u7528<code>os._exit(0)<\/code>\u51fd\u6570\uff0c\u7a0b\u5e8f\u4f1a\u7acb\u5373\u7ec8\u6b62\uff0c\u4e4b\u540e\u7684\u4ee3\u7801\u4e0d\u4f1a\u88ab\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u4f7f\u7528\u7ebf\u7a0b\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\uff0c\u7ec8\u6b62\u7a0b\u5e8f\u9700\u8981\u5bf9\u5404\u4e2a\u7ebf\u7a0b\u8fdb\u884c\u7ba1\u7406\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u7ebf\u7a0b\u7684\u5b88\u62a4\u72b6\u6001\u6216\u4f7f\u7528\u7ebf\u7a0b\u4e8b\u4ef6\u6765\u63a7\u5236\u7ebf\u7a0b\u7684\u7ec8\u6b62\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def worker():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Thread is running...&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>t = threading.Thread(target=worker)<\/p>\n<p>t.daemon = True<\/p>\n<p>t.start()<\/p>\n<p>try:<\/p>\n<p>    while True:<\/p>\n<p>        time.sleep(1)<\/p>\n<p>except KeyboardInterrupt:<\/p>\n<p>    print(&quot;M<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n program terminated by user.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a\u5b88\u62a4\u7ebf\u7a0b<code>worker<\/code>\uff0c\u5b83\u4f1a\u4e0d\u65ad\u8f93\u51fa\u201cThread is running&#8230;\u201d\u3002\u5f53\u7528\u6237\u6309\u4e0b<code>Ctrl+C<\/code>\u7ec4\u5408\u952e\u65f6\uff0c\u4e3b\u7ebf\u7a0b\u6355\u83b7\u5230<code>KeyboardInterrupt<\/code>\u5f02\u5e38\uff0c\u8f93\u51fa\u201cMain program terminated by user.\u201d\uff0c\u5b88\u62a4\u7ebf\u7a0b\u4f1a\u81ea\u52a8\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u4fe1\u53f7\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728UNIX\u548c\u7c7bUNIX\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4fe1\u53f7\u5904\u7406\u6765\u7ec8\u6b62\u7a0b\u5e8f\u3002<code>signal<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\uff0c\u7528\u4e8e\u6355\u83b7\u548c\u5904\u7406\u64cd\u4f5c\u7cfb\u7edf\u4fe1\u53f7\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import signal<\/p>\n<p>import sys<\/p>\n<p>import time<\/p>\n<p>def signal_handler(sig, frame):<\/p>\n<p>    print(&#39;Signal received, program terminated.&#39;)<\/p>\n<p>    sys.exit(0)<\/p>\n<p>signal.signal(signal.SIGINT, signal_handler)<\/p>\n<p>print(&#39;Press Ctrl+C to terminate the program.&#39;)<\/p>\n<p>while True:<\/p>\n<p>    time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4fe1\u53f7\u5904\u7406\u51fd\u6570<code>signal_handler<\/code>\uff0c\u7528\u4e8e\u5904\u7406<code>SIGINT<\/code>\u4fe1\u53f7\uff08\u5373<code>Ctrl+C<\/code>\uff09\u3002\u5f53\u7528\u6237\u6309\u4e0b<code>Ctrl+C<\/code>\u7ec4\u5408\u952e\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u8c03\u7528<code>signal_handler<\/code>\u51fd\u6570\uff0c\u8f93\u51fa\u201cSignal received, program terminated.\u201d\uff0c\u5e76\u8c03\u7528<code>sys.exit(0)<\/code>\u7ec8\u6b62\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u6761\u4ef6\u53d8\u91cf\u7ec8\u6b62\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u53d8\u91cf\u6765\u63a7\u5236\u7a0b\u5e8f\u7684\u7ec8\u6b62\u3002\u6761\u4ef6\u53d8\u91cf\u5141\u8bb8\u7ebf\u7a0b\u7b49\u5f85\u67d0\u4e2a\u6761\u4ef6\u53d1\u751f\uff0c\u7136\u540e\u7ee7\u7eed\u6267\u884c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>condition = threading.Condition()<\/p>\n<p>def worker():<\/p>\n<p>    with condition:<\/p>\n<p>        while True:<\/p>\n<p>            print(&quot;Thread is running...&quot;)<\/p>\n<p>            condition.wait()<\/p>\n<p>t = threading.Thread(target=worker)<\/p>\n<p>t.start()<\/p>\n<p>try:<\/p>\n<p>    while True:<\/p>\n<p>        time.sleep(1)<\/p>\n<p>except KeyboardInterrupt:<\/p>\n<p>    with condition:<\/p>\n<p>        print(&quot;Main program terminated by user.&quot;)<\/p>\n<p>        condition.notify_all()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a\u6761\u4ef6\u53d8\u91cf<code>condition<\/code>\uff0c\u5e76\u5728<code>worker<\/code>\u7ebf\u7a0b\u4e2d\u4f7f\u7528\u5b83\u3002\u4e3b\u7ebf\u7a0b\u6355\u83b7\u5230<code>KeyboardInterrupt<\/code>\u5f02\u5e38\u540e\uff0c\u4f1a\u901a\u77e5\u6240\u6709\u7b49\u5f85\u7684\u7ebf\u7a0b\u7ee7\u7eed\u6267\u884c\uff0c\u5e76\u8f93\u51fa\u201cMain program terminated by user.\u201d\uff0c\u7136\u540e\u7ec8\u6b62\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u5b50\u8fdb\u7a0b\u7ba1\u7406\u7ec8\u6b62\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b50\u8fdb\u7a0b\u6765\u8fd0\u884c\u4efb\u52a1\uff0c\u5e76\u901a\u8fc7\u7ba1\u7406\u5b50\u8fdb\u7a0b\u6765\u7ec8\u6b62\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>import time<\/p>\n<p>process = subprocess.Popen([&#39;python&#39;, &#39;-c&#39;, &#39;while True: print(&quot;Subprocess is running...&quot;); time.sleep(1)&#39;])<\/p>\n<p>try:<\/p>\n<p>    while True:<\/p>\n<p>        time.sleep(1)<\/p>\n<p>except KeyboardInterrupt:<\/p>\n<p>    process.terminate()<\/p>\n<p>    print(&quot;Main program terminated by user.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u6765\u8fd0\u884c\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u4efb\u52a1\u3002\u4e3b\u7ebf\u7a0b\u6355\u83b7\u5230<code>KeyboardInterrupt<\/code>\u5f02\u5e38\u540e\uff0c\u4f1a\u7ec8\u6b62\u5b50\u8fdb\u7a0b\uff0c\u5e76\u8f93\u51fa\u201cMain program terminated by user.\u201d\uff0c\u7136\u540e\u7ec8\u6b62\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7ec8\u6b62\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6765\u786e\u4fdd\u5728\u7a0b\u5e8f\u9000\u51fa\u65f6\u6267\u884c\u4e00\u4e9b\u6e05\u7406\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>class ProgramTerminator:<\/p>\n<p>    def __enter__(self):<\/p>\n<p>        return self<\/p>\n<p>    def __exit__(self, exc_type, exc_val, exc_tb):<\/p>\n<p>        print(&quot;Program terminated.&quot;)<\/p>\n<p>        return True<\/p>\n<p>with ProgramTerminator():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Program is running...&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<code>ProgramTerminator<\/code>\uff0c\u7528\u4e8e\u5728\u7a0b\u5e8f\u9000\u51fa\u65f6\u8f93\u51fa\u201cProgram terminated.\u201d\u3002\u5f53\u7528\u6237\u6309\u4e0b<code>Ctrl+C<\/code>\u7ec4\u5408\u952e\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u6355\u83b7\u5230<code>KeyboardInterrupt<\/code>\u5f02\u5e38\uff0c\u5e76\u8c03\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7684<code>__exit__<\/code>\u65b9\u6cd5\uff0c\u8f93\u51fa\u201cProgram terminated.\u201d\uff0c\u7136\u540e\u7ec8\u6b62\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u4fe1\u53f7\u91cf\u7ec8\u6b62\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u4fe1\u53f7\u91cf\u662f\u4e00\u79cd\u7528\u4e8e\u5728\u7ebf\u7a0b\u4e4b\u95f4\u540c\u6b65\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u7684\u673a\u5236\u3002\u53ef\u4ee5\u4f7f\u7528\u4fe1\u53f7\u91cf\u6765\u63a7\u5236\u7a0b\u5e8f\u7684\u7ec8\u6b62\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>sem = threading.Semaphore(0)<\/p>\n<p>def worker():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Thread is running...&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>t = threading.Thread(target=worker)<\/p>\n<p>t.start()<\/p>\n<p>try:<\/p>\n<p>    while True:<\/p>\n<p>        time.sleep(1)<\/p>\n<p>except KeyboardInterrupt:<\/p>\n<p>    sem.release()<\/p>\n<p>    print(&quot;Main program terminated by user.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a\u4fe1\u53f7\u91cf<code>sem<\/code>\uff0c\u5e76\u5728<code>worker<\/code>\u7ebf\u7a0b\u4e2d\u4f7f\u7528\u5b83\u3002\u4e3b\u7ebf\u7a0b\u6355\u83b7\u5230<code>KeyboardInterrupt<\/code>\u5f02\u5e38\u540e\uff0c\u4f1a\u91ca\u653e\u4fe1\u53f7\u91cf\uff0c\u5e76\u8f93\u51fa\u201cMain program terminated by user.\u201d\uff0c\u7136\u540e\u7ec8\u6b62\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528\u8d85\u65f6\u673a\u5236\u7ec8\u6b62\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u8d85\u65f6\u673a\u5236\u6765\u63a7\u5236\u7a0b\u5e8f\u7684\u7ec8\u6b62\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>timeout = 10  # seconds<\/p>\n<p>start_time = time.time()<\/p>\n<p>try:<\/p>\n<p>    while True:<\/p>\n<p>        if time.time() - start_time &gt; timeout:<\/p>\n<p>            print(&quot;Program terminated due to timeout.&quot;)<\/p>\n<p>            break<\/p>\n<p>        print(&quot;Program is running...&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>except KeyboardInterrupt:<\/p>\n<p>    print(&quot;Program terminated by user.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u8d85\u65f6\u65f6\u95f4<code>timeout<\/code>\uff0c\u5e76\u5728\u4e3b\u5faa\u73af\u4e2d\u68c0\u67e5\u662f\u5426\u8d85\u8fc7\u8d85\u65f6\u65f6\u95f4\u3002\u5982\u679c\u8d85\u8fc7\u8d85\u65f6\u65f6\u95f4\uff0c\u7a0b\u5e8f\u4f1a\u8f93\u51fa\u201cProgram terminated due to timeout.\u201d\uff0c\u7136\u540e\u7ec8\u6b62\u6267\u884c\u3002\u7528\u6237\u4e5f\u53ef\u4ee5\u901a\u8fc7\u6309\u4e0b<code>Ctrl+C<\/code>\u7ec4\u5408\u952e\u6765\u7ec8\u6b62\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u7ec8\u6b62\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u6765\u8bb0\u5f55\u7a0b\u5e8f\u7684\u7ec8\u6b62\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>import time<\/p>\n<p>logging.basicConfig(level=logging.INFO)<\/p>\n<p>try:<\/p>\n<p>    while True:<\/p>\n<p>        logging.info(&quot;Program is running...&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>except KeyboardInterrupt:<\/p>\n<p>    logging.info(&quot;Program terminated by user.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u8bb0\u5f55\u7a0b\u5e8f\u7684\u8fd0\u884c\u548c\u7ec8\u6b62\u4fe1\u606f\u3002\u5f53\u7528\u6237\u6309\u4e0b<code>Ctrl+C<\/code>\u7ec4\u5408\u952e\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u6355\u83b7\u5230<code>KeyboardInterrupt<\/code>\u5f02\u5e38\uff0c\u5e76\u8bb0\u5f55\u201cProgram terminated by user.\u201d\uff0c\u7136\u540e\u7ec8\u6b62\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528\u88c5\u9970\u5668\u7ec8\u6b62\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u88c5\u9970\u5668\u6765\u63a7\u5236\u7a0b\u5e8f\u7684\u7ec8\u6b62\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import functools<\/p>\n<p>def handle_exit(func):<\/p>\n<p>    @functools.wraps(func)<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        try:<\/p>\n<p>            return func(*args, kwargs)<\/p>\n<p>        except KeyboardInterrupt:<\/p>\n<p>            print(&quot;Program terminated by user.&quot;)<\/p>\n<p>    return wrapper<\/p>\n<p>@handle_exit<\/p>\n<p>def main():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Program is running...&quot;)<\/p>\n<p>        time.sleep(1)<\/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\u88c5\u9970\u5668<code>handle_exit<\/code>\uff0c\u7528\u4e8e\u6355\u83b7\u548c\u5904\u7406<code>KeyboardInterrupt<\/code>\u5f02\u5e38\u3002\u4e3b\u51fd\u6570<code>main<\/code>\u88ab\u88c5\u9970\u5668\u4fee\u9970\uff0c\u5f53\u7528\u6237\u6309\u4e0b<code>Ctrl+C<\/code>\u7ec4\u5408\u952e\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u6355\u83b7\u5230<code>KeyboardInterrupt<\/code>\u5f02\u5e38\uff0c\u5e76\u8f93\u51fa\u201cProgram terminated by user.\u201d\uff0c\u7136\u540e\u7ec8\u6b62\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u591a\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7ec8\u6b62\u6b63\u5728\u8fd0\u884c\u7684Python\u7a0b\u5e8f\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u5728\u9700\u8981\u65f6\u5b89\u5168\u3001\u53ca\u65f6\u5730\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Windows\u7cfb\u7edf\u4e2d\u7ec8\u6b62\u6b63\u5728\u8fd0\u884c\u7684Python\u7a0b\u5e8f\uff1f<\/strong><br \/>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u7ec8\u6b62\u6b63\u5728\u8fd0\u884c\u7684Python\u7a0b\u5e8f\u3002\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5feb\u6377\u952eCtrl + C\uff0c\u8fd9\u5c06\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u7a97\u53e3\u4e2d\u4e2d\u65ad\u5f53\u524d\u8fd0\u884c\u7684\u7a0b\u5e8f\u3002\u5982\u679c\u7a0b\u5e8f\u662f\u4ee5\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u8fd0\u884c\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u4efb\u52a1\u7ba1\u7406\u5668\u627e\u5230\u5bf9\u5e94\u7684Python\u8fdb\u7a0b\uff0c\u53f3\u952e\u9009\u62e9\u201c\u7ed3\u675f\u4efb\u52a1\u201d\u6765\u5f3a\u5236\u5173\u95ed\u3002<\/p>\n<p><strong>\u5728Linux\u6216Mac\u7cfb\u7edf\u4e2d\uff0c\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u7ed3\u675fPython\u7a0b\u5e8f\uff1f<\/strong><br \/>\u5728Linux\u6216Mac\u7cfb\u7edf\u4e2d\uff0c\u7ec8\u6b62Python\u7a0b\u5e8f\u540c\u6837\u53ef\u4ee5\u4f7f\u7528Ctrl + C\u5feb\u6377\u952e\u3002\u5982\u679c\u7a0b\u5e8f\u5728\u540e\u53f0\u8fd0\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4<code>ps aux | grep python<\/code>\u627e\u5230\u5bf9\u5e94\u7684\u8fdb\u7a0bID\uff08PID\uff09\uff0c\u7136\u540e\u4f7f\u7528<code>kill PID<\/code>\u547d\u4ee4\u6765\u7ed3\u675f\u8be5\u8fdb\u7a0b\u3002\u5982\u679c\u5e0c\u671b\u5f3a\u5236\u7ed3\u675f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>kill -9 PID<\/code>\u3002<\/p>\n<p><strong>\u5982\u679cPython\u7a0b\u5e8f\u65e0\u54cd\u5e94\uff0c\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5f53Python\u7a0b\u5e8f\u65e0\u54cd\u5e94\u65f6\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528\u4e0a\u8ff0\u7684\u65b9\u6cd5\u8fdb\u884c\u7ec8\u6b62\u3002\u5982\u679c\u7a0b\u5e8f\u4f9d\u7136\u6ca1\u6709\u54cd\u5e94\uff0c\u53ef\u4ee5\u91cd\u542f\u8ba1\u7b97\u673a\u6216\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u5f3a\u5236\u7ed3\u675f\u8fdb\u7a0b\u3002\u5728Windows\u4e2d\uff0c\u4efb\u52a1\u7ba1\u7406\u5668\u662f\u4e00\u4e2a\u6709\u6548\u7684\u5de5\u5177\uff0c\u800c\u5728Linux\u6216Mac\u4e2d\uff0c\u7ec8\u7aef\u547d\u4ee4<code>kill<\/code>\u4e0e<code>xkill<\/code>\u4e5f\u80fd\u6709\u6548\u89e3\u51b3\u95ee\u9898\u3002\u786e\u4fdd\u5728\u5f3a\u5236\u7ed3\u675f\u7a0b\u5e8f\u524d\u4fdd\u5b58\u91cd\u8981\u6570\u636e\uff0c\u4ee5\u514d\u9020\u6210\u6570\u636e\u4e22\u5931\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7ec8\u6b62\u6b63\u5728\u8fd0\u884c\u7684python\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff1a\u4f7f\u7528KeyboardInterrupt\u3001\u8c03\u7528sys.exi [&hellip;]","protected":false},"author":3,"featured_media":1095080,"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\/1095070"}],"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=1095070"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095070\/revisions"}],"predecessor-version":[{"id":1095083,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095070\/revisions\/1095083"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1095080"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1095070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1095070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1095070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}