{"id":742,"date":"2026-03-23T13:33:00","date_gmt":"2026-03-23T05:33:00","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=742"},"modified":"2026-07-21T13:40:13","modified_gmt":"2026-07-21T05:40:13","slug":"python%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b%e4%ba%8c","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/03\/23\/python%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b%e4%ba%8c\/","title":{"rendered":"python\u5f02\u6b65\u7f16\u7a0b\u4e8c"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1. \u4e0d\u8981\u5728\u534f\u7a0b\u4e2d\u8c03\u7528\u540c\u6b65\u963b\u585e\u51fd\u6570<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import time<br>\u200b<br>\u200b<br>async def bad():<br> &nbsp; &nbsp;time.sleep(1) &nbsp;# \u9519\u8bef\uff01\u5c06\u963b\u585e\u6574\u4e2a\u4e8b\u4ef6\u5faa\u73af<br>\u200b<br>\u200b<br>async def good():<br> &nbsp; &nbsp;await asyncio.sleep(1) &nbsp;# \u6b63\u786e\uff01\u4e3b\u52a8\u8ba9\u51fa\u63a7\u5236\u6743<br>\u200b<br>\u200b<br>async def acceptable():<br> &nbsp; &nbsp;await asyncio.to_thread(time.sleep, 1) &nbsp;# \u53ef\u884c\uff01\u8ba9\u7ebf\u7a0b\u6c60\u53bb\u963b\u585e<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u59cb\u7ec8\u4f7f\u7528 asyncio.run \u4f5c\u4e3a\u5165\u53e3<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># \u9519\u8bef\uff1a\u624b\u52a8\u7ba1\u7406\u4e8b\u4ef6\u5faa\u73af<br>loop = asyncio.new_event_loop()<br>asyncio.set_event_loop(loop)<br>task = loop.create_task(main())<br>loop.run_forever()<br>\u200b<br># \u6b63\u786e\uff1aasyncio.run \u81ea\u52a8\u521b\u5efa\u548c\u5173\u95ed\u4e8b\u4ef6\u5faa\u73af<br>asyncio.run(main())<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u5c0f\u5fc3\u534f\u7a0b\u5bf9\u8c61\u672a\u88ab await<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>async def main():<br> &nbsp; &nbsp;# \u9519\u8bef\uff1a\u521b\u5efa\u4e86\u534f\u7a0b\u4f46\u672a await\uff0c\u534f\u7a0b\u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c<br> &nbsp; &nbsp;fetch(\"url\", 1)<br>\u200b<br> &nbsp; &nbsp;# \u6b63\u786e<br> &nbsp; &nbsp;await fetch(\"url\", 1)<br>\u200b<br> &nbsp; &nbsp;# \u6216\u901a\u8fc7 gather<br> &nbsp; &nbsp;await asyncio.gather(fetch(\"url1\", 1), fetch(\"url2\", 2))<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. gather \u5f02\u5e38\u5904\u7406<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">gather \u9ed8\u8ba4\u4efb\u4f55\u534f\u7a0b\u5f02\u5e38\u90fd\u4f1a\u7acb\u5373\u4f20\u64ad\uff0c\u5176\u4ed6\u534f\u7a0b\u4e0d\u4f1a\u53d6\u6d88\uff0c\u4f46\u7ed3\u679c\u4e22\u5931<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>async def main():<br> &nbsp; &nbsp;# \u65b9\u5f0f\u4e00\uff1a\u4f7f\u7528 return_exceptions=True<br> &nbsp; &nbsp;results = await asyncio.gather(<br> &nbsp; &nbsp; &nbsp; &nbsp;risky_task(),<br> &nbsp; &nbsp; &nbsp; &nbsp;safe_task(),<br> &nbsp; &nbsp; &nbsp; &nbsp;return_exceptions=True,<br> &nbsp;  )<br> &nbsp; &nbsp;for r in results:<br> &nbsp; &nbsp; &nbsp; &nbsp;if isinstance(r, Exception):<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print(f\"\u67d0\u4e2a\u4efb\u52a1\u5931\u8d25: {r}\")<br>\u200b<br> &nbsp; &nbsp;# \u65b9\u5f0f\u4e8c\uff1a\u4f7f\u7528 TaskGroup\uff08Python 3.11+\uff09<br> &nbsp; &nbsp;# \u4efb\u4e00\u5f02\u5e38\u4f1a\u53d6\u6d88\u7ec4\u5185\u6240\u6709\u4efb\u52a1<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u4f7f\u7528 debug \u6a21\u5f0f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">asyncio \u7684 debug \u6a21\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u4f60\u53d1\u73b0\u5f02\u6b65\u4ee3\u7801\u4e2d\u7684\u5e38\u89c1\u95ee\u9898\uff0c\u6bd4\u5982\u534f\u7a0b\u963b\u585e\u4e8b\u4ef6\u5faa\u73af\u3001\u5fd8\u8bb0 await\u3001\u56de\u8c03\u6267\u884c\u65f6\u95f4\u8fc7\u957f\u7b49\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5f00\u542f asyncio \u8c03\u8bd5\u6a21\u5f0f<br>asyncio.run(main(), debug=True)<br>\u200b<br># \u6216\u901a\u8fc7\u73af\u5883\u53d8\u91cf<br># PYTHONASYNCIODEBUG=1 python script.py<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u68c0\u6d4b\u957f\u65f6\u95f4\u963b\u585e\u7684\u534f\u7a0b<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">debug \u6a21\u5f0f\u4e0b\uff0c\u4e8b\u4ef6\u5faa\u73af\u4f1a\u76d1\u63a7\u6bcf\u4e2a\u534f\u7a0b\u7684\u6267\u884c\u65f6\u95f4\u3002\u5982\u679c\u67d0\u4e2a\u534f\u7a0b\u6267\u884c\u8d85\u8fc7 0.1 \u79d2\uff08\u9ed8\u8ba4\u9608\u503c\uff09\uff0c\u4f1a\u5728 stderr \u8f93\u51fa\u8b66\u544a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import time<br>import asyncio<br>\u200b<br>async def blocking_coroutine():<br> &nbsp; &nbsp;\"\"\"\u6a21\u62df\u4e00\u4e2a\u534f\u7a0b\u5185\u90e8\u505a\u4e86\u540c\u6b65\u963b\u585e\u64cd\u4f5c\"\"\"<br> &nbsp; &nbsp;print(\"\u5f00\u59cb\u963b\u585e\u64cd\u4f5c...\")<br> &nbsp; &nbsp;time.sleep(0.2) &nbsp;# \u540c\u6b65\u963b\u585e\uff0c\u4f1a\u963b\u585e\u6574\u4e2a\u4e8b\u4ef6\u5faa\u73af<br> &nbsp; &nbsp;print(\"\u963b\u585e\u64cd\u4f5c\u7ed3\u675f\")<br>\u200b<br>\u200b<br>async def main():<br> &nbsp; &nbsp;await blocking_coroutine()<br>\u200b<br>\u200b<br>asyncio.run(main(), debug=True)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8f93\u51fa\u7c7b\u4f3c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5f00\u59cb\u963b\u585e\u64cd\u4f5c...<br>\u963b\u585e\u64cd\u4f5c\u7ed3\u675f<br>Executor &lt;TaskInfo name='Task-1' ...&gt; running at (...)<br> &nbsp;  blocking_coroutine at demo.py:12<br> &nbsp;  main at demo.py:18<br> &nbsp;  ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>time.sleep(0.2)<\/code> \u662f\u540c\u6b65\u963b\u585e\uff0c\u4f46 debug \u6a21\u5f0f\u4e0b\u68c0\u6d4b\u5230\u534f\u7a0b\u5728\u540c\u4e00\u4e2a\u4f4d\u7f6e\u505c\u7559\u8d85\u8fc7 0.1 \u79d2\uff0c\u4f1a\u6253\u5370\u51fa\u6267\u884c\u6808\u4fe1\u606f\uff0c\u7cbe\u786e\u5b9a\u4f4d\u963b\u585e\u7684\u4ee3\u7801\u884c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u68c0\u6d4b\u672a await \u7684\u534f\u7a0b\u5bf9\u8c61<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5fd8\u8bb0 <code>await<\/code> \u534f\u7a0b\u662f\u65b0\u624b\u6700\u5bb9\u6613\u72af\u7684\u9519\u8bef\uff0cdebug \u6a21\u5f0f\u4f1a\u68c0\u6d4b\u5230\u534f\u7a0b\u5bf9\u8c61\u88ab\u521b\u5efa\u4f46\u4ece\u672a\u88ab\u8fed\u4ee3\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import asyncio<br>\u200b<br>\u200b<br>async def fetch_data(url: str) -&gt; str:<br> &nbsp; &nbsp;await asyncio.sleep(0.5)<br> &nbsp; &nbsp;return f\"{url} \u6570\u636e\"<br>\u200b<br>\u200b<br>async def main():<br> &nbsp; &nbsp;# \u5fd8\u8bb0 await\uff0c\u534f\u7a0b\u5bf9\u8c61\u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c<br> &nbsp; &nbsp;fetch_data(\"https:\/\/example.com\")<br>\u200b<br> &nbsp; &nbsp;await asyncio.sleep(1)<br>\u200b<br>\u200b<br>asyncio.run(main(), debug=True)<br>\u200b<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8f93\u51fa\u7c7b\u4f3c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Coroutine 'fetch_data' was never awaited (at demo.py:12)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u8b66\u544a\u5728\u4f60\u5fd8\u8bb0 <code>await<\/code> \u65f6\u975e\u5e38\u6709\u7528\uff0c\u907f\u514d\u534f\u7a0b&#8221;\u9759\u9ed8\u4e22\u5931&#8221;\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u81ea\u5b9a\u4e49\u6162\u64cd\u4f5c\u9608\u503c<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7 <code>loop.slow_callback_duration<\/code> \u8c03\u6574\u68c0\u6d4b\u9608\u503c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>async def main():<br> &nbsp; &nbsp;loop = asyncio.get_running_loop()<br> &nbsp; &nbsp;loop.slow_callback_duration = 0.5 &nbsp;# \u6539\u4e3a 0.5 \u79d2\u624d\u62a5\u8b66<br>\u200b<br> &nbsp; &nbsp;def acceptable_callback():<br> &nbsp; &nbsp; &nbsp; &nbsp;time.sleep(0.3) &nbsp;# 0.3 \u79d2\uff0c\u4f4e\u4e8e\u81ea\u5b9a\u4e49\u9608\u503c\uff0c\u4e0d\u4f1a\u62a5\u8b66<br>\u200b<br> &nbsp; &nbsp;loop.call_later(0.1, acceptable_callback)<br> &nbsp; &nbsp;await asyncio.sleep(0.5)<br>\u200b<br>\u200b<br>asyncio.run(main(), debug=True)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. \u907f\u514d\u5168\u5c40\u4e8b\u4ef6\u5faa\u73af<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># \u9519\u8bef\uff1a\u5728\u6a21\u5757\u7ea7\u522b\u83b7\u53d6\u4e8b\u4ef6\u5faa\u73af<br>loop = asyncio.get_event_loop() &nbsp;# \u53ef\u80fd\u83b7\u53d6\u5230\u9519\u8bef\u7684\u4e8b\u4ef6\u5faa\u73af<br>\u200b<br># \u6b63\u786e\uff1a\u5728\u534f\u7a0b\u5185\u90e8\u83b7\u53d6\u5f53\u524d\u4e8b\u4ef6\u5faa\u73af<br>async def my_func():<br> &nbsp; &nbsp;loop = asyncio.get_running_loop()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7. CancelledError \u7684\u6b63\u786e\u5904\u7406<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>async def cleanup():<br> &nbsp; &nbsp;try:<br> &nbsp; &nbsp; &nbsp; &nbsp;await long_running_task()<br> &nbsp; &nbsp;except asyncio.CancelledError:<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u52a1\u5fc5\u5b8c\u6210\u6e05\u7406\u540e\u518d\u91cd\u65b0\u629b\u51fa<br> &nbsp; &nbsp; &nbsp; &nbsp;await release_resources()<br> &nbsp; &nbsp; &nbsp; &nbsp;raise &nbsp;# \u5fc5\u987b\u91cd\u65b0\u629b\u51fa<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1. \u4e0d\u8981\u5728\u534f\u7a0b\u4e2d\u8c03\u7528\u540c\u6b65\u963b\u585e\u51fd\u6570 2. \u59cb\u7ec8\u4f7f\u7528..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52,14],"tags":[],"class_list":["post-742","post","type-post","status-publish","format-standard","hentry","category-python","category-back"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/python\/\" rel=\"category tag\">Python<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/\" rel=\"category tag\">\u540e\u7aef<\/a>","tag_info":"\u540e\u7aef","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/742","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=742"}],"version-history":[{"count":1,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/742\/revisions"}],"predecessor-version":[{"id":743,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/742\/revisions\/743"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}