{"id":59,"date":"2026-05-13T01:34:29","date_gmt":"2026-05-12T17:34:29","guid":{"rendered":"http:\/\/w.renho.local\/?p=59"},"modified":"2026-05-13T01:37:49","modified_gmt":"2026-05-12T17:37:49","slug":"async-io","status":"publish","type":"post","link":"https:\/\/www.devfm.cn\/?p=59","title":{"rendered":"async io"},"content":{"rendered":"<pre><code class=\"language-python line-numbers\">import asyncio\nimport threading\n\n\n# \u4f20\u5165name\u53c2\u6570:\nasync def hello(name):\n    # \u6253\u5370name\u548c\u5f53\u524d\u7ebf\u7a0b:\n    print(\"Hello %s! (%s)\" % (name, threading.current_thread))\n    # \u5f02\u6b65\u8c03\u7528asyncio.sleep(1):\n    await asyncio.sleep(10)\n    print(\"Hello %s again! (%s)\" % (name, threading.current_thread))\n    return name\n\n# syncio.ensure_future\u662fPython asyncio\u5e93\u4e2d\u7528\u4e8e\u5b89\u6392\u534f\u7a0b\u6216Future\u5bf9\u8c61\u6267\u884c\u7684\u51fd\u6570\uff0c\u5176\u6838\u5fc3\u4f5c\u7528\u662f\u5c06\u53ef\u7b49\u5f85\u5bf9\u8c61\uff08\u5982\u534f\u7a0b\u6216Future\uff09\u52a0\u5165\u4efb\u52a1\u961f\u5217\u5e76\u786e\u4fdd\u5176\u88ab\u8c03\u5ea6\u6267\u884c\uff0c\u5373\u4f7f\u672a\u88ab\u663e\u5f0fawait\n\nasync def main():\n    L = await asyncio.gather(hello(\"Bob\"), hello(\"Alice\"))\n    #L = await asyncio.wait(hello(\"Bob\"), hello(\"Alice\"))\n    print(L)\n\nasyncio.run(main())\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import asyncio import threading # \u4f20\u5165name\u53c2\u6570: async def h&hellip; <a class=\"read-more-link\" href=\"https:\/\/www.devfm.cn\/?p=59\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-59","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.devfm.cn\/index.php?rest_route=\/wp\/v2\/posts\/59","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devfm.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devfm.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devfm.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devfm.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=59"}],"version-history":[{"count":2,"href":"https:\/\/www.devfm.cn\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions"}],"predecessor-version":[{"id":65,"href":"https:\/\/www.devfm.cn\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions\/65"}],"wp:attachment":[{"href":"https:\/\/www.devfm.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devfm.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devfm.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}