{"id":359816,"date":"2021-02-14T13:05:59","date_gmt":"2021-02-14T05:05:59","guid":{"rendered":"http:\/\/4563.org\/?p=359816"},"modified":"2021-02-14T13:05:59","modified_gmt":"2021-02-14T05:05:59","slug":"asyncio-%e5%a4%9a%e7%ba%bf%e7%a8%8b%e4%b8%ad%e5%ad%90%e7%ba%bf%e7%a8%8b%e4%bd%bf%e7%94%a8%e4%b8%bb%e7%ba%bf%e7%a8%8b-event-loop%ef%bc%8c%e8%bf%99%e6%a0%b7%e6%98%af%e5%90%a6%e4%b8%8e%e7%9b%b4","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=359816","title":{"rendered":"[asyncio] \u591a\u7ebf\u7a0b\u4e2d\u5b50\u7ebf\u7a0b\u4f7f\u7528\u4e3b\u7ebf\u7a0b event loop\uff0c\u8fd9\u6837\u662f\u5426\u4e0e\u76f4\u63a5\u7528\u4e3b\u7ebf\u7a0b\u53bb\u8fdb\u884c\u4e8b\u4ef6\u5faa\u73af\u65e0\u5dee\u522b"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  [asyncio] \u591a\u7ebf\u7a0b\u4e2d\u5b50\u7ebf\u7a0b\u4f7f\u7528\u4e3b\u7ebf\u7a0b event loop\uff0c\u8fd9\u6837\u662f\u5426\u4e0e\u76f4\u63a5\u7528\u4e3b\u7ebf\u7a0b\u53bb\u8fdb\u884c\u4e8b\u4ef6\u5faa\u73af\u65e0\u5dee\u522b               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : opengo <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4f7f\u7528<code>concurrent.futures<\/code>\u521b\u5efa\u7ebf\u7a0b\u6c60\u7136\u540e\u4f7f\u7528<code>asyncio<\/code>\u7684<code>run_in_executor<\/code>\u65b9\u6cd5\u53bb\u5229\u7528\u7ebf\u7a0b\u6c60\u6267\u884c\u67d0\u5f02\u6b65\u4efb\u52a1\uff0c\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code># -*- coding: utf-8 -*- import asyncio import concurrent.futures import threading import time import logging  from datetime import datetime   logger = logging.getLogger(__file__) logger.setLevel('INFO')   async def run_task(val):     logger.info(f\"run_task Owning thread - {threading.currentThread().native_id} {threading.currentThread().getName()}\")     await asyncio.sleep(val)     return datetime.now()   def thread_task(loop, val):     logger.info(f\"thread_task Start thread - {threading.currentThread().ident} {threading.currentThread().getName()}\")      v = asyncio.run_coroutine_threadsafe(run_task(val), loop)     result = v.result()      logger.info(f\"thread - {threading.currentThread().ident}  {threading.currentThread().getName()} done ==== {result}\")   async def main(loop):     result = [0.1, 0.2, 0.3, 0.4, 0.5]     logger.info(f\"main thread :: {threading.currentThread().ident} {threading.currentThread().getName()}\")      with concurrent.futures.ThreadPoolExecutor() as pool:         blocking_tasks = [             loop.run_in_executor(                 pool, thread_task, loop, val             ) for val in result         ]         await asyncio.gather(*blocking_tasks)   if __name__ == '__main__':      loop = asyncio.get_event_loop()     try:         loop.run_until_complete(main(loop))     finally:         loop.close()  <\/code><\/pre>\n<p>\u4f7f\u7528<code>run_in_executor<\/code>\u8c03\u7528<code>thread_task<\/code>\u65b9\u6cd5\uff0c\u518d\u5728\u5b50\u7ebf\u7a0b\u4e2d\u4f7f\u7528 <code>asyncio.run_coroutine_threadsafe<\/code> \u65b9\u6cd5\u53bb\u8c03\u7528\u771f\u6b63\u7684\u5f02\u6b65\u4efb\u52a1\uff0c\u800c\u5b9e\u9645\u4e0a\u6e90\u7801\u4e2d <code>asyncio.run_coroutine_threadsafe<\/code> \u65b9\u6cd5\u901a\u8fc7<code>ensure_future<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a<code>future<\/code> \u6dfb\u52a0\u5230\u4e3b\u7ebf\u7a0b\u7684 event loop \u4e2d\u5e76\u7ed1\u5b9a\u5f53\u524d\u7ebf\u7a0b future\uff0c\u5b50\u7ebf\u7a0b\u7684\u5f02\u6b65\u4efb\u52a1\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u88ab\u5faa\u73af\uff0c\u5faa\u73af\u5b8c\u6210\u540e\u518d\u4ece\u5b50\u7ebf\u7a0b\u4e2d\u53bb\u83b7\u53d6\u7ed3\u679c<\/p>\n<p>\u611f\u89c9\u4e0a\u4e0d\u5982\u76f4\u63a5\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u4f7f\u7528<code>asyncio.gather<\/code> \u6216\u8005 <code>asyncio.as_completed<\/code> \u53bb\u5e76\u53d1\u6267\u884c\u8fd9\u4e9b\u4efb\u52a1\uff0c\u53e6\u4e00\u79cd\u529e\u6cd5\u662f\u6bcf\u4e2a\u5b50\u7ebf\u7a0b\u518d\u8bbe\u7f6e\u4e00\u4e2a loop \u53bb\u8fdb\u884c\u4e8b\u4ef6\u5faa\u73af\uff0c\u4f46\u662f\u5b9e\u9645\u6d4b\u8bd5\u4e2d\u8fd9\u51e0\u79cd\u65b9\u6848\u6027\u80fd\u76f8\u5dee\u5e76\u4e0d\u591a<\/p>\n<pre><code># \u4f7f\u7528 asyncio.gather \u6216 asyncio.as_completed task_list = []          for val in result:         task_list.append(asyncio.create_task(run_task(val)))              await asyncio.gather(*task_list)      for f in asyncio.as_completed(task_list, loop=loop):         results = await f   # \u8bbe\u7f6e\u65b0\u7684 loop def thread_task(val):      logger.info(f\"thread_task Start thread - {threading.currentThread().ident} {threading.currentThread().getName()}\")      loop = asyncio.new_event_loop()     try:         asyncio.set_event_loop(loop)         v = loop.run_until_complete(asyncio.gather(run_task(val)))         logger.info(f\"thread - {threading.currentThread().ident}  {threading.currentThread().getName()} done ==== {result}\")     finally:         loop.close()  <\/code><\/pre>\n<p>\u5927\u5bb6\u6709\u4ec0\u4e48\u597d\u7684\u591a\u7ebf\u7a0b+\u534f\u7a0b\u7684\u5b9e\u73b0\u65b9\u6848\u5417\uff0c\u8fd9\u91cc\u5bf9\u5e94\u7684\u573a\u666f\u662f\u540c\u65f6\u5904\u7406\u591a\u4e2a\u6587\u4ef6\u7684 io \u4efb\u52a1<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5409097\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : liuxingdeyu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6ca1\u592a\u641e\u660e\u767d\uff0c\u7ebf\u7a0b\u52a0\u534f\u7a0b\u7684\u610f\u4e49                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5409098\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : opengo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @liuxingdeyu \u53ef\u4ee5\u591a\u7ebf\u7a0b\u53bb\u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42\uff0c\u6587\u4ef6 IO\uff0c\u8fd9\u91cc\u6211\u60f3\u5229\u7528\u591a\u7ebf\u7a0b\u53bb\u5904\u7406\u6587\u4ef6 IO\uff0c\u5229\u7528\u534f\u7a0b\u53bb\u63d0\u9ad8\u6027\u80fd\uff0c\u591a\u8fdb\u7a0b+\u534f\u7a0b\u7684\u65b9\u5f0f\u9762\u5bf9\u5927\u91cf\u6587\u4ef6 IO \u64cd\u4f5c\u6027\u80fd\u66f4\u597d\uff0c\u4f46\u662f\u8d44\u6e90\u5f00\u9500\u4e5f\u66f4\u5927\uff0c\u6240\u4ee5\u60f3\u5c1d\u8bd5\u591a\u7ebf\u7a0b+\u534f\u7a0b\u7684\u6700\u4f18\u65b9\u6848                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5409099\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : linw1995 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5e94\u8be5\u7b49\u540c\uff0c\u4f46\u4f1a\u66f4\u6162                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5409100\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : opengo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @linw1995 \u786e\u5b9e\u4e0d\u5982\u5355\u7ebf\u7a0b+\u534f\u7a0b\uff0c\u5e94\u8be5\u662f\u7ebf\u7a0b\u6c60\u5316+\u4e0a\u4e0b\u6587\u5207\u6362\u6d88\u8017\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>[asyncio] \u591a\u7ebf\u7a0b\u4e2d\u5b50\u7ebf\u7a0b&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\/359816"}],"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=359816"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/359816\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=359816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=359816"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=359816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}