{"id":203069,"date":"2020-11-25T11:01:51","date_gmt":"2020-11-25T03:01:51","guid":{"rendered":"http:\/\/4563.org\/?p=203069"},"modified":"2020-11-25T11:01:51","modified_gmt":"2020-11-25T03:01:51","slug":"%e8%bf%99%e4%b8%aa%e5%8d%8f%e7%a8%8b%e4%be%8b%e5%ad%90%e4%b8%ad%ef%bc%8c-consumers-%e6%98%af%e6%80%8e%e4%b9%88%e8%a2%ab%e6%89%a7%e8%a1%8c%e7%9a%84%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=203069","title":{"rendered":"\u8fd9\u4e2a\u534f\u7a0b\u4f8b\u5b50\u4e2d\uff0c consumers \u662f\u600e\u4e48\u88ab\u6267\u884c\u7684\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8fd9\u4e2a\u534f\u7a0b\u4f8b\u5b50\u4e2d\uff0c consumers \u662f\u600e\u4e48\u88ab\u6267\u884c\u7684\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : killva4624 <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>import asyncio   # \u6d88\u8d39\u8005 async def consumer(n, q):     print(\"consumer {}: \u8fdb\u5165\u5546\u5e97\".format(n))     while True:         item = await q.get()         print(\"consumer {}: \u8d2d\u4e70\u4ea7\u54c1 {}\".format(n, item))         await asyncio.sleep(0.15)         q.task_done()     print(\"consumer {}: ending\".format(n))   # \u751f\u4ea7\u8005 A async def producer_a(q, num_workers):     print(\"\u751f\u4ea7\u8005 A: \u5f00\u59cb\u751f\u4ea7\")     for i in range(num_workers * 2):         await q.put(\"A \" + str(i))         print(\"\u751f\u4ea7\u8005 A: \u4e0a\u67b6\u4ea7\u54c1 A {}\".format(i))         await asyncio.sleep(0.01)   # \u751f\u4ea7\u8005 B async def producer_b(q, num_workers):     print(\"\u751f\u4ea7\u8005 B: \u5f00\u59cb\u751f\u4ea7\")     for i in range(num_workers * 2):         await q.put(\"B \" + str(i))         print(\"\u751f\u4ea7\u8005 B: \u4e0a\u67b6\u4ea7\u54c1 B {}\".format(i))         await asyncio.sleep(0.02)   # \u4efb\u52a1\u8c03\u5ea6 async def main(num_consumers, num_workers):     q = asyncio.Queue(maxsize=num_consumers)     print(\"\u521d\u59cb\u5316\u751f\u4ea7\u8005\")     prod_a = [asyncio.create_task(producer_a(q, num_workers))]     prod_b = [asyncio.create_task(producer_b(q, num_workers))]     print(\"\u521d\u59cb\u5316\u6d88\u8d39\u8005\")     consumers = [asyncio.create_task(consumer(i, q)) for i in range(num_consumers)]     print(\"asyncio \u8c03\u5ea6\u5f00\u59cb\")     await asyncio.gather(*prod_a, *prod_b)     print(\"\u751f\u4ea7\u8005 All: ending\")      await q.join()     print(\"\u6d88\u8d39\u8005 All: ending\")      for c in consumers:         c.cancel()   # main(\u6d88\u8d39\u8005\u6570\u91cf, \u751f\u4ea7\u8005\u6570\u91cf) asyncio.run(main(3, 2))  <\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code>\u521d\u59cb\u5316\u751f\u4ea7\u8005 \u521d\u59cb\u5316\u6d88\u8d39\u8005 asyncio \u8c03\u5ea6\u5f00\u59cb \u751f\u4ea7\u8005 A: \u5f00\u59cb\u751f\u4ea7 \u751f\u4ea7\u8005 A: \u4e0a\u67b6\u4ea7\u54c1 A 0 \u751f\u4ea7\u8005 B: \u5f00\u59cb\u751f\u4ea7 \u751f\u4ea7\u8005 B: \u4e0a\u67b6\u4ea7\u54c1 B 0 consumer 0: \u8fdb\u5165\u5546\u5e97 consumer 0: \u8d2d\u4e70\u4ea7\u54c1 A 0 consumer 1: \u8fdb\u5165\u5546\u5e97 consumer 1: \u8d2d\u4e70\u4ea7\u54c1 B 0 consumer 2: \u8fdb\u5165\u5546\u5e97 \u751f\u4ea7\u8005 A: \u4e0a\u67b6\u4ea7\u54c1 A 1 consumer 2: \u8d2d\u4e70\u4ea7\u54c1 A 1 \u751f\u4ea7\u8005 B: \u4e0a\u67b6\u4ea7\u54c1 B 1 \u751f\u4ea7\u8005 A: \u4e0a\u67b6\u4ea7\u54c1 A 2 \u751f\u4ea7\u8005 A: \u4e0a\u67b6\u4ea7\u54c1 A 3 consumer 0: \u8d2d\u4e70\u4ea7\u54c1 B 1 consumer 1: \u8d2d\u4e70\u4ea7\u54c1 A 2 \u751f\u4ea7\u8005 B: \u4e0a\u67b6\u4ea7\u54c1 B 2 consumer 2: \u8d2d\u4e70\u4ea7\u54c1 A 3 \u751f\u4ea7\u8005 B: \u4e0a\u67b6\u4ea7\u54c1 B 3 \u751f\u4ea7\u8005 All: ending consumer 0: \u8d2d\u4e70\u4ea7\u54c1 B 2 consumer 1: \u8d2d\u4e70\u4ea7\u54c1 B 3 \u6d88\u8d39\u8005 All: ending <\/code><\/pre>\n<p>\u6211\u7406\u89e3 await asyncio.gather(*prod_a, *prod_b) \u6267\u884c\u4e86 <strong>prod_a<\/strong> \u548c <strong>prod_b<\/strong> \uff0c\u4f46 <strong>consumers<\/strong> \u662f\u600e\u4e48\u88ab\u89e6\u53d1\u7684\u5462\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>3<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4314857\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : mercurylanded <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             create_task \u5c31\u89e6\u53d1\u4e86 gather \u662f\u7b49\u5f85\u7ed3\u675f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4314858\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : killva4624 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @mercurylanded \u8c22\u8c22\uff0c\u91cd\u65b0\u53bb\u770b\u4e86\u5b98\u65b9\u6587\u6863\u548c\u6e90\u7801\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4314859\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fasionchan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             create_task \u7684\u65f6\u5019\uff0c\u534f\u7a0b\u88ab\u521b\u5efa\uff0c\u4f46\u8fd8\u6ca1\u88ab\u8c03\u5ea6\u6267\u884c\uff1b<br \/>await gather \u7684\u65f6\u5019\uff0c\u7a0b\u5e8f\u6267\u884c\u6743\u88ab\u8f6c\u79fb\u5230 asyncio \u5185\u90e8\u7684\u4e8b\u4ef6\u5faa\u73af\uff1b<br \/>\u8fd9\u65f6\uff0c\u5148\u524d\u521b\u5efa\u7684\u534f\u7a0b\u5982\u679c\u8fbe\u5230\u6267\u884c\u6761\u4ef6\uff0c\u5c06\u88ab\u4e8b\u4ef6\u5faa\u73af\u8c03\u5ea6\u6267\u884c\uff1b<br \/>\u76f4\u5230 gather \u7b49\u5f85\u7684\u534f\u7a0b\uff0c\u4e5f\u5c31\u662f producer \u6267\u884c\u5b8c\u6bd5\uff0c\u7a0b\u5e8f\u6267\u884c\u6743\u56de\u5230\u5f53\u524d\u4ee3\u7801\uff1b<br \/>await q.join \u7684\u65f6\u5019\uff0c\u7a0b\u5e8f\u6267\u884c\u6743\u53c8\u88ab\u8f6c\u79fb\u5230\u4e8b\u4ef6\u5faa\u73af\uff0ccomsumer \u7ee7\u7eed\u6267\u884c\uff1b<br \/>\u76f4\u5230\u961f\u5217 q \u4e3a\u7a7a\uff0c\u8fd9\u65f6 consumer \u80af\u5b9a\u5df2\u7ecf\u5b8c\u6210\u6240\u6709\u6570\u636e\u6d88\u8d39\uff1b<\/p>\n<p>\u53ef\u4ee5\u81ea\u5df1\u52a8\u624b\u5199\u4e00\u4e2a\u6781\u7b80\u7684\u534f\u7a0b\u5e93\u52a0\u6df1\u5bf9\u534f\u7a0b\u8fd0\u884c\u539f\u7406\u7684\u7406\u89e3\uff0c\u53ea\u9700 100 \u6765\u884c\u4ee3\u7801\uff0c\u5373\u53ef\u4e00\u4e3e\u62ff\u4e0b\u534f\u7a0b\u3001\u4e8b\u4ef6\u5faa\u73af\u3001IO \u591a\u8def\u590d\u7528\u7b49\u6838\u5fc3\u6982\u5ff5\uff1a<\/p>\n<p>https:\/\/www.imooc.com\/read\/76\/article\/1935<\/p>\n<p>\u6211\u53ea\u770b asyncio \u6587\u6863\u65f6\uff0c\u603b\u6709\u4e00\u79cd\u4f3c\u61c2\u975e\u61c2\u7684\u611f\u89c9\uff1b\u81ea\u5df1\u6298\u817e\u8fc7\u4e00\u904d\u540e\u5c31\u6e05\u6670\u4e86\uff0c\u5404\u79cd\u6982\u5ff5\u5bf9\u53f7\u5165\u5ea7\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e2a\u534f\u7a0b\u4f8b\u5b50\u4e2d\uff0c consumer&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/203069"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=203069"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/203069\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=203069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=203069"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=203069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}