{"id":204815,"date":"2025-05-29T08:01:24","date_gmt":"2025-05-29T00:01:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204815\/"},"modified":"2025-05-29T08:01:24","modified_gmt":"2025-05-29T00:01:24","slug":"python%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b%e4%b8%adawait%e5%85%b3%e9%94%ae%e5%ad%97%e5%a6%82%e4%bd%95%e7%a1%ae%e4%bf%9d%e5%a4%9a%e4%b8%aa%e5%8d%8f%e7%a8%8b%e5%ae%8c%e6%88%90%e5%90%8e%e5%86%8d%e6%89%a7","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204815\/","title":{"rendered":"Python\u5f02\u6b65\u7f16\u7a0b\u4e2dawait\u5173\u952e\u5b57\u5982\u4f55\u786e\u4fdd\u591a\u4e2a\u534f\u7a0b\u5b8c\u6210\u540e\u518d\u6267\u884c\u540e\u7eed\u64cd\u4f5c\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python\u5f02\u6b65\u7f16\u7a0b\u4e2dawait\u5173\u952e\u5b57\u5982\u4f55\u786e\u4fdd\u591a\u4e2a\u534f\u7a0b\u5b8c\u6210\u540e\u518d\u6267\u884c\u540e\u7eed\u64cd\u4f5c\uff1f<\/h1>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aPython\u5f02\u6b65\u7f16\u7a0b\u4e2dawait\u5173\u952e\u5b57\u5982\u4f55\u786e\u4fdd\u591a\u4e2a\u534f\u7a0b\u5b8c\u6210\u540e\u518d\u6267\u884c\u540e\u7eed\u64cd\u4f5c\uff1f\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241117\/1731805433673940f97b567.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python \u5f02\u6b65\u7f16\u7a0b\u4e2d\u7684 await \u548c async<\/strong><\/p>\n<p>\u5f02\u6b65\u7f16\u7a0b\u5728 python \u4e2d\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5b83\u5141\u8bb8\u5e94\u7528\u7a0b\u5e8f\u5904\u7406\u8017\u65f6\u4efb\u52a1\uff0c\u800c\u4e0d\u4f1a\u963b\u585e\u4e3b\u8fdb\u7a0b\u3002\u5728\u60a8\u7684\u95ee\u9898\u4e2d\uff0c\u60a8\u63d0\u5230 await \u4f3c\u4e4e\u6ca1\u6709\u4f7f update_product_loop \u51fd\u6570\u5f02\u6b65\u3002<\/p>\n<p><strong>await \u548c async<\/strong><\/p>\n<ul>\n<li>async \u5173\u952e\u5b57\u8868\u793a\u4e00\u4e2a\u534f\u7a0b\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u88ab\u6682\u505c\u548c\u6062\u590d\u3002<\/li>\n<li>await \u5173\u952e\u5b57\u7528\u6765\u6682\u505c\u534f\u7a0b\u5e76\u7b49\u5f85\u53e6\u4e00\u4e2a\u534f\u7a0b\u5b8c\u6210\u3002<\/li>\n<\/ul>\n<p><strong>\u60a8\u7684\u95ee\u9898<\/strong><\/p>\n<p>\u5728\u60a8\u63d0\u4f9b\u7684\u4ee3\u7801\u4e2d\uff0cawait \u5b9e\u9645\u4e0a\u88ab\u6b63\u786e\u4f7f\u7528\u4e86\u3002\u5b83\u5728\u8c03\u7528 recursion_products_init \u51fd\u6570\u540e\u4f7f\u7528\u4e86\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u5c06\u7b49\u5f85\u8be5\u51fd\u6570\u5b8c\u6210\u518d\u7ee7\u7eed\u6267\u884c\u3002\u7136\u800c\uff0c\u60a8\u4f3c\u4e4e\u5e0c\u671b\u5728\u5b8c\u6210 recursion_products_init \u51fd\u6570\u7684\u6240\u6709\u8c03\u7528\u540e\u624d\u6267\u884c update_product_loop\u3002<\/p>\n<p><strong>\u6539\u8fdb\u540e\u7684\u4ee3\u7801<\/strong><\/p>\n<p>\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 asyncio.gather \u51fd\u6570\uff0c\u5b83\u5141\u8bb8\u5e76\u53d1\u8fd0\u884c\u591a\u4e2a\u534f\u7a0b\u5e76\u7b49\u5f85\u5b83\u4eec\u5168\u90e8\u5b8c\u6210\u3002\u4ee5\u4e0b\u662f\u4fee\u6539\u540e\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>async def main():\n    results = []\n\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    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<\/pre>\n<p><strong>\u6d4b\u8bd5<\/strong><\/p>\n<p>\u8fd0\u884c\u4fee\u6539\u540e\u7684\u4ee3\u7801\u5c06\u4ea7\u751f\u4ee5\u4e0b\u7ed3\u679c\uff1a<\/p>\n<pre>['Initialized products for page 1',\n 'Initialized products for page 2',\n 'Initialized products for page 3',\n 'Updated product loop',\n 'Synchronized category for page 1',\n 'Synchronized category for page 2',\n 'Synchronized category for page 3',\n 'Updated product category',\n 'Updated products price']<\/pre>\n<p>\u6b63\u5982\u60a8\u6240\u770b\u5230\u7684\uff0cupdate_product_loop \u51fd\u6570\u5728\u5b8c\u6210\u6240\u6709 recursion_products_init \u8c03\u7528\u540e\u624d\u6267\u884c\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aPython\u5f02\u6b65\u7f16\u7a0b\u4e2dawait\u5173\u952e\u5b57\u5982\u4f55\u786e\u4fdd\u591a\u4e2a\u534f\u7a0b\u5b8c\u6210\u540e\u518d\u6267\u884c\u540e\u7eed\u64cd\u4f5c\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u5f02\u6b65\u7f16\u7a0b\u4e2dawait&#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-204815","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204815","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=204815"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204815\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}