{"id":1003893,"date":"2024-12-27T10:21:16","date_gmt":"2024-12-27T02:21:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1003893.html"},"modified":"2024-12-27T10:21:20","modified_gmt":"2024-12-27T02:21:20","slug":"%e5%a6%82%e4%bd%95%e7%bb%88%e6%ad%a2python%e7%9a%84%e4%bb%bb%e5%8a%a1","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1003893.html","title":{"rendered":"\u5982\u4f55\u7ec8\u6b62python\u7684\u4efb\u52a1"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081049\/0762a0c7-1b45-48cb-8271-ec4fd65d1a78.webp\" alt=\"\u5982\u4f55\u7ec8\u6b62python\u7684\u4efb\u52a1\" \/><\/p>\n<p><p> <strong>\u7ec8\u6b62Python\u4efb\u52a1\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528Ctrl+C\u4e2d\u65ad\u3001\u8bbe\u7f6e\u8d85\u65f6\u3001\u4f7f\u7528\u4fe1\u53f7\u5904\u7406\u3001\u901a\u8fc7\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u7ba1\u7406\u3001\u4f7f\u7528\u5916\u90e8\u5de5\u5177\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528Ctrl+C\u4e2d\u65ad\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4e0d\u591f\u7075\u6d3b\uff0c\u56e0\u6b64\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6\u65b9\u6cd5\u5982\u8bbe\u7f6e\u8d85\u65f6\u548c\u4fe1\u53f7\u5904\u7406\u6765\u589e\u5f3a\u5bf9\u4efb\u52a1\u7684\u63a7\u5236\u3002\u4ee5\u4e0b\u5c06\u5bf9\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u2014\u2014\u4f7f\u7528\u4fe1\u53f7\u5904\u7406\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fe1\u53f7\u5904\u7406\u6765\u7ec8\u6b62\u7279\u5b9a\u4efb\u52a1\u3002Python\u7684<code>signal<\/code>\u6a21\u5757\u5141\u8bb8\u6355\u83b7\u548c\u5904\u7406\u64cd\u4f5c\u7cfb\u7edf\u4fe1\u53f7\uff0c\u4f8b\u5982SIGINT\uff08\u901a\u5e38\u7531Ctrl+C\u4ea7\u751f\uff09\u3002\u901a\u8fc7\u81ea\u5b9a\u4e49\u4fe1\u53f7\u5904\u7406\u51fd\u6570\uff0c\u53ef\u4ee5\u4f18\u96c5\u5730\u7ec8\u6b62\u4efb\u52a1\uff0c\u540c\u65f6\u786e\u4fdd\u8d44\u6e90\u5f97\u5230\u6b63\u786e\u91ca\u653e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u6355\u83b7SIGINT\u4fe1\u53f7\uff0c\u5f53\u7528\u6237\u6309\u4e0bCtrl+C\u65f6\uff0c\u6267\u884c\u6e05\u7406\u64cd\u4f5c\u5e76\u5b89\u5168\u9000\u51fa\u7a0b\u5e8f\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u4fdd\u8bc1\u4efb\u52a1\u4e2d\u6570\u636e\u5b8c\u6574\u6027\u548c\u8d44\u6e90\u91ca\u653e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528Ctrl+C\u4e2d\u65ad<\/p>\n<\/p>\n<p><p>Ctrl+C\u662f\u6700\u5e38\u7528\u7684\u4e2d\u65adPython\u4efb\u52a1\u7684\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u5f0f\u5229\u7528\u4e86Python\u9ed8\u8ba4\u7684\u4fe1\u53f7\u5904\u7406\u673a\u5236\uff0c\u6309\u4e0bCtrl+C\u4f1a\u89e6\u53d1KeyboardInterrupt\u5f02\u5e38\uff0c\u4ece\u800c\u7ec8\u6b62\u4efb\u52a1\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u5e76\u4e0d\u9002\u7528\u4e8e\u6240\u6709\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u5f53Python\u7a0b\u5e8f\u8fd0\u884c\u5728\u540e\u53f0\u6216\u901a\u8fc7\u67d0\u4e9b\u63a5\u53e3\u8c03\u7528\u65f6\uff0cCtrl+C\u53ef\u80fd\u65e0\u6cd5\u53d1\u6325\u4f5c\u7528\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u6355\u83b7KeyboardInterrupt\u5f02\u5e38\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5728\u4e2d\u65ad\u4efb\u52a1\u65f6\u8fdb\u884c\u4e00\u4e9b\u6e05\u7406\u5de5\u4f5c\u3002\u4f8b\u5982\uff0c\u91ca\u653e\u8d44\u6e90\u3001\u4fdd\u5b58\u6570\u636e\u7b49\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u72b6\u6001\u4e00\u81f4\u6027\u548c\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # long running task<\/p>\n<p>    while True:<\/p>\n<p>        pass<\/p>\n<p>except KeyboardInterrupt:<\/p>\n<p>    print(&quot;Task interrupted&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u8bbe\u7f6e\u8d85\u65f6<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u53ef\u80fd\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\uff0c\u8bbe\u7f6e\u8d85\u65f6\u662f\u975e\u5e38\u6709\u6548\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u4f7f\u7528Python\u7684<code>threading<\/code>\u6216<code>concurrent.futures<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u4e3a\u4efb\u52a1\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\uff0c\u4e00\u65e6\u8d85\u65f6\u672a\u5b8c\u6210\u5219\u7ec8\u6b62\u4efb\u52a1\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u5904\u7406\u90a3\u4e9b\u53ef\u80fd\u56e0\u4e3a\u5916\u90e8\u539f\u56e0\u800c\u65e0\u9650\u671f\u6302\u8d77\u7684\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u5728<code>concurrent.futures<\/code>\u6a21\u5757\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t<\/code>\u51fd\u6570\u7684<code>timeout<\/code>\u53c2\u6570\u5b9e\u73b0\u4efb\u52a1\u7684\u8d85\u65f6\u63a7\u5236\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor, wait<\/p>\n<p>def long_task():<\/p>\n<p>    # simulate long task<\/p>\n<p>    import time<\/p>\n<p>    time.sleep(10)<\/p>\n<p>with ThreadPoolExecutor() as executor:<\/p>\n<p>    future = executor.submit(long_task)<\/p>\n<p>    try:<\/p>\n<p>        wait([future], timeout=5)<\/p>\n<p>    except TimeoutError:<\/p>\n<p>        print(&quot;Task timed out&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u4fe1\u53f7\u5904\u7406<\/p>\n<\/p>\n<p><p>\u4fe1\u53f7\u5904\u7406\u63d0\u4f9b\u4e86\u4e00\u79cd\u4f18\u96c5\u7684\u7ec8\u6b62\u4efb\u52a1\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u6355\u83b7\u7279\u5b9a\u4fe1\u53f7\uff08\u5982SIGINT\u3001SIGTERM\uff09\uff0c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u81ea\u5b9a\u4e49\u5904\u7406\u903b\u8f91\uff0c\u4f8b\u5982\u6e05\u7406\u8d44\u6e90\u3001\u8bb0\u5f55\u65e5\u5fd7\u7b49\u3002<\/p>\n<\/p>\n<p><p>Python\u7684<code>signal<\/code>\u6a21\u5757\u975e\u5e38\u9002\u5408\u8fd9\u79cd\u9700\u6c42\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\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(&#39;Task terminated&#39;)<\/p>\n<p>    sys.exit(0)<\/p>\n<p>signal.signal(signal.SIGINT, signal_handler)<\/p>\n<h2><strong>long running task<\/strong><\/h2>\n<p>while True:<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u4efb\u52a1\u7684\u7ec8\u6b62\u8fc7\u7a0b\uff0c\u786e\u4fdd\u7a0b\u5e8f\u5728\u6536\u5230\u7ec8\u6b62\u4fe1\u53f7\u65f6\u80fd\u591f\u4ee5\u53d7\u63a7\u7684\u65b9\u5f0f\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u901a\u8fc7\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u7ba1\u7406<\/p>\n<\/p>\n<p><p>\u5728\u590d\u6742\u7684\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u66f4\u7075\u6d3b\u7684\u4efb\u52a1\u7ec8\u6b62\u63a7\u5236\u3002\u8fd9\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u7ba1\u7406\u4efb\u52a1\u3002\u901a\u8fc7\u4e3b\u7ebf\u7a0b\u6216\u4e3b\u8fdb\u7a0b\u76d1\u63a7\u4efb\u52a1\u7684\u72b6\u6001\uff0c\u4e00\u65e6\u9700\u8981\u7ec8\u6b62\uff0c\u53ef\u4ee5\u5411\u4efb\u52a1\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u53d1\u9001\u7ec8\u6b62\u4fe1\u53f7\u3002<\/p>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>threading.Event<\/code>\u5bf9\u8c61\u6765\u63a7\u5236\u4efb\u52a1\u7684\u7ec8\u6b62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def long_task(stop_event):<\/p>\n<p>    while not stop_event.is_set():<\/p>\n<p>        print(&quot;Running...&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>    print(&quot;Task terminated&quot;)<\/p>\n<p>stop_event = threading.Event()<\/p>\n<p>task_thread = threading.Thread(target=long_task, args=(stop_event,))<\/p>\n<p>task_thread.start()<\/p>\n<h2><strong>simulate some condition for stopping the task<\/strong><\/h2>\n<p>time.sleep(5)<\/p>\n<p>stop_event.set()<\/p>\n<p>task_thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u5916\u90e8\u5de5\u5177<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u5916\u90e8\u5de5\u5177\u6765\u76d1\u63a7\u548c\u7ec8\u6b62Python\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u4efb\u52a1\u7ba1\u7406\u5de5\u5177\u6216\u811a\u672c\u6765\u7ec8\u6b62\u7279\u5b9a\u8fdb\u7a0b\u3002\u8fd9\u79cd\u65b9\u6cd5\u5bf9\u4e8e\u9700\u8981\u5728\u4e0d\u540c\u73af\u5883\u4e0b\u8fd0\u884c\u7684Python\u7a0b\u5e8f\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4f9d\u8d56\u4e8e\u7a0b\u5e8f\u5185\u90e8\u7684\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ps<\/code>\u547d\u4ee4\u67e5\u627e\u5e76\u7ec8\u6b62\u7279\u5b9a\u7684Python\u8fdb\u7a0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">ps aux | grep python<\/p>\n<p>kill &lt;PID&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u7b80\u5355\uff0c\u4f46\u9700\u8981\u624b\u52a8\u5e72\u9884\uff0c\u5e76\u4e14\u9700\u8981\u786e\u4fdd\u6b63\u786e\u8bc6\u522b\u8981\u7ec8\u6b62\u7684\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c\u7ec8\u6b62Python\u4efb\u52a1\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u6765\u51b3\u5b9a\u3002\u5728\u9700\u8981\u786e\u4fdd\u4efb\u52a1\u6570\u636e\u5b8c\u6574\u6027\u548c\u8d44\u6e90\u91ca\u653e\u7684\u60c5\u51b5\u4e0b\uff0c\u5efa\u8bae\u4f7f\u7528\u4fe1\u53f7\u5904\u7406\u3001\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u7ba1\u7406\u7b49\u65b9\u5f0f\uff0c\u4ee5\u4fdd\u8bc1\u7a0b\u5e8f\u7684\u6b63\u5e38\u9000\u51fa\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u67e5\u770b\u5f53\u524d\u8fd0\u884c\u7684Python\u4efb\u52a1\uff1f<\/strong><br \/>\u8981\u67e5\u770b\u5f53\u524d\u8fd0\u884c\u7684Python\u4efb\u52a1\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u3002\u5728Windows\u4e2d\uff0c\u53ef\u4ee5\u6253\u5f00\u4efb\u52a1\u7ba1\u7406\u5668\uff0c\u627e\u5230\u201cPython\u201d\u6216\u201cpython.exe\u201d\u8fdb\u7a0b\u3002\u5728Linux\u6216Mac\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4<code>ps aux | grep python<\/code>\u6765\u5217\u51fa\u6240\u6709\u8fd0\u884c\u4e2d\u7684Python\u8fdb\u7a0b\u3002\u8fd9\u5c06\u5e2e\u52a9\u4f60\u786e\u5b9a\u9700\u8981\u7ec8\u6b62\u7684\u4efb\u52a1\u3002<\/p>\n<p><strong>\u7ec8\u6b62Python\u4efb\u52a1\u65f6\u4f1a\u4e22\u5931\u672a\u4fdd\u5b58\u7684\u6570\u636e\u5417\uff1f<\/strong><br \/>\u5982\u679c\u5728\u7ec8\u6b62Python\u4efb\u52a1\u4e4b\u524d\u6ca1\u6709\u4fdd\u5b58\u6570\u636e\uff0c\u90a3\u4e48\u672a\u4fdd\u5b58\u7684\u8fdb\u7a0b\u6570\u636e\u5c06\u4f1a\u4e22\u5931\u3002\u4e3a\u4e86\u907f\u514d\u6570\u636e\u4e22\u5931\uff0c\u5efa\u8bae\u5b9a\u671f\u4fdd\u5b58\u5de5\u4f5c\u8fdb\u5ea6\uff0c\u5c24\u5176\u662f\u5728\u8fdb\u884c\u957f\u65f6\u95f4\u8ba1\u7b97\u6216\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5b89\u5168\u5730\u7ec8\u6b62Python\u7a0b\u5e8f\uff1f<\/strong><br \/>\u5b89\u5168\u5730\u7ec8\u6b62Python\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002\u4f7f\u7528<code>KeyboardInterrupt<\/code>\uff08\u901a\u5e38\u662fCtrl+C\uff09\u5728\u547d\u4ee4\u884c\u4e2d\u7ec8\u6b62\u8fdb\u7a0b\u662f\u4e00\u79cd\u5e38\u89c1\u65b9\u6cd5\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u7f16\u5199\u9002\u5f53\u7684\u5f02\u5e38\u5904\u7406\u6765\u6355\u83b7\u7ec8\u6b62\u4fe1\u53f7\u5e76\u8fdb\u884c\u6e05\u7406\u5de5\u4f5c\uff0c\u786e\u4fdd\u7a0b\u5e8f\u5728\u5173\u95ed\u524d\u4fdd\u5b58\u5fc5\u8981\u7684\u6570\u636e\u6216\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7ec8\u6b62Python\u4efb\u52a1\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528Ctrl+C\u4e2d\u65ad\u3001\u8bbe\u7f6e\u8d85\u65f6\u3001\u4f7f\u7528\u4fe1\u53f7\u5904\u7406\u3001\u901a\u8fc7\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u7ba1\u7406\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1003910,"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\/1003893"}],"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=1003893"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1003893\/revisions"}],"predecessor-version":[{"id":1003912,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1003893\/revisions\/1003912"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1003910"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1003893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1003893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1003893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}