{"id":1186348,"date":"2025-01-15T19:51:27","date_gmt":"2025-01-15T11:51:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1186348.html"},"modified":"2025-01-15T19:51:30","modified_gmt":"2025-01-15T11:51:30","slug":"python%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%ae%e8%b6%85%e6%97%b6%e6%97%b6%e9%97%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1186348.html","title":{"rendered":"python\u5982\u4f55\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25135202\/b376c275-f8e9-4d41-8b0c-dc5ad17fc841.webp\" alt=\"python\u5982\u4f55\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>requests<\/code>\u5e93\u3001<code>socket<\/code>\u5e93\u3001<code>threading<\/code>\u5e93\u7b49\u3002<\/strong> \u5176\u4e2d\u6700\u5e38\u7528\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>requests<\/code>\u5e93\u7684<code>timeout<\/code>\u53c2\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>try:<\/p>\n<p>    response = requests.get(&#39;https:\/\/example.com&#39;, timeout=5)<\/p>\n<p>    print(response.status_code)<\/p>\n<p>except requests.Timeout:<\/p>\n<p>    print(&#39;The request timed out&#39;)<\/p>\n<p>except requests.RequestException as e:<\/p>\n<p>    print(f&#39;An error occurred: {e}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>timeout=5<\/code>\u8bbe\u7f6e\u4e86\u8bf7\u6c42\u7684\u8d85\u65f6\u65f6\u95f4\u4e3a5\u79d2\u3002\u5982\u679c\u8bf7\u6c42\u57285\u79d2\u5185\u6ca1\u6709\u5b8c\u6210\uff0c\u4f1a\u5f15\u53d1<code>requests.Timeout<\/code>\u5f02\u5e38\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528requests\u5e93<\/strong><\/h2>\n<p><h2>1\u3001\u8bbe\u7f6e\u8bf7\u6c42\u8d85\u65f6\u65f6\u95f4<\/h2>\n<\/p>\n<p><p><code>requests<\/code>\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684HTTP\u5e93\uff0c\u5b83\u5141\u8bb8\u4f60\u53d1\u9001HTTP\u8bf7\u6c42\u3002<code>timeout<\/code>\u53c2\u6570\u53ef\u4ee5\u7528\u6765\u8bbe\u7f6e\u8bf7\u6c42\u7684\u8d85\u65f6\u65f6\u95f4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>try:<\/p>\n<p>    response = requests.get(&#39;https:\/\/example.com&#39;, timeout=5)<\/p>\n<p>    print(response.status_code)<\/p>\n<p>except requests.Timeout:<\/p>\n<p>    print(&#39;The request timed out&#39;)<\/p>\n<p>except requests.RequestException as e:<\/p>\n<p>    print(f&#39;An error occurred: {e}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>timeout=5<\/code>\u8bbe\u7f6e\u4e86\u8bf7\u6c42\u7684\u8d85\u65f6\u65f6\u95f4\u4e3a5\u79d2\u3002\u5982\u679c\u8bf7\u6c42\u57285\u79d2\u5185\u6ca1\u6709\u5b8c\u6210\uff0c\u4f1a\u5f15\u53d1<code>requests.Timeout<\/code>\u5f02\u5e38\uff0c\u4ece\u800c\u53ef\u4ee5\u8fdb\u884c\u9519\u8bef\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u5206\u522b\u8bbe\u7f6e\u8fde\u63a5\u548c\u8bfb\u53d6\u8d85\u65f6\u65f6\u95f4<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5206\u522b\u8bbe\u7f6e\u8fde\u63a5\uff08connection\uff09\u548c\u8bfb\u53d6\uff08read\uff09\u7684\u8d85\u65f6\u65f6\u95f4\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a\u5143\u7ec4\u7ed9<code>timeout<\/code>\u53c2\u6570\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>try:<\/p>\n<p>    response = requests.get(&#39;https:\/\/example.com&#39;, timeout=(3.05, 27))<\/p>\n<p>    print(response.status_code)<\/p>\n<p>except requests.Timeout:<\/p>\n<p>    print(&#39;The request timed out&#39;)<\/p>\n<p>except requests.RequestException as e:<\/p>\n<p>    print(f&#39;An error occurred: {e}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>timeout=(3.05, 27)<\/code>\u8868\u793a\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\u4e3a3.05\u79d2\uff0c\u8bfb\u53d6\u8d85\u65f6\u65f6\u95f4\u4e3a27\u79d2\u3002\u8fd9\u6837\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u63a7\u5236\u4e0d\u540c\u7c7b\u578b\u7684\u8d85\u65f6\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528socket\u5e93<\/strong><\/h2>\n<p><h2>1\u3001\u8bbe\u7f6e\u9ed8\u8ba4\u8d85\u65f6\u65f6\u95f4<\/h2>\n<\/p>\n<p><p><code>socket<\/code>\u5e93\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u53ef\u4ee5\u7528\u4e8e\u5e95\u5c42\u7f51\u7edc\u7f16\u7a0b\u3002\u53ef\u4ee5\u4f7f\u7528<code>socket.setdefaulttimeout()<\/code>\u51fd\u6570\u6765\u8bbe\u7f6e\u9ed8\u8ba4\u7684\u8d85\u65f6\u65f6\u95f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<h2><strong>\u8bbe\u7f6e\u9ed8\u8ba4\u8d85\u65f6\u65f6\u95f4\u4e3a5\u79d2<\/strong><\/h2>\n<p>socket.setdefaulttimeout(5)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684socket\u5bf9\u8c61<\/strong><\/h2>\n<p>s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<h2><strong>\u8fde\u63a5\u5230\u8fdc\u7a0b\u670d\u52a1\u5668<\/strong><\/h2>\n<p>try:<\/p>\n<p>    s.connect((&#39;example.com&#39;, 80))<\/p>\n<p>    print(&#39;Connected successfully&#39;)<\/p>\n<p>except socket.timeout:<\/p>\n<p>    print(&#39;The connection timed out&#39;)<\/p>\n<p>finally:<\/p>\n<p>    s.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>socket.setdefaulttimeout(5)<\/code>\u8bbe\u7f6e\u4e86\u9ed8\u8ba4\u7684\u8d85\u65f6\u65f6\u95f4\u4e3a5\u79d2\u3002\u5982\u679c\u8fde\u63a5\u64cd\u4f5c\u57285\u79d2\u5185\u6ca1\u6709\u5b8c\u6210\uff0c\u4f1a\u5f15\u53d1<code>socket.timeout<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u8bbe\u7f6e\u7279\u5b9a\u64cd\u4f5c\u7684\u8d85\u65f6\u65f6\u95f4<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u8bbe\u7f6e\u9ed8\u8ba4\u8d85\u65f6\u65f6\u95f4\uff0c\u8fd8\u53ef\u4ee5\u4e3a\u7279\u5b9a\u7684\u64cd\u4f5c\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u3002\u901a\u8fc7\u4f7f\u7528<code>settimeout()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4e3a\u67d0\u4e2asocket\u5bf9\u8c61\u8bbe\u7f6e\u7279\u5b9a\u7684\u8d85\u65f6\u65f6\u95f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684socket\u5bf9\u8c61<\/strong><\/h2>\n<p>s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<h2><strong>\u8bbe\u7f6e\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\u4e3a5\u79d2<\/strong><\/h2>\n<p>s.settimeout(5)<\/p>\n<h2><strong>\u8fde\u63a5\u5230\u8fdc\u7a0b\u670d\u52a1\u5668<\/strong><\/h2>\n<p>try:<\/p>\n<p>    s.connect((&#39;example.com&#39;, 80))<\/p>\n<p>    print(&#39;Connected successfully&#39;)<\/p>\n<p>except socket.timeout:<\/p>\n<p>    print(&#39;The connection timed out&#39;)<\/p>\n<p>finally:<\/p>\n<p>    s.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>s.settimeout(5)<\/code>\u8bbe\u7f6e\u4e86\u8fde\u63a5\u64cd\u4f5c\u7684\u8d85\u65f6\u65f6\u95f4\u4e3a5\u79d2\u3002\u5982\u679c\u8fde\u63a5\u64cd\u4f5c\u57285\u79d2\u5185\u6ca1\u6709\u5b8c\u6210\uff0c\u4f1a\u5f15\u53d1<code>socket.timeout<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528threading\u5e93<\/strong><\/h2>\n<p><h2>1\u3001\u4f7f\u7528\u7ebf\u7a0b\u548c\u4e8b\u4ef6\u5bf9\u8c61<\/h2>\n<\/p>\n<p><p><code>threading<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u591a\u7ebf\u7a0b\u652f\u6301\uff0c\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u5e26\u6709\u8d85\u65f6\u529f\u80fd\u7684\u64cd\u4f5c\u3002\u901a\u8fc7\u4f7f\u7528\u7ebf\u7a0b\u548c\u4e8b\u4ef6\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u8d85\u65f6\u63a7\u5236\u903b\u8f91\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def long_running_task(event):<\/p>\n<p>    # \u6a21\u62df\u4e00\u4e2a\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1<\/p>\n<p>    for i in range(10):<\/p>\n<p>        if event.is_set():<\/p>\n<p>            print(&#39;Task was cancelled&#39;)<\/p>\n<p>            return<\/p>\n<p>        print(f&#39;Running task {i}&#39;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>    print(&#39;Task completed&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e8b\u4ef6\u5bf9\u8c61<\/strong><\/h2>\n<p>event = threading.Event()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u6765\u6267\u884c\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1<\/strong><\/h2>\n<p>thread = threading.Thread(target=long_running_task, args=(event,))<\/p>\n<p>thread.start()<\/p>\n<h2><strong>\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4<\/strong><\/h2>\n<p>timeout = 5<\/p>\n<h2><strong>\u7b49\u5f85\u8d85\u65f6\u65f6\u95f4\u8fc7\u53bb\u6216\u4efb\u52a1\u5b8c\u6210<\/strong><\/h2>\n<p>thread.join(timeout)<\/p>\n<h2><strong>\u68c0\u67e5\u4efb\u52a1\u662f\u5426\u8fd8\u5728\u8fd0\u884c<\/strong><\/h2>\n<p>if thread.is_alive():<\/p>\n<p>    print(&#39;Task is still running, cancelling it&#39;)<\/p>\n<p>    event.set()<\/p>\n<p>    thread.join()<\/p>\n<p>else:<\/p>\n<p>    print(&#39;Task completed within timeout&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528\u4e86<code>threading.Event<\/code>\u5bf9\u8c61\u6765\u5b9e\u73b0\u4efb\u52a1\u7684\u53d6\u6d88\u3002\u4e3b\u7ebf\u7a0b\u7b49\u5f85\u4efb\u52a1\u7ebf\u7a0b\u8fd0\u884c\u6307\u5b9a\u7684\u8d85\u65f6\u65f6\u95f4\uff0c\u5982\u679c\u4efb\u52a1\u7ebf\u7a0b\u5728\u8d85\u65f6\u65f6\u95f4\u5185\u6ca1\u6709\u5b8c\u6210\uff0c\u4e3b\u7ebf\u7a0b\u4f1a\u8bbe\u7f6e\u4e8b\u4ef6\u5bf9\u8c61\uff0c\u4ece\u800c\u901a\u77e5\u4efb\u52a1\u7ebf\u7a0b\u53d6\u6d88\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u4f7f\u7528asyncio\u5e93<\/strong><\/h2>\n<p><h2>1\u3001\u8bbe\u7f6e\u5f02\u6b65\u64cd\u4f5c\u7684\u8d85\u65f6\u65f6\u95f4<\/h2>\n<\/p>\n<p><p><code>asyncio<\/code>\u5e93\u662fPython 3.4\u4e2d\u5f15\u5165\u7684\u7528\u4e8e\u7f16\u5199\u5f02\u6b65\u4ee3\u7801\u7684\u6807\u51c6\u5e93\u3002<code>asyncio.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t_for<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u8bbe\u7f6e\u5f02\u6b65\u64cd\u4f5c\u7684\u8d85\u65f6\u65f6\u95f4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def long_running_task():<\/p>\n<p>    for i in range(10):<\/p>\n<p>        print(f&#39;Running task {i}&#39;)<\/p>\n<p>        await asyncio.sleep(1)<\/p>\n<p>    print(&#39;Task completed&#39;)<\/p>\n<p>async def main():<\/p>\n<p>    try:<\/p>\n<p>        # \u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u4e3a5\u79d2<\/p>\n<p>        await asyncio.wait_for(long_running_task(), timeout=5)<\/p>\n<p>    except asyncio.TimeoutError:<\/p>\n<p>        print(&#39;The task timed out&#39;)<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u51fd\u6570<\/strong><\/h2>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>asyncio.wait_for<\/code>\u51fd\u6570\u7528\u4e8e\u8bbe\u7f6e\u5f02\u6b65\u64cd\u4f5c\u7684\u8d85\u65f6\u65f6\u95f4\u3002\u5982\u679c<code>long_running_task<\/code>\u57285\u79d2\u5185\u6ca1\u6709\u5b8c\u6210\uff0c\u4f1a\u5f15\u53d1<code>asyncio.TimeoutError<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u4f7f\u7528asyncio.TimeoutError\u8fdb\u884c\u9519\u8bef\u5904\u7406<\/h2>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>asyncio<\/code>\u65f6\uff0c\u8d85\u65f6\u901a\u5e38\u8868\u793a\u67d0\u4e2a\u64cd\u4f5c\u82b1\u8d39\u7684\u65f6\u95f4\u6bd4\u9884\u671f\u7684\u8981\u957f\u3002\u53ef\u4ee5\u901a\u8fc7\u6355\u83b7<code>asyncio.TimeoutError<\/code>\u5f02\u5e38\u6765\u5904\u7406\u8d85\u65f6\u60c5\u51b5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def long_running_task():<\/p>\n<p>    for i in range(10):<\/p>\n<p>        print(f&#39;Running task {i}&#39;)<\/p>\n<p>        await asyncio.sleep(1)<\/p>\n<p>    print(&#39;Task completed&#39;)<\/p>\n<p>async def main():<\/p>\n<p>    try:<\/p>\n<p>        # \u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u4e3a5\u79d2<\/p>\n<p>        await asyncio.wait_for(long_running_task(), timeout=5)<\/p>\n<p>    except asyncio.TimeoutError:<\/p>\n<p>        print(&#39;The task timed out&#39;)<\/p>\n<p>    else:<\/p>\n<p>        print(&#39;The task completed successfully&#39;)<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u51fd\u6570<\/strong><\/h2>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6355\u83b7\u5230<code>asyncio.TimeoutError<\/code>\u5f02\u5e38\u540e\uff0c\u53ef\u4ee5\u6267\u884c\u76f8\u5e94\u7684\u9519\u8bef\u5904\u7406\u903b\u8f91\u3002\u5982\u679c\u4efb\u52a1\u5728\u8d85\u65f6\u65f6\u95f4\u5185\u5b8c\u6210\uff0c\u5219\u4f1a\u6267\u884c<code>else<\/code>\u5757\u4e2d\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u4f7f\u7528concurrent.futures\u5e93<\/strong><\/h2>\n<p><h2>1\u3001\u8bbe\u7f6e\u7ebf\u7a0b\u6c60\u6267\u884c\u5668\u7684\u8d85\u65f6\u65f6\u95f4<\/h2>\n<\/p>\n<p><p><code>concurrent.futures<\/code>\u5e93\u63d0\u4f9b\u4e86\u9ad8\u5c42\u6b21\u7684\u63a5\u53e3\u6765\u7ba1\u7406\u7ebf\u7a0b\u548c\u8fdb\u7a0b\u6c60\u3002\u53ef\u4ee5\u4f7f\u7528<code>ThreadPoolExecutor<\/code>\u548c<code>ProcessPoolExecutor<\/code>\u6765\u6267\u884c\u5e26\u6709\u8d85\u65f6\u529f\u80fd\u7684\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor, TimeoutError<\/p>\n<p>def long_running_task():<\/p>\n<p>    import time<\/p>\n<p>    for i in range(10):<\/p>\n<p>        print(f&#39;Running task {i}&#39;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>    return &#39;Task completed&#39;<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u6c60\u6267\u884c\u5668<\/strong><\/h2>\n<p>with ThreadPoolExecutor() as executor:<\/p>\n<p>    # \u63d0\u4ea4\u4efb\u52a1\u5e76\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u4e3a5\u79d2<\/p>\n<p>    future = executor.submit(long_running_task)<\/p>\n<p>    try:<\/p>\n<p>        result = future.result(timeout=5)<\/p>\n<p>    except TimeoutError:<\/p>\n<p>        print(&#39;The task timed out&#39;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&#39;The task completed successfully: {result}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528<code>ThreadPoolExecutor<\/code>\u6765\u6267\u884c\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\uff0c\u5e76\u901a\u8fc7<code>future.result(timeout=5)<\/code>\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u3002\u5982\u679c\u4efb\u52a1\u57285\u79d2\u5185\u6ca1\u6709\u5b8c\u6210\uff0c\u4f1a\u5f15\u53d1<code>TimeoutError<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u8bbe\u7f6e\u8fdb\u7a0b\u6c60\u6267\u884c\u5668\u7684\u8d85\u65f6\u65f6\u95f4<\/h2>\n<\/p>\n<p><p>\u7c7b\u4f3c\u5730\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ProcessPoolExecutor<\/code>\u6765\u6267\u884c\u5e26\u6709\u8d85\u65f6\u529f\u80fd\u7684\u4efb\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ProcessPoolExecutor, TimeoutError<\/p>\n<p>def long_running_task():<\/p>\n<p>    import time<\/p>\n<p>    for i in range(10):<\/p>\n<p>        print(f&#39;Running task {i}&#39;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>    return &#39;Task completed&#39;<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u8fdb\u7a0b\u6c60\u6267\u884c\u5668<\/strong><\/h2>\n<p>with ProcessPoolExecutor() as executor:<\/p>\n<p>    # \u63d0\u4ea4\u4efb\u52a1\u5e76\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u4e3a5\u79d2<\/p>\n<p>    future = executor.submit(long_running_task)<\/p>\n<p>    try:<\/p>\n<p>        result = future.result(timeout=5)<\/p>\n<p>    except TimeoutError:<\/p>\n<p>        print(&#39;The task timed out&#39;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&#39;The task completed successfully: {result}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528<code>ProcessPoolExecutor<\/code>\u6765\u6267\u884c\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\uff0c\u5e76\u901a\u8fc7<code>future.result(timeout=5)<\/code>\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u3002\u5982\u679c\u4efb\u52a1\u57285\u79d2\u5185\u6ca1\u6709\u5b8c\u6210\uff0c\u4f1a\u5f15\u53d1<code>TimeoutError<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u4f7f\u7528signal\u5e93<\/strong><\/h2>\n<p><h2>1\u3001\u8bbe\u7f6e\u4fe1\u53f7\u5904\u7406\u5668<\/h2>\n<\/p>\n<p><p>\u5728Unix\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528<code>signal<\/code>\u5e93\u6765\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u3002\u901a\u8fc7\u8bbe\u7f6e\u4fe1\u53f7\u5904\u7406\u5668\uff0c\u53ef\u4ee5\u5728\u8d85\u65f6\u65f6\u95f4\u5185\u4e2d\u65ad\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import signal<\/p>\n<p>def handler(signum, frame):<\/p>\n<p>    raise TimeoutError(&#39;The operation timed out&#39;)<\/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>    # \u6a21\u62df\u4e00\u4e2a\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u64cd\u4f5c<\/p>\n<p>    import time<\/p>\n<p>    for i in range(10):<\/p>\n<p>        print(f&#39;Running operation {i}&#39;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>except TimeoutError as e:<\/p>\n<p>    print(e)<\/p>\n<p>finally:<\/p>\n<p>    # \u53d6\u6d88\u95f9\u949f<\/p>\n<p>    signal.alarm(0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528<code>signal.signal<\/code>\u51fd\u6570\u8bbe\u7f6e\u4fe1\u53f7\u5904\u7406\u5668\uff0c\u5e76\u901a\u8fc7<code>signal.alarm<\/code>\u51fd\u6570\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u3002\u5982\u679c\u64cd\u4f5c\u57285\u79d2\u5185\u6ca1\u6709\u5b8c\u6210\uff0c\u4f1a\u5f15\u53d1<code>TimeoutError<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u53d6\u6d88\u95f9\u949f<\/h2>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>signal<\/code>\u5e93\u65f6\uff0c\u52a1\u5fc5\u8981\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u53d6\u6d88\u95f9\u949f\uff0c\u4ee5\u907f\u514d\u540e\u7eed\u64cd\u4f5c\u53d7\u5230\u5f71\u54cd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import signal<\/p>\n<p>def handler(signum, frame):<\/p>\n<p>    raise TimeoutError(&#39;The operation timed out&#39;)<\/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>    # \u6a21\u62df\u4e00\u4e2a\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u64cd\u4f5c<\/p>\n<p>    import time<\/p>\n<p>    for i in range(10):<\/p>\n<p>        print(f&#39;Running operation {i}&#39;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>except TimeoutError as e:<\/p>\n<p>    print(e)<\/p>\n<p>finally:<\/p>\n<p>    # \u53d6\u6d88\u95f9\u949f<\/p>\n<p>    signal.alarm(0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528<code>signal.alarm(0)<\/code>\u53d6\u6d88\u95f9\u949f\uff0c\u4ee5\u786e\u4fdd\u540e\u7eed\u64cd\u4f5c\u4e0d\u4f1a\u53d7\u5230\u8d85\u65f6\u4fe1\u53f7\u7684\u5f71\u54cd\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/strong><\/h2>\n<p><h2>1\u3001\u4f7f\u7528timeout-decorator\u5e93<\/h2>\n<\/p>\n<p><p><code>timeout-decorator<\/code>\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u4e3a\u51fd\u6570\u6dfb\u52a0\u8d85\u65f6\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from timeout_decorator import timeout, TimeoutError<\/p>\n<p>@timeout(5)<\/p>\n<p>def long_running_task():<\/p>\n<p>    import time<\/p>\n<p>    for i in range(10):<\/p>\n<p>        print(f&#39;Running task {i}&#39;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>    return &#39;Task completed&#39;<\/p>\n<p>try:<\/p>\n<p>    result = long_running_task()<\/p>\n<p>except TimeoutError:<\/p>\n<p>    print(&#39;The task timed out&#39;)<\/p>\n<p>else:<\/p>\n<p>    print(f&#39;The task completed successfully: {result}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528<code>@timeout(5)<\/code>\u88c5\u9970\u5668\u4e3a<code>long_running_task<\/code>\u51fd\u6570\u6dfb\u52a0\u8d85\u65f6\u529f\u80fd\u3002\u5982\u679c\u51fd\u6570\u57285\u79d2\u5185\u6ca1\u6709\u5b8c\u6210\uff0c\u4f1a\u5f15\u53d1<code>TimeoutError<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u5b89\u88c5\u548c\u4f7f\u7528\u5176\u4ed6\u7b2c\u4e09\u65b9\u5e93<\/h2>\n<\/p>\n<p><p>\u9664\u4e86<code>timeout-decorator<\/code>\u5e93\u5916\uff0c\u8fd8\u6709\u8bb8\u591a\u5176\u4ed6\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u8d85\u65f6\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>gevent<\/code>\u5e93\u6765\u5b9e\u73b0\u5f02\u6b65\u8d85\u65f6\u63a7\u5236\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import gevent<\/p>\n<p>from gevent import Timeout<\/p>\n<p>def long_running_task():<\/p>\n<p>    import time<\/p>\n<p>    for i in range(10):<\/p>\n<p>        print(f&#39;Running task {i}&#39;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>    return &#39;Task completed&#39;<\/p>\n<p>timeout = Timeout(5)<\/p>\n<p>timeout.start()<\/p>\n<p>try:<\/p>\n<p>    result = gevent.spawn(long_running_task).get()<\/p>\n<p>except Timeout:<\/p>\n<p>    print(&#39;The task timed out&#39;)<\/p>\n<p>else:<\/p>\n<p>    print(f&#39;The task completed successfully: {result}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528<code>gevent.Timeout<\/code>\u7c7b\u6765\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\uff0c\u5e76\u901a\u8fc7<code>gevent.spawn<\/code>\u51fd\u6570\u6765\u6267\u884c\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\u3002\u5982\u679c\u4efb\u52a1\u57285\u79d2\u5185\u6ca1\u6709\u5b8c\u6210\uff0c\u4f1a\u5f15\u53d1<code>Timeout<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<h2><strong>\u516b\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>Python\u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\uff0c\u5305\u62ec\u4f7f\u7528<code>requests<\/code>\u5e93\u3001<code>socket<\/code>\u5e93\u3001<code>threading<\/code>\u5e93\u3001<code>asyncio<\/code>\u5e93\u3001<code>concurrent.futures<\/code>\u5e93\u3001<code>signal<\/code>\u5e93\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u5982<code>timeout-decorator<\/code>\u548c<code>gevent<\/code>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u9002\u5408\u7684\u8d85\u65f6\u63a7\u5236\u65b9\u6cd5\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u7a0b\u5e8f\u7684\u53ef\u9760\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>requests<\/code>\u5e93\u7684<code>timeout<\/code>\u53c2\u6570\u662f\u6700\u5e38\u7528\u4e14\u7b80\u5355\u7684\u65b9\u6cd5<\/strong>\uff0c\u9002\u7528\u4e8e\u5927\u90e8\u5206HTTP\u8bf7\u6c42\u573a\u666f\u3002\u800c\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u5f02\u6b65\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>asyncio<\/code>\u5e93\u548c<code>concurrent.futures<\/code>\u5e93\u6765\u5b9e\u73b0\u7075\u6d3b\u7684\u8d85\u65f6\u63a7\u5236\u3002\u5bf9\u4e8e\u5e95\u5c42\u7f51\u7edc\u7f16\u7a0b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>socket<\/code>\u5e93\u548c<code>signal<\/code>\u5e93\u6765\u5b9e\u73b0\u8d85\u65f6\u63a7\u5236\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u8d85\u65f6\u95ee\u9898\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7a33\u5b9a\u8fd0\u884c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6eHTTP\u8bf7\u6c42\u7684\u8d85\u65f6\u65f6\u95f4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>requests<\/code>\u5e93\u8fdb\u884cHTTP\u8bf7\u6c42\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>timeout<\/code>\u53c2\u6570\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u3002\u4f8b\u5982\uff0c<code>requests.get(&#39;http:\/\/example.com&#39;, timeout=5)<\/code>\u4f1a\u57285\u79d2\u5185\u7b49\u5f85\u54cd\u5e94\u3002\u5982\u679c\u5728\u8be5\u65f6\u95f4\u5185\u672a\u6536\u5230\u54cd\u5e94\uff0c\u5c06\u5f15\u53d1<code>requests.exceptions.Timeout<\/code>\u5f02\u5e38\u3002\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u8d85\u65f6\u65f6\u95f4\uff0c\u4ee5\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n<p><strong>\u5728Python\u7684Socket\u7f16\u7a0b\u4e2d\u5982\u4f55\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\uff1f<\/strong><br \/>\u5728Socket\u7f16\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>settimeout<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2aSocket\u5bf9\u8c61\u540e\uff0c\u53ef\u4ee5\u8c03\u7528<code>sock.settimeout(5)<\/code>\uff0c\u8fd9\u6837\u57285\u79d2\u5185\u6ca1\u6709\u4efb\u4f55\u64cd\u4f5c\uff08\u5982\u53d1\u9001\u6216\u63a5\u6536\u6570\u636e\uff09\uff0c\u5c06\u4f1a\u5f15\u53d1<code>socket.timeout<\/code>\u5f02\u5e38\u3002\u8fd9\u5728\u7f51\u7edc\u8fde\u63a5\u4e0d\u7a33\u5b9a\u7684\u60c5\u51b5\u4e0b\u5c24\u5176\u6709\u7528\uff0c\u53ef\u4ee5\u907f\u514d\u7a0b\u5e8f\u65e0\u54cd\u5e94\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u7684\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\uff1f<\/strong><br \/>\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>threading.Event<\/code>\u5bf9\u8c61\u6765\u5b9e\u73b0\u8d85\u65f6\u63a7\u5236\u3002\u901a\u8fc7\u8c03\u7528<code>event.wait(timeout)<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u8d85\u65f6\u65f6\u95f4\u3002\u5982\u679c\u5728\u6307\u5b9a\u65f6\u95f4\u5185\u4e8b\u4ef6\u672a\u88ab\u8bbe\u7f6e\uff0c\u65b9\u6cd5\u4f1a\u8fd4\u56de<code>False<\/code>\uff0c\u4ece\u800c\u53ef\u4ee5\u5728\u591a\u7ebf\u7a0b\u4e2d\u907f\u514d\u65e0\u9650\u671f\u7b49\u5f85\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u5408\u9700\u8981\u540c\u65f6\u5904\u7406\u591a\u4e2a\u4efb\u52a1\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528requests\u5e93\u3001socket\u5e93\u3001threading\u5e93\u7b49\u3002 \u5176\u4e2d\u6700\u5e38 [&hellip;]","protected":false},"author":3,"featured_media":1186353,"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\/1186348"}],"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=1186348"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1186348\/revisions"}],"predecessor-version":[{"id":1186355,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1186348\/revisions\/1186355"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1186353"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1186348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1186348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1186348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}