{"id":773,"date":"2025-01-05T23:06:00","date_gmt":"2025-01-05T15:06:00","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=773"},"modified":"2025-01-05T23:06:00","modified_gmt":"2025-01-05T15:06:00","slug":"%e5%9c%a8-python-%e4%b8%ad%ef%bc%8c%e6%80%8e%e6%a0%b7%e5%8f%96%e6%b6%88%e9%95%bf%e6%97%b6%e9%97%b4%e8%bf%90%e8%a1%8c%e7%9a%84%e5%87%bd%e6%95%b0%e6%88%96%e6%93%8d%e4%bd%9c%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/05\/%e5%9c%a8-python-%e4%b8%ad%ef%bc%8c%e6%80%8e%e6%a0%b7%e5%8f%96%e6%b6%88%e9%95%bf%e6%97%b6%e9%97%b4%e8%bf%90%e8%a1%8c%e7%9a%84%e5%87%bd%e6%95%b0%e6%88%96%e6%93%8d%e4%bd%9c%ef%bc%9f\/","title":{"rendered":"\u5728 Python \u4e2d\uff0c\u600e\u6837\u53d6\u6d88\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u51fd\u6570\u6216\u64cd\u4f5c\uff1f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 Python \u4e2d\uff0c\u6709\u65f6\u4f60\u9700\u8981\u53d6\u6d88\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u51fd\u6570\u6216\u64cd\u4f5c\uff0c\u5c24\u5176\u662f\u5f53\u7a0b\u5e8f\u9047\u5230\u65e0\u6cd5\u63a7\u5236\u7684\u5916\u90e8\u4e8b\u4ef6\u6216\u9700\u8981\u54cd\u5e94\u7528\u6237\u8bf7\u6c42\u65f6\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e <strong>\u8d85\u65f6<\/strong> \u6765\u4e2d\u65ad\u8fd9\u4e9b\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u5904\u7406\u65b9\u5f0f\uff1a<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u4f7f\u7528 <code>signal<\/code> \u6a21\u5757<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>signal<\/code> \u6a21\u5757\u5141\u8bb8\u4f60\u8bbe\u7f6e\u8d85\u65f6\u673a\u5236\u6765\u4e2d\u65ad\u4e00\u4e2a\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u64cd\u4f5c\u3002\u901a\u5e38\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7528\u4e8e Unix \u7cfb\u7edf\uff08Linux \u548c macOS\uff09\uff0c\u5b83\u4e0d\u9002\u7528\u4e8e Windows\uff0c\u56e0\u4e3a Windows \u4e0d\u5b8c\u5168\u652f\u6301\u4fe1\u53f7\u673a\u5236\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import signal\nimport time\n\n# \u81ea\u5b9a\u4e49\u8d85\u65f6\u5904\u7406\u51fd\u6570\ndef handler(signum, frame):\n    raise TimeoutError(\"Function timed out\")\n\n# \u8bbe\u7f6e\u8d85\u65f6\u4fe1\u53f7\nsignal.signal(signal.SIGALRM, handler)\n\ndef long_running_function():\n    # \u6a21\u62df\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\n    time.sleep(10)\n    print(\"Function finished\")\n\ntry:\n    # \u8bbe\u7f6e 5 \u79d2\u7684\u8d85\u65f6\n    signal.alarm(5)\n\n    long_running_function()  # \u8c03\u7528\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u51fd\u6570\n\nexcept TimeoutError as e:\n    print(f\"Timeout occurred: {e}\")\nfinally:\n    # \u53d6\u6d88\u4efb\u4f55\u540e\u7eed\u7684\u4fe1\u53f7\u8b66\u62a5\n    signal.alarm(0)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>signal.alarm(5)<\/code><\/strong>\uff1a\u8bbe\u7f6e\u4e00\u4e2a 5 \u79d2\u7684\u8d85\u65f6\u5b9a\u65f6\u5668\u3002<\/li>\n\n\n\n<li><strong><code>signal.signal(signal.SIGALRM, handler)<\/code><\/strong>\uff1a\u6307\u5b9a\u4e00\u4e2a\u5904\u7406\u8d85\u65f6\u7684\u56de\u8c03\u51fd\u6570 <code>handler<\/code>\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u51fd\u6570 <code>long_running_function<\/code> \u8fd0\u884c\u8d85\u8fc7 5 \u79d2\uff0c<code>TimeoutError<\/code> \u5c06\u88ab\u629b\u51fa\u5e76\u88ab\u6355\u83b7\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u4f7f\u7528 <code>threading<\/code> \u6a21\u5757<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>threading<\/code> \u6a21\u5757\u5141\u8bb8\u4f60\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u7ebf\u7a0b\u4e2d\u8fd0\u884c\u957f\u65f6\u95f4\u6267\u884c\u7684\u51fd\u6570\uff0c\u5e76\u901a\u8fc7\u4e3b\u7ebf\u7a0b\u76d1\u63a7\u8d85\u65f6\u3002\u4e00\u65e6\u8d85\u65f6\u5230\u8fbe\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e2d\u65ad\u7ebf\u7a0b\u6765\u505c\u6b62\u4efb\u52a1\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import threading\nimport time\n\ndef long_running_function():\n    # \u6a21\u62df\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\n    time.sleep(10)\n    print(\"Function finished\")\n\ndef run_with_timeout(func, timeout):\n    # \u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u6765\u8fd0\u884c\u51fd\u6570\n    thread = threading.Thread(target=func)\n    thread.start()\n    thread.join(timeout)  # \u7b49\u5f85\u6307\u5b9a\u65f6\u95f4\n\n    if thread.is_alive():  # \u5982\u679c\u7ebf\u7a0b\u8fd8\u5728\u8fd0\u884c\uff0c\u8bf4\u660e\u8d85\u65f6\n        print(\"Function timed out\")\n        # \u505c\u6b62\u7ebf\u7a0b\uff1a\u7ebf\u7a0b\u672c\u8eab\u4e0d\u80fd\u88ab\u76f4\u63a5\u4e2d\u6b62\uff0c\u9700\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u6e05\u7406\n        # \u8fd9\u91cc\u53ea\u662f\u793a\u610f\uff0c\u5b9e\u9645\u4e2d\u9700\u7528\u7ebf\u7a0b\u95f4\u901a\u4fe1\u6216\u6807\u5fd7\u4f4d\u63a7\u5236\n        return False\n    return True\n\n# \u8c03\u7528\u5e26\u6709\u8d85\u65f6\u7684\u51fd\u6570\nrun_with_timeout(long_running_function, timeout=5)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>threading.Thread(target=func)<\/code><\/strong>\uff1a\u5c06 <code>func<\/code> \u4f5c\u4e3a\u76ee\u6807\u51fd\u6570\u653e\u5165\u4e00\u4e2a\u5355\u72ec\u7684\u7ebf\u7a0b\u4e2d\u6267\u884c\u3002<\/li>\n\n\n\n<li><strong><code>thread.join(timeout)<\/code><\/strong>\uff1a\u4e3b\u7ebf\u7a0b\u7b49\u5f85 <code>timeout<\/code> \u79d2\uff0c\u5982\u679c\u7ebf\u7a0b\u6ca1\u6709\u7ed3\u675f\uff0c<code>is_alive()<\/code> \u4f1a\u8fd4\u56de <code>True<\/code>\uff0c\u8fd9\u65f6\u8bf4\u660e\u8d85\u65f6\u3002<\/li>\n\n\n\n<li>\u7531\u4e8e Python \u4e0d\u5141\u8bb8\u76f4\u63a5\u4e2d\u6b62\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u56e0\u6b64\u9700\u8981\u4f9d\u8d56\u5176\u4ed6\u673a\u5236\uff08\u5982\u7ebf\u7a0b\u95f4\u901a\u4fe1\u6216\u6807\u5fd7\u4f4d\uff09\u6765\u505c\u6b62\u7ebf\u7a0b\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u4f7f\u7528 <code>concurrent.futures<\/code> \u6a21\u5757<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>concurrent.futures<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u9ad8\u5c42\u6b21\u7684\u5f02\u6b65\u4efb\u52a1\u6267\u884c\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>ThreadPoolExecutor<\/code> \u6216 <code>ProcessPoolExecutor<\/code> \u6765\u6267\u884c\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\uff0c\u5e76\u901a\u8fc7 <code>timeout<\/code> \u53c2\u6570\u63a7\u5236\u8d85\u65f6\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import concurrent.futures\nimport time\n\ndef long_running_function():\n    time.sleep(10)\n    print(\"Function finished\")\n\ndef run_with_timeout(func, timeout):\n    with concurrent.futures.ThreadPoolExecutor() as executor:\n        future = executor.submit(func)\n        try:\n            future.result(timeout=timeout)  # \u8bbe\u7f6e\u8d85\u65f6\n        except concurrent.futures.TimeoutError:\n            print(\"Function timed out\")\n\n# \u8c03\u7528\u5e26\u6709\u8d85\u65f6\u7684\u51fd\u6570\nrun_with_timeout(long_running_function, timeout=5)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>ThreadPoolExecutor().submit(func)<\/code><\/strong>\uff1a\u5728\u7ebf\u7a0b\u6c60\u4e2d\u63d0\u4ea4 <code>func<\/code> \u51fd\u6570\u6267\u884c\u3002<\/li>\n\n\n\n<li><strong><code>future.result(timeout=timeout)<\/code><\/strong>\uff1a\u7b49\u5f85\u51fd\u6570\u6267\u884c\u7684\u7ed3\u679c\uff0c\u5982\u679c\u8d85\u65f6\u4f1a\u629b\u51fa <code>TimeoutError<\/code>\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u4f7f\u7528 <code>asyncio<\/code> \u6a21\u5757\uff08\u9002\u7528\u4e8e\u5f02\u6b65\u4efb\u52a1\uff09<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5728\u4f7f\u7528\u5f02\u6b65\u4ee3\u7801\uff08<code>asyncio<\/code>\uff09\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>asyncio.wait_for<\/code> \u6765\u8bbe\u7f6e\u8d85\u65f6\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import asyncio\n\nasync def long_running_function():\n    await asyncio.sleep(10)\n    print(\"Function finished\")\n\nasync def run_with_timeout(func, timeout):\n    try:\n        await asyncio.wait_for(func(), timeout=timeout)\n    except asyncio.TimeoutError:\n        print(\"Function timed out\")\n\n# \u8c03\u7528\u5e26\u6709\u8d85\u65f6\u7684\u5f02\u6b65\u51fd\u6570\nasyncio.run(run_with_timeout(long_running_function, timeout=5))<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>asyncio.wait_for()<\/code><\/strong>\uff1a\u7528\u4e8e\u8bbe\u7f6e\u5f02\u6b65\u4efb\u52a1\u7684\u8d85\u65f6\uff0c\u5982\u679c\u8d85\u8fc7\u6307\u5b9a\u7684\u65f6\u95f4\uff0c<code>TimeoutError<\/code> \u4f1a\u88ab\u629b\u51fa\u3002<\/li>\n\n\n\n<li>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u5f02\u6b65 I\/O \u64cd\u4f5c\u7684\u573a\u666f\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>signal<\/code> \u6a21\u5757<\/strong>\uff1a\u9002\u7528\u4e8e UNIX \u7cfb\u7edf\uff0c\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u4e0d\u80fd\u5728 Windows \u4e0a\u4f7f\u7528\u3002<\/li>\n\n\n\n<li><strong><code>threading<\/code> \u6a21\u5757<\/strong>\uff1a\u9002\u7528\u4e8e\u4e00\u822c\u7684\u591a\u7ebf\u7a0b\u4efb\u52a1\uff0c\u9002\u5408 Python 2.x \u548c 3.x\uff0c\u4f46\u4e2d\u6b62\u7ebf\u7a0b\u7684\u673a\u5236\u8f83\u4e3a\u590d\u6742\u3002<\/li>\n\n\n\n<li><strong><code>concurrent.futures<\/code> \u6a21\u5757<\/strong>\uff1a\u66f4\u9ad8\u7ea7\u7684\u7ebf\u7a0b\u6c60\u548c\u8fdb\u7a0b\u6c60\uff0c\u4f7f\u7528\u8d77\u6765\u975e\u5e38\u7b80\u6d01\u3002<\/li>\n\n\n\n<li><strong><code>asyncio<\/code> \u6a21\u5757<\/strong>\uff1a\u4e13\u95e8\u7528\u4e8e\u5904\u7406\u5f02\u6b65\u4efb\u52a1\uff0c\u9002\u5408 I\/O \u5bc6\u96c6\u578b\u5e94\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u80fd\u591f\u6709\u6548\u5730\u5e2e\u52a9\u4f60\u63a7\u5236\u957f\u65f6\u95f4\u8fd0\u884c\u7684 Python \u51fd\u6570\uff0c\u5e76\u5728\u8d85\u65f6\u540e\u53d6\u6d88\u5b83\u4eec\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\u53d6\u51b3\u4e8e\u4f60\u7684\u5e94\u7528\u573a\u666f\uff0c\u4f8b\u5982\u540c\u6b65\u4efb\u52a1\u3001\u5f02\u6b65\u4efb\u52a1\u6216\u9700\u8981\u8de8\u5e73\u53f0\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python \u4e2d\uff0c\u6709\u65f6\u4f60\u9700\u8981\u53d6\u6d88\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u51fd\u6570\u6216\u64cd\u4f5c\uff0c\u5c24\u5176\u662f\u5f53\u7a0b\u5e8f\u9047\u5230\u65e0\u6cd5\u63a7\u5236\u7684\u5916\u90e8\u4e8b\u4ef6\u6216\u9700\u8981\u54cd\u5e94\u7528\u6237\u8bf7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[],"class_list":["post-773","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/773","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=773"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/773\/revisions"}],"predecessor-version":[{"id":774,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/773\/revisions\/774"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}