{"id":3649,"date":"2025-05-28T15:19:18","date_gmt":"2025-06-04T07:53:17","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3649"},"modified":"2025-06-04T15:19:18","modified_gmt":"2025-06-04T07:53:17","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84timeouterror%e5%87%bd%e6%95%b8%e5%8f%8a%e5%85%b6%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84timeouterror%e5%87%bd%e6%95%b8%e5%8f%8a%e5%85%b6%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 TimeoutError\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, TimeoutError, Python\u4f8b\u5916\u8655\u7406\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 TimeoutError<\/h1>\n<p>Python \u7684 `TimeoutError` \u662f\u4e00\u500b\u5e38\u898b\u7684\u4f8b\u5916\uff0c\u7576\u7a0b\u5f0f\u57f7\u884c\u6642\u9593\u8d85\u904e\u6307\u5b9a\u7684\u6642\u9593\u6642\uff0c\u5c07\u6703\u62cb\u51fa\u6b64\u4f8b\u5916\u3002\u9019\u500b\u4f8b\u5916\u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u7a0b\u5f0f\u662f\u5426\u57f7\u884c\u592a\u4e45\uff0c\u4e26\u4e14\u5e6b\u52a9\u958b\u767c\u8005\u5075\u6e2c\u7a0b\u5f0f\u4e2d\u53ef\u80fd\u5b58\u5728\u7684\u7121\u9650\u8ff4\u5708\u554f\u984c\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u63a2\u8a0e `TimeoutError` \u7684\u6700\u65b0\u8a9e\u6cd5\u3001\u5be6\u4f5c\u7bc4\u4f8b\u3001\u932f\u8aa4\u6392\u9664\u65b9\u6cd5\u548c\u5ef6\u4f38\u61c9\u7528\u3002<\/p>\n<h2>TimeoutError \u7684\u8a9e\u6cd5<\/h2>\n<p>\u5728 Python 3.9 \u53ca\u66f4\u65b0\u7248\u672c\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 `asyncio` \u6a21\u7d44\u4f86\u8655\u7406\u7570\u6b65\u64cd\u4f5c\u4e2d\u7684 `TimeoutError`\uff0c\u9019\u662f\u66f4\u7b26\u5408\u73fe\u4ee3 Python \u7a0b\u5f0f\u8a2d\u8a08\u7684\u505a\u6cd5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 `asyncio` \u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nimport asyncio<\/p>\n<p>async def main():<br \/>\n    await asyncio.sleep(10)<\/p>\n<p>try:<br \/>\n    asyncio.run(main())<br \/>\nexcept asyncio.TimeoutError:<br \/>\n    print(&#8220;\u57f7\u884c\u6642\u9593\u8d85\u904e\u6307\u5b9a\u6642\u9593\uff0c\u62cb\u51fa TimeoutError \u4f8b\u5916&#8221;)<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u82e5 `main` \u51fd\u6578\u57f7\u884c\u8d85\u904e\u9810\u5b9a\u6642\u9593\uff0c\u5c07\u6703\u62cb\u51fa `TimeoutError`\u3002<\/p>\n<h2>TimeoutError \u7684\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684 `TimeoutError` \u7684\u7bc4\u4f8b\uff0c\u9019\u6b21\u6211\u5011\u5c07\u4f7f\u7528 `threading` \u6a21\u7d44\u4f86\u5c55\u793a\u5982\u4f55\u8655\u7406\u963b\u585e\u64cd\u4f5c\uff1a<\/p>\n<p>&#8220;`python<br \/>\nimport threading<br \/>\nimport time<\/p>\n<p>def long_running_task():<br \/>\n    time.sleep(10)<\/p>\n<p>thread = threading.Thread(target=long_running_task)<br \/>\nthread.start()<br \/>\nthread.join(timeout=5)<\/p>\n<p>if thread.is_alive():<br \/>\n    print(&#8220;\u57f7\u884c\u6642\u9593\u8d85\u904e5\u79d2\uff0c\u62cb\u51fa TimeoutError \u4f8b\u5916&#8221;)<br \/>\n    thread.join()  # \u53ef\u9078\u64c7\u7d42\u6b62\u57f7\u884c\u7dd2\u6216\u57f7\u884c\u5176\u4ed6\u8655\u7406<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u555f\u52d5\u4e86\u4e00\u500b\u57f7\u884c\u7dd2\uff0c\u4e26\u5728\u4e3b\u57f7\u884c\u7dd2\u4e2d\u7b49\u5f85\u5b83\u5b8c\u6210\u3002\u5982\u679c\u7b49\u5f85\u6642\u9593\u8d85\u904e 5 \u79d2\uff0c\u5247\u5831\u544a\u8d85\u6642\u3002<\/p>\n<h2>TimeoutError \u7684\u512a\u9ede<\/h2>\n<p>`TimeoutError` \u7684\u512a\u9ede\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u6aa2\u67e5\u7a0b\u5f0f\u57f7\u884c\u6642\u9593<\/strong>\uff1a\u53ef\u4ee5\u6709\u6548\u6aa2\u67e5\u7a0b\u5f0f\u57f7\u884c\u904e\u4e45\uff0c\u907f\u514d\u7cfb\u7d71\u8cc7\u6e90\u88ab\u7121\u9650\u4f54\u7528\u3002<\/li>\n<li><strong>\u5075\u6e2c\u7121\u9650\u8ff4\u5708<\/strong>\uff1a\u900f\u904e\u8a2d\u7f6e\u8d85\u6642\uff0c\u53ef\u4ee5\u5feb\u901f\u5b9a\u4f4d\u7a0b\u5f0f\u4e2d\u7684\u7121\u9650\u8ff4\u5708\u554f\u984c\uff0c\u4fdd\u969c\u7cfb\u7d71\u7a69\u5b9a\u3002<\/li>\n<\/ul>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u7576\u4f7f\u7528 `TimeoutError` \u6642\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u554f\u984c\uff1a<\/p>\n<p>1. **\u672a\u6b63\u78ba\u8a2d\u7f6e\u8d85\u6642**\uff1a\u78ba\u4fdd\u5728\u9069\u7576\u7684\u5730\u65b9\u8a2d\u7f6e\u8d85\u6642\uff0c\u5426\u5247\u53ef\u80fd\u7121\u6cd5\u6355\u6349\u5230\u4f8b\u5916\u3002<br \/>\n2. **\u7570\u6b65\u7a0b\u5f0f\u4e2d\u7684\u8d85\u6642**\uff1a\u5728\u4f7f\u7528 `asyncio` \u6642\uff0c\u8acb\u78ba\u8a8d\u4e8b\u4ef6\u5faa\u74b0\u6b63\u78ba\u904b\u884c\uff0c\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8d85\u6642\u932f\u8aa4\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u6aa2\u67e5\u7a0b\u5f0f\u57f7\u884c\u6642\u9593\uff0c`TimeoutError` \u9084\u53ef\u7528\u65bc\uff1a<\/p>\n<p>&#8211; \u7db2\u7d61\u8acb\u6c42\u7684\u8d85\u6642\u8655\u7406\uff08\u5982\u4f7f\u7528 `requests` \u6a21\u7d44\uff09\u3002<br \/>\n&#8211; \u7528\u65bc\u591a\u7dda\u7a0b\u6216\u591a\u9032\u7a0b\u7a0b\u5e8f\u4e2d\uff0c\u4ee5\u78ba\u4fdd\u7cfb\u7d71\u8cc7\u6e90\u4e0d\u88ab\u9577\u6642\u9593\u4f54\u7528\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u4f8b\u5916\u8655\u7406\uff0c\u8acb\u53c3\u8003\u9019\u7bc7[Python \u7570\u5e38\u8655\u7406\u6559\u5b78](https:\/\/vocus.cc\/article\/648b9fb9fd8978b5d1f2d5b9)\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>`TimeoutError` \u662f\u4e00\u500b\u91cd\u8981\u7684\u4f8b\u5916\u8655\u7406\u5de5\u5177\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u6aa2\u67e5\u7a0b\u5f0f\u662f\u5426\u57f7\u884c\u904e\u4e45\uff0c\u4e26\u5b9a\u4f4d\u7121\u9650\u8ff4\u5708\u554f\u984c\u3002\u901a\u904e\u7c21\u55ae\u7684 `try` \u548c `except` \u5340\u584a\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07\u5176\u61c9\u7528\u5230\u60a8\u7684\u7a0b\u5f0f\u4e2d\uff0c\u63d0\u5347\u7a0b\u5f0f\u7684\u5065\u58ef\u6027\u3002<\/p>\n<p>&#8212;<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: \u5982\u4f55\u5728\u975e\u7570\u6b65\u7a0b\u5f0f\u4e2d\u4f7f\u7528 TimeoutError\uff1f**<br \/>\nA1: \u60a8\u53ef\u4ee5\u4f7f\u7528 `threading` \u6216 `multiprocessing` \u6a21\u7d44\u4f86\u8a2d\u7f6e\u8d85\u6642\uff0c\u6355\u6349\u57f7\u884c\u6642\u9593\u904e\u9577\u7684\u4efb\u52d9\u3002<\/p>\n<p>**Q2: TimeoutError \u662f\u5426\u6703\u5f71\u97ff\u7a0b\u5f0f\u7684\u6027\u80fd\uff1f**<br \/>\nA2: \u4f7f\u7528 `TimeoutError` \u53ef\u4ee5\u907f\u514d\u7a0b\u5f0f\u9577\u6642\u9593\u57f7\u884c\u9020\u6210\u7684\u6027\u80fd\u554f\u984c\uff0c\u5be6\u969b\u4e0a\u662f\u6709\u52a9\u65bc\u63d0\u9ad8\u7a0b\u5f0f\u7684\u6574\u9ad4\u7a69\u5b9a\u6027\u3002<\/p>\n<p>**Q3: \u5982\u4f55\u8a2d\u7f6e\u81ea\u5b9a\u7fa9\u7684\u8d85\u6642\u9650\u5236\uff1f**<br \/>\nA3: \u5728\u4f7f\u7528 `asyncio` \u6642\uff0c\u53ef\u4ee5\u900f\u904e `asyncio.wait_for()` \u51fd\u6578\u8a2d\u7f6e\u81ea\u5b9a\u7fa9\u7684\u8d85\u6642\u9650\u5236\uff0c\u9019\u6a23\u53ef\u4ee5\u9748\u6d3b\u63a7\u5236\u6bcf\u500b\u4efb\u52d9\u7684\u57f7\u884c\u6642\u9593\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2d\u7684TimeoutError\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u5e6b\u52a9\u60a8\u66f4\u6709\u6548\u5730\u63a7\u5236\u7a0b\u5e8f\u7684\u57f7\u884c\u6642\u9593\uff0c\u4e26\u63d0\u4f9b\u66f4\u597d\u7684\u7a0b\u5e8f\u57f7\u884c\u6548\u7387\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39TimeoutError\u51fd\u6578\u7684\u529f\u80fd\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u63d0\u9ad8\u7a0b\u5e8f\u7684\u57f7\u884c\u6548\u7387\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3649","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-WR","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3649"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3649\/revisions"}],"predecessor-version":[{"id":12767,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3649\/revisions\/12767"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}