{"id":1014152,"date":"2024-12-27T11:53:49","date_gmt":"2024-12-27T03:53:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1014152.html"},"modified":"2024-12-27T11:53:51","modified_gmt":"2024-12-27T03:53:51","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e7%a8%8b%e5%ba%8f%e8%b6%85%e6%97%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1014152.html","title":{"rendered":"python\u5982\u4f55\u5224\u65ad\u7a0b\u5e8f\u8d85\u65f6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094354\/37a30e0d-0116-4db6-98a2-a8a9dbe1c68c.webp\" alt=\"python\u5982\u4f55\u5224\u65ad\u7a0b\u5e8f\u8d85\u65f6\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5224\u65ad\u7a0b\u5e8f\u662f\u5426\u8d85\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4fe1\u53f7\u5904\u7406\u3001\u7ebf\u7a0b\u548c\u8ba1\u65f6\u5668\u3001\u5f02\u6b65\u7f16\u7a0b\u7b49\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528<code>signal<\/code>\u6a21\u5757\u5904\u7406\u8d85\u65f6\u3001\u7ed3\u5408\u7ebf\u7a0b\u548c\u8ba1\u65f6\u5668\u8bbe\u5b9a\u8d85\u65f6\u65f6\u95f4\u662f\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u5728\u9884\u8bbe\u65f6\u95f4\u5185\u81ea\u52a8\u68c0\u67e5\u662f\u5426\u8d85\u65f6\u5e76\u91c7\u53d6\u76f8\u5e94\u7684\u63aa\u65bd\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>signal<\/code>\u6a21\u5757\u5904\u7406\u8d85\u65f6<\/p>\n<\/p>\n<p><p><code>signal<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u5904\u7406\u8d85\u65f6\u7684\u7b80\u5355\u65b9\u6cd5\u3002\u901a\u8fc7\u8bbe\u7f6e\u4fe1\u53f7\u548c\u4fe1\u53f7\u5904\u7406\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u6267\u884c\u8d85\u8fc7\u6307\u5b9a\u65f6\u95f4\u65f6\u5f15\u53d1\u5f02\u5e38\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8eUnix\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8bbe\u7f6e\u4fe1\u53f7\u5904\u7406\u5668<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>signal.signal()<\/code>\u51fd\u6570\u6765\u5b9a\u4e49\u4e00\u4e2a\u4fe1\u53f7\u5904\u7406\u5668\u3002\u4fe1\u53f7\u5904\u7406\u5668\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u5f53\u4fe1\u53f7\u88ab\u63a5\u6536\u5230\u65f6\u4f1a\u88ab\u8c03\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bbe\u7f6e\u4fe1\u53f7\u5b9a\u65f6\u5668<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>signal.alarm()<\/code>\u51fd\u6570\u6765\u8bbe\u5b9a\u4e00\u4e2a\u5b9a\u65f6\u5668\u3002\u5f53\u65f6\u95f4\u5230\u8fbe\u65f6\uff0c\u4f1a\u53d1\u9001\u4e00\u4e2a<code>SIGALRM<\/code>\u4fe1\u53f7\u7ed9\u5f53\u524d\u7684\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import signal<\/p>\n<h2><strong>\u5b9a\u4e49\u8d85\u65f6\u5904\u7406\u51fd\u6570<\/strong><\/h2>\n<p>def handler(signum, frame):<\/p>\n<p>    r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se TimeoutError(&quot;\u7a0b\u5e8f\u8d85\u65f6&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u4fe1\u53f7\u5904\u7406\u5668<\/strong><\/h2>\n<p>signal.signal(signal.SIGALRM, handler)<\/p>\n<h2><strong>\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u4e3a5\u79d2<\/strong><\/h2>\n<p>signal.alarm(5)<\/p>\n<p>try:<\/p>\n<p>    # \u653e\u7f6e\u9700\u8981\u76d1\u63a7\u8d85\u65f6\u7684\u4ee3\u7801<\/p>\n<p>    while True:<\/p>\n<p>        pass<\/p>\n<p>except TimeoutError:<\/p>\n<p>    print(&quot;\u7a0b\u5e8f\u6267\u884c\u8d85\u65f6&quot;)<\/p>\n<p>finally:<\/p>\n<p>    # \u53d6\u6d88\u4fe1\u53f7\u5b9a\u65f6\u5668<\/p>\n<p>    signal.alarm(0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u7ebf\u7a0b\u548c\u8ba1\u65f6\u5668<\/p>\n<\/p>\n<p><p>\u7ebf\u7a0b\u548c\u8ba1\u65f6\u5668\u7684\u7ed3\u5408\u4f7f\u7528\u4e5f\u53ef\u4ee5\u6709\u6548\u5904\u7406\u7a0b\u5e8f\u8d85\u65f6\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u8de8\u5e73\u53f0\u5e94\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u7ebf\u7a0b<\/strong><\/p>\n<\/p>\n<p><p>\u5c06\u9700\u8981\u76d1\u63a7\u8d85\u65f6\u7684\u4ee3\u7801\u653e\u5165\u4e00\u4e2a\u5355\u72ec\u7684\u7ebf\u7a0b\u4e2d\u8fd0\u884c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u8ba1\u65f6\u5668<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>threading.Timer<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u8ba1\u65f6\u5668\uff0c\u5f53\u8ba1\u65f6\u5668\u5230\u8fbe\u65f6\u7ec8\u6b62\u7ebf\u7a0b\u6216\u6267\u884c\u5176\u5b83\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<h2><strong>\u5b9a\u4e49\u8d85\u65f6\u5904\u7406\u51fd\u6570<\/strong><\/h2>\n<p>def timeout():<\/p>\n<p>    print(&quot;\u7a0b\u5e8f\u6267\u884c\u8d85\u65f6&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u8fd0\u884c\u76ee\u6807\u51fd\u6570<\/strong><\/h2>\n<p>def target_function():<\/p>\n<p>    while True:<\/p>\n<p>        pass<\/p>\n<p>t = threading.Thread(target=target_function)<\/p>\n<p>t.start()<\/p>\n<h2><strong>\u8bbe\u7f6e\u8ba1\u65f6\u5668\uff0c5\u79d2\u540e\u8c03\u7528timeout\u51fd\u6570<\/strong><\/h2>\n<p>timer = threading.Timer(5, timeout)<\/p>\n<p>timer.start()<\/p>\n<h2><strong>\u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f<\/strong><\/h2>\n<p>t.join()<\/p>\n<p>timer.cancel()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5f02\u6b65\u7f16\u7a0b<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5f02\u6b65\u7f16\u7a0b\u4efb\u52a1\uff0c\u53ef\u4ee5\u4f7f\u7528<code>asyncio<\/code>\u5e93\u6765\u7ba1\u7406\u4efb\u52a1\u7684\u8d85\u65f6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u5f02\u6b65\u4efb\u52a1<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>asyncio.create_task()<\/code>\u6765\u521b\u5efa\u5f02\u6b65\u4efb\u52a1\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ba1\u7406\u8d85\u65f6<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>asyncio.wait_for()<\/code>\u6765\u8bbe\u5b9a\u4efb\u52a1\u7684\u8d85\u65f6\u65f6\u95f4\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def main():<\/p>\n<p>    try:<\/p>\n<p>        # \u7b49\u5f85\u4efb\u52a1\u7684\u540c\u65f6\u8bbe\u5b9a\u8d85\u65f6\u65f6\u95f4<\/p>\n<p>        await asyncio.wait_for(async_task(), timeout=5)<\/p>\n<p>    except asyncio.TimeoutError:<\/p>\n<p>        print(&quot;\u7a0b\u5e8f\u6267\u884c\u8d85\u65f6&quot;)<\/p>\n<p>async def async_task():<\/p>\n<p>    while True:<\/p>\n<p>        await asyncio.sleep(1)<\/p>\n<h2><strong>\u8fd0\u884c\u5f02\u6b65\u4efb\u52a1<\/strong><\/h2>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/p>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u66f4\u52a0\u4f18\u96c5\u7684\u8d85\u65f6\u5904\u7406\u673a\u5236\uff0c\u901a\u8fc7\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6765\u8bbe\u5b9a\u8d85\u65f6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>__enter__<\/code>\u548c<code>__exit__<\/code>\u65b9\u6cd5\u6765\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u5b9e\u73b0\u8d85\u65f6\u529f\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7<code>with<\/code>\u8bed\u53e5\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u5728\u4ee3\u7801\u5757\u4e2d\u76d1\u63a7\u8d85\u65f6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>class Timeout:<\/p>\n<p>    def __init__(self, seconds):<\/p>\n<p>        self.seconds = seconds<\/p>\n<p>    def __enter__(self):<\/p>\n<p>        self.start_time = time.time()<\/p>\n<p>    def __exit__(self, exc_type, exc_value, traceback):<\/p>\n<p>        elapsed_time = time.time() - self.start_time<\/p>\n<p>        if elapsed_time &gt; self.seconds:<\/p>\n<p>            print(&quot;\u7a0b\u5e8f\u6267\u884c\u8d85\u65f6&quot;)<\/p>\n<p>with Timeout(5):<\/p>\n<p>    while True:<\/p>\n<p>        pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u5224\u65adPython\u7a0b\u5e8f\u8d85\u65f6\u7684\u65b9\u6cd5\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u64cd\u4f5c\u7cfb\u7edf\u7684\u7279\u6027\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>signal<\/code>\u6216<code>threading<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u8d85\u65f6\u5224\u65ad\uff1b\u5bf9\u4e8e\u590d\u6742\u7684\u5f02\u6b65\u4efb\u52a1\uff0c<code>asyncio<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6e\u7a0b\u5e8f\u8d85\u65f6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>signal<\/code>\u6a21\u5757\u6765\u8bbe\u7f6e\u7a0b\u5e8f\u7684\u8d85\u65f6\u3002\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u8d85\u65f6\u5904\u7406\u51fd\u6570\uff0c\u5e76\u4f7f\u7528<code>signal.alarm()<\/code>\u6765\u8bbe\u5b9a\u8d85\u65f6\u7684\u79d2\u6570\u3002\u5982\u679c\u8d85\u65f6\u672a\u80fd\u5b8c\u6210\uff0c\u5904\u7406\u51fd\u6570\u5c06\u4f1a\u88ab\u89e6\u53d1\uff0c\u4ece\u800c\u53ef\u4ee5\u5904\u7406\u8d85\u65f6\u903b\u8f91\u3002<\/p>\n<p><strong>\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u5982\u4f55\u5904\u7406\u8d85\u65f6\uff1f<\/strong><br \/>\u5728\u591a\u7ebf\u7a0b\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u4e2d\u7684<code>Event<\/code>\u5bf9\u8c61\u6765\u5b9e\u73b0\u8d85\u65f6\u673a\u5236\u3002\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u5b9a\u65f6\u5668\uff0c\u5728\u8fbe\u5230\u8d85\u65f6\u65f6\u95f4\u65f6\uff0c\u4e3b\u7ebf\u7a0b\u53ef\u4ee5\u68c0\u67e5\u7ebf\u7a0b\u662f\u5426\u5b8c\u6210\u5e76\u6839\u636e\u60c5\u51b5\u91c7\u53d6\u76f8\u5e94\u63aa\u65bd\uff0c\u4f8b\u5982\u7ec8\u6b62\u7ebf\u7a0b\u6216\u8bb0\u5f55\u9519\u8bef\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5e93\u53ef\u4ee5\u7b80\u5316\u8d85\u65f6\u5904\u7406\u7684\u8fc7\u7a0b\uff1f<\/strong><br \/>\u662f\u7684\uff0c<code>concurrent.futures<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u6267\u884c\u51fd\u6570\u5e76\u8bbe\u7f6e\u8d85\u65f6\u3002\u4f7f\u7528<code>ThreadPoolExecutor<\/code>\u6216<code>ProcessPoolExecutor<\/code>\u53ef\u4ee5\u63d0\u4ea4\u4efb\u52a1\u5e76\u4f7f\u7528<code>result(timeout)<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u7ed3\u679c\uff0c\u5982\u679c\u8d85\u65f6\u5219\u4f1a\u629b\u51fa<code>TimeoutError<\/code>\u5f02\u5e38\uff0c\u4fbf\u4e8e\u5904\u7406\u8d85\u65f6\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5224\u65ad\u7a0b\u5e8f\u662f\u5426\u8d85\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4fe1\u53f7\u5904\u7406\u3001\u7ebf\u7a0b\u548c\u8ba1\u65f6\u5668\u3001\u5f02\u6b65\u7f16\u7a0b\u7b49\u65b9\u6cd5\u3002 \u5176\u4e2d\uff0c\u4f7f\u7528signal\u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":1014160,"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\/1014152"}],"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=1014152"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1014152\/revisions"}],"predecessor-version":[{"id":1014163,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1014152\/revisions\/1014163"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1014160"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1014152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1014152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1014152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}