{"id":205422,"date":"2025-05-29T15:24:03","date_gmt":"2025-05-29T07:24:03","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205422\/"},"modified":"2025-05-29T15:24:03","modified_gmt":"2025-05-29T07:24:03","slug":"asyncio%e4%b8%adawait%e6%97%a0%e6%95%88%ef%bc%9f%e5%a6%82%e4%bd%95%e6%ad%a3%e7%a1%ae%e5%ae%9e%e7%8e%b0%e5%bc%82%e6%ad%a5%e4%bb%bb%e5%8a%a1%e5%b9%b6%e5%8f%91%e6%89%a7%e8%a1%8c%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205422\/","title":{"rendered":"Asyncio\u4e2dawait\u65e0\u6548\uff1f\u5982\u4f55\u6b63\u786e\u5b9e\u73b0\u5f02\u6b65\u4efb\u52a1\u5e76\u53d1\u6267\u884c\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Asyncio\u4e2dawait\u65e0\u6548\uff1f\u5982\u4f55\u6b63\u786e\u5b9e\u73b0\u5f02\u6b65\u4efb\u52a1\u5e76\u53d1\u6267\u884c\uff1f<\/h1>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241201\/1733042569674c2189ac53b.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5f02\u6b65\u7f16\u7a0b\u4e2d\u7684 asyncio \u7684\u56f0\u60d1<\/strong><\/p>\n<p>\u6b63\u5982\u4f60\u6240\u89c2\u5bdf\u5230\u7684\uff0c\u5728\u4f60\u7684\u4ee3\u7801\u4e2d\uff0cawait \u548c update_product_loop \u5e76\u4e0d\u662f\u540c\u6b65\u6267\u884c\u7684\uff0c\u5373\u4f7f\u4f7f\u7528\u4e86 await\u3002\u8fd9\u662f\u56e0\u4e3a update_product_loop \u5e76\u672a\u4f7f\u7528 async \u58f0\u660e\uff0c\u56e0\u6b64\u5b83\u53ea\u662f\u4e00\u4e2a\u5e38\u89c4\u7684\u540c\u6b65\u51fd\u6570\u3002<\/p>\n<p>await \u548c async \u7684\u4f5c\u7528\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>async \u8868\u793a\u4e00\u4e2a\u51fd\u6570\u662f\u5f02\u6b65\u7684\uff0c\u610f\u5473\u7740\u5b83\u53ef\u4ee5\u5728\u4e0d\u963b\u585e\u5f53\u524d\u7ebf\u7a0b\u7684\u60c5\u51b5\u4e0b\u6267\u884c\u3002<\/li>\n<li>await \u5141\u8bb8\u5728\u5f02\u6b65\u51fd\u6570\u5185\u6682\u505c\u6267\u884c\uff0c\u76f4\u5230\u6307\u5b9a\u7684\u4efb\u52a1\u5b8c\u6210\u3002<\/li>\n<\/ul>\n<p>\u4e3a\u4e86\u8ba9 update_product_loop \u771f\u6b63\u6210\u4e3a\u5f02\u6b65\u7684\uff0c\u4f60\u9700\u8981\u4f7f\u7528 async \u4fee\u9970\u5b83\uff0c\u5e76\u4f7f\u7528 asyncio.gather \u5c06\u5b83\u4e0e\u5176\u4ed6\u5f02\u6b65\u4efb\u52a1\u7ec4\u5408\u8d77\u6765\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4fee\u6539\u4f60\u7684\u4ee3\u7801\uff1a<\/p>\n<pre># Modifying the main function to use asyncio.gather instead of TaskGroup\nasync def main_modified():\n    # Initialize products for each page\n    results = []\n    for page in JDServer.api(\"api\/product\/getPageNum\"):\n        if True:  # Mocking products_insert_on as always True for testing\n            result = await recursion_products_init(page[\"page_num\"])\n            results.append(result)\n    \n    # Ensure all recursion_products_init are done before executing update_product_loop\n    if True:  # Mocking products_insert_on as always True for testing\n        results.append(update_product_loop())\n\n    # Synchronize categories for each page\n    if True:  # Mocking category_insert_on as always True for testing\n        for page in JDServer.api(\"api\/product\/getPageNum\"):\n            result = await recursion_sync_category(page[\"page_num\"])\n            results.append(result)\n\n    # Using asyncio.gather to run tasks concurrently\n    result1, result2 = await asyncio.gather(update_product_category(), update_products_price())\n    results.extend([result1, result2])\n\n    return results\n\n# Using the auxiliary function to execute the modified main coroutine\nresults_modified = await auxiliary_runner(main_modified())\n\n# Printing the results\nprint(results_modified)<\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u4fee\u6539\uff0cupdate_product_loop \u5c06\u6210\u4e3a\u5f02\u6b65\u7684\uff0c\u5e76\u4e14\u53ea\u6709\u5728\u6240\u6709\u521d\u59cb\u5316\u4ea7\u54c1\u548c\u540c\u6b65\u7c7b\u522b\u7684\u4efb\u52a1\u5b8c\u6210\u540e\u624d\u4f1a\u6267\u884c\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aAsyncio\u4e2dawait\u65e0\u6548\uff1f\u5982\u4f55\u6b63\u786e\u5b9e\u73b0\u5f02\u6b65\u4efb\u52a1\u5e76\u53d1\u6267\u884c\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Asyncio\u4e2dawait\u65e0\u6548\uff1f&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205422","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205422","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205422"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205422\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}