{"id":205059,"date":"2025-05-29T08:57:08","date_gmt":"2025-05-29T00:57:08","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205059\/"},"modified":"2025-05-29T08:57:08","modified_gmt":"2025-05-29T00:57:08","slug":"asyncio-queue-%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e8%b6%85%e6%97%b6%e5%a4%84%e7%90%86%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205059\/","title":{"rendered":"asyncio.Queue() \u5982\u4f55\u5b9e\u73b0\u8d85\u65f6\u5904\u7406\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>asyncio.Queue() \u5982\u4f55\u5b9e\u73b0\u8d85\u65f6\u5904\u7406\uff1f<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9\u6587\u7ae0\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300aasyncio.Queue() \u5982\u4f55\u5b9e\u73b0\u8d85\u65f6\u5904\u7406\uff1f\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241122\/1732261715674037531591d.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>asyncio.queue() \u4e2d\u7684\u8d85\u65f6\u5904\u7406<\/strong><\/p>\n<p>\u5f02\u6b65\u7f16\u7a0b\u4e2d\uff0c\u5f53\u4efb\u52a1\u9700\u8981\u7b49\u5f85\u5176\u4ed6\u4efb\u52a1\u6267\u884c\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u4f7f\u7528\u8d85\u65f6\u673a\u5236\u3002\u5728 asyncio \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 asyncio.queue() \u6765\u5b9e\u73b0\u961f\u5217\u901a\u4fe1\uff0c\u4f46\u5bf9\u4e8e\u5b9e\u65f6\u6027\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\uff0c\u9700\u8981\u5408\u7406\u4f7f\u7528\u8d85\u65f6\u673a\u5236\u3002<\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/p>\n<p>\u6211\u4eec\u4ee5\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u6765\u8bf4\u660e\u8d85\u65f6\u95ee\u9898\uff1a<\/p>\n<pre>import asyncio\n\nasync def producer(queue):\n    # \u5728\u961f\u5217\u4e2d\u653e\u5165\u6570\u636e\n    await queue.put(1)\n\nasync def consumer(queue):\n    # \u4ece\u961f\u5217\u4e2d\u53d6\u51fa\u6570\u636e\n    try:\n        item = await queue.get(timeout=0.1)  # \u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u4e3a 0.1 \u79d2\n        print(f\"\u53d6\u51fa\u6570\u636e\uff1a{item}\")\n    except asyncio.TimeoutError:\n        print(\"\u7b49\u5f85\u8d85\u65f6\")\n\nasync def main():\n    queue = asyncio.Queue()\n    producer_task = asyncio.create_task(producer(queue))\n    consumer_task = asyncio.create_task(consumer(queue))\n    await asyncio.gather(producer_task, consumer_task)\n\nasyncio.run(main())<\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728\u6d88\u8d39\u8005\u7684 get \u65b9\u6cd5\u4e2d\u8bbe\u7f6e\u4e86\u8d85\u65f6\u65f6\u95f4\u4e3a 0.1 \u79d2\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u961f\u5217\u5728 0.1 \u79d2\u5185\u6ca1\u6709\u6570\u636e\u53ef\u53d6\uff0c\u4f1a\u629b\u51fa\u4e00\u4e2a asyncio.timeouterror \u5f02\u5e38\u3002<\/p>\n<p><strong>\u8d85\u65f6\u8bbe\u7f6e\u7684\u539f\u56e0<\/strong><\/p>\n<p>\u5728 asyncio \u4e2d\uff0c\u4efb\u52a1\u603b\u662f\u7531\u4e8b\u4ef6\u5faa\u73af\u8c03\u5ea6\u6267\u884c\u7684\u3002\u5f53\u4efb\u52a1\u7b49\u5f85\u5176\u4ed6\u4efb\u52a1\u65f6\uff08\u4f8b\u5982\u901a\u8fc7\u961f\u5217\uff09\uff0c\u5b83\u4f1a\u5c06\u81ea\u8eab\u6302\u8d77\uff0c\u76f4\u5230\u6709\u4e8b\u4ef6\u53d1\u751f\u3002\u5982\u679c\u7b49\u5f85\u65f6\u95f4\u8fc7\u957f\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\uff0c\u56e0\u4e3a\u4e8b\u4ef6\u5faa\u73af\u65e0\u6cd5\u5904\u7406\u5176\u4ed6\u4efb\u52a1\u3002<\/p>\n<p>\u901a\u8fc7\u8bbe\u7f6e\u8d85\u65f6\uff0c\u6211\u4eec\u9650\u5236\u4e86\u4efb\u52a1\u7b49\u5f85\u7684\u65f6\u95f4\u3002\u5982\u679c\u961f\u5217\u5728\u8d85\u65f6\u65f6\u95f4\u5185\u6ca1\u6709\u6570\u636e\u53ef\u53d6\uff0c\u4efb\u52a1\u4f1a\u7ee7\u7eed\u6267\u884c\uff0c\u800c\u4e0d\u662f\u65e0\u9650\u671f\u5730\u7b49\u5f85\u3002<\/p>\n<p><strong>\u5b9e\u73b0\u65b9\u6cd5<\/strong><\/p>\n<p>asyncio \u63d0\u4f9b\u4e86\u591a\u79cd\u8bbe\u7f6e\u8d85\u65f6\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>await queue.get(timeout=none)\uff1a\u4ece\u961f\u5217\u4e2d\u83b7\u53d6\u6570\u636e\uff0c\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\uff08\u5355\u4f4d\u4e3a\u79d2\uff09<\/li>\n<li>queue.get_nowait()\uff1a\u7acb\u5373\u4ece\u961f\u5217\u4e2d\u83b7\u53d6\u6570\u636e\uff0c\u4f46\u4e0d\u7b49\u5f85<\/li>\n<li>queue.join()\uff1a\u7b49\u5f85\u961f\u5217\u4e2d\u7684\u6240\u6709\u4efb\u52a1\u5b8c\u6210<\/li>\n<\/ul>\n<p>\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u3002\u5bf9\u4e8e\u9700\u8981\u4e25\u683c\u63a7\u5236\u7b49\u5f85\u65f6\u95f4\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u660e\u786e\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u3002\u5bf9\u4e8e\u4e0d\u9700\u8981\u7cbe\u786e\u63a7\u5236\u7b49\u5f85\u65f6\u95f4\u6216\u5e0c\u671b\u5728\u8d85\u65f6\u540e\u7acb\u5373\u7ee7\u7eed\u6267\u884c\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528 get_nowait \u6216 join\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aasyncio.Queue() \u5982\u4f55\u5b9e\u73b0\u8d85\u65f6\u5904\u7406\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>asyncio.Queue() &#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-205059","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205059","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=205059"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205059\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}