{"id":55,"date":"2026-05-13T00:55:28","date_gmt":"2026-05-12T16:55:28","guid":{"rendered":"http:\/\/w.renho.local\/?p=55"},"modified":"2026-05-13T01:37:15","modified_gmt":"2026-05-12T17:37:15","slug":"async-for","status":"publish","type":"post","link":"https:\/\/www.devfm.cn\/?p=55","title":{"rendered":"async for"},"content":{"rendered":"<pre><code class=\"language-python line-numbers\">import asyncio\nimport time\n\nprint(\"\\n\\n=== 2. \u5f02\u6b65\u6a21\u5f0f\u5f00\u59cb (\u8bf7\u6ce8\u610f\u89c2\u5bdf\u5fc3\u8df3) ===\")\n\n# \u8fd9\u662f\u4e00\u4e2a\u80cc\u666f\u4efb\u52a1\uff0c\u6a21\u62df UI \u52a8\u753b\u6216\u8005\u5fc3\u8df3\u68c0\u6d4b\nasync def background_heartbeat():\n    while True:\n        print(\"   \ud83d\udc93 (\u80cc\u666f\u4efb\u52a1) \u549a-\u549a-\u549a... \u7a0b\u5e8f\u8fd8\u6d3b\u7740\uff01\")\n        await asyncio.sleep(0.3) # \u6bcf0.3\u79d2\u8df3\u52a8\u4e00\u6b21\n\n# \u5f02\u6b65\u751f\u6210\u5668\nasync def async_stream():\n    for i in range(3):\n        print(f\"\ud83d\udd04 (\u751f\u6210\u5668\u5185\u90e8) \u6b63\u5728\u7528\u529b\u4e0b\u8f7d\u7b2c {i+1} \u4e2a\u5305...\")\n        # \u3010\u6302\u8d77\u3011\u544a\u8bc9 Python\uff1a\u6211\u8981\u7b491\u79d2\uff0c\u8fd9\u671f\u95f4\u4f60\u53bb\u5904\u7406\u90a3\u4e2a\u201c\u5fc3\u8df3\u201d\u4efb\u52a1\u5427\n        await asyncio.sleep(1) \n        yield f\"\ud83d\udce6 \u5305 {i+1} \u4e0b\u8f7d\u5b8c\u6210\"\n\nasync def main():\n    # 1. \u542f\u52a8\u80cc\u666f\u5fc3\u8df3\u4efb\u52a1\n    task = asyncio.create_task(background_heartbeat())\n\n    start_time = time.time()\n\n    # 2. \u5f00\u59cb Async For \u5faa\u73af\n    # \u5173\u952e\u70b9\uff1a\u6bcf\u6b21\u5728\u8fd9\u91cc\u201c\u7b49\u201d\u6570\u636e\u7684\u65f6\u5019\uff0c\u4e0a\u9762\u7684 background_heartbeat \u5c31\u4f1a\u63d2\u961f\u8fd0\u884c\n    async for data in async_stream():\n        print(f\"\u2705 \u4e3b\u7a0b\u5e8f\u6536\u5230: {data}\\n\")\n\n    # \u505c\u6b62\u5fc3\u8df3\u4efb\u52a1\n    task.cancel()\n    print(f\"=== \u5f02\u6b65\u6a21\u5f0f\u7ed3\u675f\uff0c\u603b\u8017\u65f6: {time.time() - start_time:.2f}\u79d2 ===\")\n\n# \u8fd0\u884c\nasyncio.run(main())\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import asyncio import time print(&#8220;\\n\\n=== 2. \u5f02\u6b65\u6a21\u5f0f\u5f00\u59cb (\u8bf7\u6ce8&hellip; <a class=\"read-more-link\" href=\"https:\/\/www.devfm.cn\/?p=55\">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-55","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.devfm.cn\/index.php?rest_route=\/wp\/v2\/posts\/55","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=55"}],"version-history":[{"count":2,"href":"https:\/\/www.devfm.cn\/index.php?rest_route=\/wp\/v2\/posts\/55\/revisions"}],"predecessor-version":[{"id":64,"href":"https:\/\/www.devfm.cn\/index.php?rest_route=\/wp\/v2\/posts\/55\/revisions\/64"}],"wp:attachment":[{"href":"https:\/\/www.devfm.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=55"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devfm.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=55"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devfm.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}