{"id":323472,"date":"2021-02-05T17:31:53","date_gmt":"2021-02-05T09:31:53","guid":{"rendered":"http:\/\/4563.org\/?p=323472"},"modified":"2021-02-05T17:31:53","modified_gmt":"2021-02-05T09:31:53","slug":"%e5%85%b3%e4%ba%8e-python-event-loop","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=323472","title":{"rendered":"\u5173\u4e8e Python event loop"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5173\u4e8e Python event loop               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : RandomAccess <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6709\u8fd9\u6837\u4e00\u4e2a\u534f\u7a0b<\/p>\n<pre><code>async def function(**kwargs):     session = aiohttp.ClientSession()     response = await session.request(**kwargs)     pass <\/code><\/pre>\n<p>\u5982\u679c\u4f7f\u7528 asyncio.run(function(arg=bala))\u548c new_event_loop \u4f1a\u51fa\u73b0<\/p>\n<pre><code>RuntimeError: Timeout context manager should be used inside a task <\/code><\/pre>\n<p>\u4f7f\u7528<\/p>\n<pre><code>loop = asyncio.get_event_loop() loop.run_until_complete(function(arg=bala)) loop.close() <\/code><\/pre>\n<p>\u4e0d\u4f1a\u51fa\u73b0\u4e0a\u4e0b\u6587\u7ba1\u7406\u7684\u95ee\u9898 \u8fd9\u4e24\u8005\u7684\u533a\u522b\u5728\u54ea\u91cc,\u7ffb\u4e86\u534a\u5929\u5b98\u65b9\u6587\u6863\u548c csdn \u7684\u8f6c\u8f7d\u6587\u5b9e\u5728\u65e0\u6cd5\u7406\u89e3 TAT<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>14<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5206418\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Lycnir <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             python3.7 \u6b63\u5e38                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5206419\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cissoid <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7ffb\u7ffb\u5b98\u65b9\u6587\u6863, <\/p>\n<p>asyncio.run:<br \/>This function cannot be called when another asyncio event loop is running in the same thread.                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5206420\" 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\u662f\u56e0\u4e3a\u4f60\u628a\u540c\u4e2a ClientSession \u7528\u5728\u4e24\u4e2a\u4e0d\u540c\u7684 eventloop \u4e0a\u3002asyncio.run \u8dd1\u5b8c\u540e\uff0cevent_loop \u4f1a\u5173\u95ed\u6389\u3002\u5728\u65b0\u7684 event_loop \u8dd1\u7684\u65f6\u5019\uff0c\u56e0\u4e3a\u7528\u7684\u540c\u4e00\u4e2a ClientSession\uff0c\u5b83\u7528\u4e86\u4e0a\u4e00\u4e2a\u5173\u95ed\u4e86\u7684 event_loop\uff0c\u5bfc\u81f4\u8fd9\u4e2a\u5f02\u5e38\u629b\u51fa\u3002<\/p>\n<p>\u4f60\u8fd9\u4e2a\u662f\u65e7\u7248\u7684 aiohttp \u5427\uff0c\u65b0\u7248\u7684 async_timeout \u4e0d\u4f1a\u51fa\u73b0\u8fd9\u4e2a\u95ee\u9898\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5206421\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : RandomAccess <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             DeprecationWarning: The object should be created within an async function<\/p>\n<p>\u770b\u6837\u5b50\u5e94\u8be5\u662f session \u5728\u7c7b\u5b9e\u4f8b\u5316\u65f6\u5728\u666e\u901a\u51fd\u6570\u4e2d\u521b\u5efa\u7684\u4ea7\u751f\u7684\u95ee\u9898\uff0c\u4e00\u4e2a\u5305\u542b\u5f02\u6b65\u51fd\u6570\u7684\u5bf9\u8c61\u5728\u53ea\u80fd\u5728 async \u51fd\u6570\u4e2d\u521b\u5efa\uff0c\u5305\u62ec\u81ea\u5df1\u5c01\u88c5\u7684\u7c7b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5206422\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ClericPy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             class TimerContext(BaseTimerContext):<br \/> &#8220;&#8221;&#8221; Low resolution timeout context manager &#8220;&#8221;&#8221;<\/p>\n<p> def __init__(self, loop: asyncio.AbstractEventLoop) -&gt; None:<br \/> raise RuntimeError(<br \/> &#8220;Timeout context manager should be used &#8221; &#8220;inside a task&#8221;<br \/> )<\/p>\n<p>\u8001\u95ee\u9898\u4e86, \u6240\u4ee5\u6211\u73b0\u5728\u7528\u5230 aiohttp \u5c3d\u91cf\u4e0d\u7528 asyncio.run, \u56e0\u4e3a\u5b83\u4e00\u5b9a\u65b0\u5efa\u4e00\u4e2a loop, \u7136\u800c Session \u7684\u9ed8\u8ba4 Timeout \u5bf9\u8c61\u5374\u662f\u5728\u534f\u7a0b\u5916\u9762\u521d\u59cb\u5316\u7684, \u5bfc\u81f4\u4e24\u4e2a\u5faa\u73af\u4e0d\u4e00\u81f4, \u89c1\u4e0b\u9762\u6e90\u7801<\/p>\n<p>https:\/\/github.com\/aio-libs\/aiohttp\/blob\/742a8b6d09b2623670ddede838c913d2a8a4d89e\/aiohttp\/client.py#L161<\/p>\n<p>\u636e\u8bf4 aiohttp 4.0 \u4ee5\u540e\u4f1a\u597d, \u4f46\u662f 4.0 \u4e00\u76f4\u53d1\u4e0d\u51fa\u6765<\/p>\n<p>\u60f3\u89e3\u51b3\u7684\u8bdd, \u7b80\u5355\u7684\u5c31\u662f\u6682\u65f6\u522b\u7528 asyncio.run. \u6216\u8005\u5728\u534f\u7a0b\u51fd\u6570\u91cc import \u5b83, \u6216\u8005\u624b\u52a8\u6307\u5b9a Timeout \u8bd5\u8bd5                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5206423\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : RandomAccess <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ClericPy \u8c22\u8c22\u5927\u4f6c<br \/>\u5982\u679c\u4e1a\u52a1\u573a\u666f\u9700\u8981 session \u5728\u4e24\u4e2a\u963b\u585e\u7684\u4e8b\u4ef6\u5faa\u73af\u4e2d\u4f20\u9012\u662f\u4e0d\u662f\u4e0d\u53ef\u884c\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5206424\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : guochao <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7b80\u5355\u8bf4 python \u7684 event loop \u662f\u4e00\u4e2a\u5355\u8fdb\u7a0b\u5355\u7ebf\u7a0b\u5355\u4e2a loop \u6267\u884c coroutine \u7684\u8fc7\u7a0b\u3002asyncio.run \u5c31\u662f\u4e00\u4e2a\u521b\u5efa loop \u5e76\u4e14 run until complete \u7684\u8fc7\u7a0b\uff0cnew event loop \u53c8\u5efa\u4e86\u4e00\u4e2a\uff0c\u6ca1\u6709\u5fc5\u8981\u3002<\/p>\n<p>\u89e3\u51b3\u529e\u6cd5\u5c31\u662f\u8ba9\u6574\u4e2a\u7a0b\u5e8f\u7684\u5165\u53e3\u662f\u4e00\u4e2a async \u51fd\u6570\uff0c\u5728 async \u51fd\u6570\u4e2d\u914d\u7f6e\u7a0b\u5e8f\uff0c\u65b0\u5efa\u5404\u79cd\u5b9e\u4f8b\uff0c\u542f\u52a8\u5e94\u7528\u3002\u5728__main__\u91cc\u9762 asyncio.run(entrypoint())\u5c31\u884c\u3002\u7136\u540e\u5728\u4efb\u4f55\u5176\u4ed6\u5730\u65b9\u90fd\u4e0d\u8981 new_event_loop \u6216\u8005 asyncio.run<\/p>\n<p>\u5982\u679c\u8981\u6267\u884c\u4e00\u4e2a coroutine \u4f46\u662f\u4e0d\u7b49\u5f85\u7ed3\u675f\uff0c\u53ef\u4ee5 asyncio.ensure_future \u6216\u8005 3.7 \u4ee5\u4e0a\u7684\u65b0 API asyncio.create_task\uff0c\u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u662f\u5728 get_event_loop()\u8fd4\u56de\u7684 loop \u4e0a\u6267\u884c\u5bf9\u5e94\u7684\u51fd\u6570\uff0c\u4f1a\u8fd4\u56de\u4e00\u4e2a Task<br \/>\u5982\u679c\u8981\u5728\u4e00\u4e2a coroutine \u4e2d\u6267\u884c\u4e00\u4e2a coroutine \u5e76\u4e14\u7b49\u5f85\u7ed3\u675f\uff0c\u90a3\u5c31\u76f4\u63a5 await \u3002<br \/>\u5982\u679c\u8981\u6267\u884c\u4e00\u4e2a\u540c\u6b65\u8fc7\u7a0b\uff0c\u53ef\u4ee5\u7528 run_in_executor\uff0c\u8fd4\u56de\u4e00\u4e2a Future                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5206425\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : RandomAccess <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @guochao thanks                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5206426\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ClericPy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @RandomAccess <br \/>\u65f6\u95f4\u5faa\u73af\u5c3d\u53ef\u80fd\u53ea\u7528\u4e00\u4e2a, \u5c31\u7b97\u534f\u7a0b\u7528\u7684\u5f88\u719f\u6089\u7684\u4eba, \u4e5f\u5f88\u5c11\u53bb\u591a\u4e2a\u7ebf\u7a0b\u4e0a\u8dd1\u591a\u4e2a loop<br \/>\u6240\u4ee5 Python 3.9 \u8fd8\u662f 3.10 \u4e4b\u540e, \u5f88\u591a\u5185\u7f6e\u7684\u534f\u7a0b\u65b9\u6cd5\u90fd\u53bb\u6389\u4e86 loop \u53c2\u6570, \u9ed8\u8ba4\u90fd\u4ece running loop \u91cc\u9762\u83b7\u53d6<\/p>\n<p>\u4f60\u8981\u505a\u7684\u662f, \u9996\u5148\u4fdd\u8bc1\u6574\u4e2a\u7a0b\u5e8f\u53ea\u7559\u6709\u4e00\u4e2a\u4e8b\u4ef6\u5faa\u73af(\u56e0\u4e3a\u591a\u4e2a\u4e5f\u6ca1\u6709\u610f\u4e49, await \u4e0d\u662f\u963b\u585e, \u662f\u7b49\u5f85), \u7136\u540e\u5728\u91cc\u9762\u4f20\u9012 session \u5bf9\u8c61\u5c31\u662f\u5408\u6cd5\u7684\u4e86<\/p>\n<p>\u7136\u540e\u4f60\u8bf4\u7684\u963b\u585e, \u534f\u7a0b\u5b9e\u9645\u4e0a\u662f\u975e\u963b\u585e\u7684\u8bbe\u8ba1, \u4f60\u7684\u963b\u585e\u53ef\u80fd\u662f await \u5173\u952e\u8bcd, \u90a3\u4e2a\u662f\u7b49\u5f85\u4e0d\u7b97\u963b\u585e. \u6240\u4ee5\u4f60\u5982\u679c\u60f3\u5e76\u53d1\u5f00\u59cb\u591a\u4e2a\u4efb\u52a1, \u53ef\u4ee5\u628a &#8220;\u534f\u7a0b\u51fd\u6570&#8221; (async def \u58f0\u660e\u7684)\u6267\u884c\u83b7\u5f97\u7684 &#8220;\u534f\u7a0b\u5bf9\u8c61&#8221; \u521b\u5efa\u4e3a Task, \u5b83\u5c31\u4f1a\u5f00\u59cb\u6267\u884c\u4f46\u662f\u53c8\u4e0d\u4f1a\u963b\u585e, \u521b\u5efa Task \u7684\u65b9\u5f0f\u6709 asyncio.ensure_future(some_coro) \u6216\u8005 asyncio.create_task(coro), \u540e\u8005\u662f 3.7 \u4ee5\u540e\u65b0\u589e\u7684                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5206427\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : RandomAccess <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ClericPy \u786e\u5b9e\u662f\u5e94\u8be5\u4e00\u4e2a\u8fdb\u7a0b\u6216\u7ebf\u7a0b\u53ea\u4f7f\u7528\u4e00\u4e2a\u4e8b\u4ef6\u5faa\u73af\uff0c\u53ea\u662f\u4e1a\u52a1\u573a\u666f\u9700\u8981\u8fdb\u884c\u4e00\u6b21\u957f io \u4e14\u903b\u8f91\u590d\u6742\u7684\u64cd\u4f5c A \u83b7\u53d6\u64cd\u4f5c B \u6240\u9700\u8981\u7684\u5fc5\u8981\u53c2\u6570\uff0c\u7136\u540e\u4efb\u52a1 B \u53bb\u6267\u884c\u591a\u5e76\u53d1\u7684\u4e5f\u662f\u957f io \u7684\u64cd\u4f5c\uff0cAB \u6709\u7740\u524d\u540e\u987a\u5e8f\uff0c\u903b\u8f91\u4e0a\u5e94\u5f53\u662f\u963b\u585e\u7684\uff0c\u4f46\u662f\u5404\u81ea\u5185\u90e8\u64cd\u4f5c\u662f\u65e0\u5e8f\u7684\u957f io\uff0c\u60f3\u5c1d\u8bd5\u591a\u4e2a\u4e8b\u4ef6\u5faa\u73af\u662f\u5426\u9002\u5e94\u8fd9\u4e2a\u573a\u666f\u6216\u591a\u4e2a\u4e0d\u540c\u5e76\u53d1\u7684\u534f\u7a0b\u6709\u6ca1\u6709\u597d\u7684\u987a\u5e8f\u6267\u884c\u65b9\u6848                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5206428\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : RandomAccess <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @RandomAccess \u64cd\u4f5c A \u548c B \u5404\u81ea\u90fd\u662f\u5d4c\u5957\u7684\u534f\u7a0b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5206429\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ClericPy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @RandomAccess <br \/>\u4e00\u6837\u662f\u4e00\u4e2a\u4e8b\u4ef6\u5faa\u73af\u6765\u89e3\u51b3\u7684, \u5148\u540e\u987a\u5e8f\u7684\u963b\u585e\u7528 await \u6216\u8005\u534f\u7a0b\u961f\u5217\u90fd\u884c<br \/>\u975e\u8981\u591a\u7ebf\u7a0b\u6216\u8005\u975e\u534f\u7a0b\u7684, \u8d70 run_in_executor                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5206430\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : RandomAccess <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ClericPy thanks                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5206431\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : RandomAccess <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ClericPy \u5bf9\u534f\u7a0b\u7684\u7406\u89e3\u4e0d\u662f\u5f88\u6df1\u523b\uff0c\u521a\u624d\u60f3\u901a await \u7684\u7b49\u5f85\u4ea4\u51fa\u6267\u884c\u6743\u5176\u5b9e\u4e5f\u53ef\u4ee5\u5728\u4ee3\u7801\u903b\u8f91\u5b9e\u73b0\u524d\u540e\u987a\u5e8f\uff0c\u6ca1\u5fc5\u8981\u590d\u6742\u5316\u591a\u4e2a\u4e8b\u4ef6\u5faa\u73af                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e Python event l&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\/323472"}],"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=323472"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/323472\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=323472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=323472"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=323472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}