{"id":4033,"date":"2021-12-06T14:09:35","date_gmt":"2021-12-06T06:09:35","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=4033"},"modified":"2021-12-06T14:09:35","modified_gmt":"2021-12-06T06:09:35","slug":"%e3%80%90python%e3%80%91%e5%8d%8f%e7%a8%8bcoroutines","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/python\/4033\/","title":{"rendered":"\u3010python\u3011\u534f\u7a0bCoroutines"},"content":{"rendered":"\n<p>\u6211\u4eec\u90fd\u77e5\u9053\u6709\u7ebf\u7a0b\u548c\u8fdb\u7a0b\uff0c\u7b80\u5355\u8bf4\uff1a\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u6709\u591a\u4e2a\u7ebf\u7a0b\uff0c\u8fdb\u7a0b\u7531\u7cfb\u7edf\u8fdb\u884c\u8c03\u5ea6\uff0c\u800c\u7ebf\u7a0b\u5219\u6709\u8f6f\u4ef6\uff08\u4e5f\u5c31\u662f\u7f16\u5199\u8005\uff09\u8fdb\u884c\u8c03\u5ea6\u3002<\/p>\n\n\n<p>\u7ebf\u7a0b\u4e0e\u8fdb\u7a0b\u7684\u533a\u522b\u4e3b\u8981\u6709\uff1a<\/p>\n\n\n<ul><li> \u5730\u5740\u7a7a\u95f4:\u7ebf\u7a0b\u662f\u8fdb\u7a0b\u5185\u7684\u4e00\u4e2a\u6267\u884c\u5355\u5143\uff0c\u8fdb\u7a0b\u5185\u81f3\u5c11\u6709\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u5b83\u4eec\u5171\u4eab\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\uff0c\u800c\u8fdb\u7a0b\u6709\u81ea\u5df1\u72ec\u7acb\u7684\u5730\u5740\u7a7a\u95f4 <\/li><li> \u8d44\u6e90\u62e5\u6709:\u8fdb\u7a0b\u662f\u8d44\u6e90\u5206\u914d\u548c\u62e5\u6709\u7684\u5355\u4f4d,\u540c\u4e00\u4e2a\u8fdb\u7a0b\u5185\u7684\u7ebf\u7a0b\u5171\u4eab\u8fdb\u7a0b\u7684\u8d44\u6e90 <\/li><li> \u7ebf\u7a0b\u662f\u5904\u7406\u5668\u8c03\u5ea6\u7684\u57fa\u672c\u5355\u4f4d,\u4f46\u8fdb\u7a0b\u4e0d\u662f <\/li><li> \u4e8c\u8005\u5747\u53ef\u5e76\u53d1\u6267\u884c <\/li><li>  \u6bcf\u4e2a\u72ec\u7acb\u7684\u7ebf\u7a0b\u6709\u4e00\u4e2a\u7a0b\u5e8f\u8fd0\u884c\u7684\u5165\u53e3\u3001\u987a\u5e8f\u6267\u884c\u5e8f\u5217\u548c\u7a0b\u5e8f\u7684\u51fa\u53e3\uff0c\u4f46\u662f\u7ebf\u7a0b\u4e0d\u80fd\u591f\u72ec\u7acb\u6267\u884c\uff0c\u5fc5\u987b\u4f9d\u5b58\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u7531\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u591a\u4e2a\u7ebf\u7a0b\u6267\u884c\u63a7\u5236 <\/li><\/ul>\n\n\n<p>\u7ebf\u7a0b\u548c\u534f\u7a0b\u7684\u533a\u522b\u4e3b\u8981\u6709\uff1a<\/p>\n\n\n<ul><li> \u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u591a\u4e2a\u534f\u7a0b\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u4e5f\u53ef\u4ee5\u5355\u72ec\u62e5\u6709\u591a\u4e2a\u534f\u7a0b\u3002 <\/li><li> \u7ebf\u7a0b\u8fdb\u7a0b\u90fd\u662f\u540c\u6b65\u673a\u5236\uff0c\u800c\u534f\u7a0b\u5219\u662f\u5f02\u6b65 \u3002<\/li><li> \u534f\u7a0b\u80fd\u4fdd\u7559\u4e0a\u4e00\u6b21\u8c03\u7528\u65f6\u7684\u72b6\u6001\uff0c\u6bcf\u6b21\u8fc7\u7a0b\u91cd\u5165\u65f6\uff0c\u5c31\u76f8\u5f53\u4e8e\u8fdb\u5165\u4e0a\u4e00\u6b21\u8c03\u7528\u7684\u72b6\u6001\u3002 <\/li><li> \u7ebf\u7a0b\u662f\u62a2\u5360\u5f0f\uff0c\u800c\u534f\u7a0b\u662f\u975e\u62a2\u5360\u5f0f\u7684\uff0c\u6240\u4ee5\u9700\u8981\u7528\u6237\u81ea\u5df1\u91ca\u653e\u4f7f\u7528\u6743\u6765\u5207\u6362\u5230\u5176\u4ed6\u534f\u7a0b\uff0c\u56e0\u6b64\u540c\u4e00\u65f6\u95f4\u5176\u5b9e\u53ea\u6709\u4e00\u4e2a\u534f\u7a0b\u62e5\u6709\u8fd0\u884c\u6743\uff0c\u76f8\u5f53\u4e8e\u5355\u7ebf\u7a0b\u7684\u80fd\u529b\u3002 <\/li><li> \u534f\u7a0b\u5e76\u4e0d\u662f\u53d6\u4ee3\u7ebf\u7a0b, \u800c\u4e14\u62bd\u8c61\u4e8e\u7ebf\u7a0b\u4e4b\u4e0a, \u7ebf\u7a0b\u662f\u88ab\u5206\u5272\u7684CPU\u8d44\u6e90, \u534f\u7a0b\u662f\u7ec4\u7ec7\u597d\u7684\u4ee3\u7801\u6d41\u7a0b, \u534f\u7a0b\u9700\u8981\u7ebf\u7a0b\u6765\u627f\u8f7d\u8fd0\u884c\u3002<\/li><li> \u7ebf\u7a0b\u662f\u534f\u7a0b\u7684\u8d44\u6e90\u3002\u534f\u7a0b\u901a\u8fc7Interceptor\u6765\u95f4\u63a5\u4f7f\u7528\u7ebf\u7a0b\u8fd9\u4e2a\u8d44\u6e90\u3002  <\/li><\/ul>\n\n\n<p>\u4e0b\u9762\u6765\u4ecb\u7ecd\u534f\u7a0b\u3002<\/p>\n\n\n<p>\u9996\u5148\u9700\u8981\u4e86\u89e3\u4e00\u4e9b\u6982\u5ff5\uff1a<\/p>\n\n\n<ul><li>event_loop\uff1a\u4e8b\u4ef6\u5faa\u73af\uff0c\u76f8\u5f53\u4e8e\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u4e00\u4e9b\u51fd\u6570\u6ce8\u518c\u5230\u8fd9\u4e2a\u4e8b\u4ef6\u5faa\u73af\u4e0a\uff0c\u5f53\u6ee1\u8db3\u6761\u4ef6\u53d1\u751f\u7684\u65f6\u5019\uff0c\u5c31\u53bb\u8c03\u7528\u5bf9\u5e94\u7684\u5904\u7406\u65b9\u6cd5\u3002<\/li><li>coroutine\uff1a\u4e2d\u6587\u7ffb\u8bd1\u53eb\u534f\u7a0b\uff0c\u5728python\u4e2d\u5e38\u6307\u4ee3\u643a\u7a0b\u5bf9\u8c61\u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u534f\u7a0b\u5bf9\u8c61\u6ce8\u518c\u5230\u65f6\u95f4\u5faa\u73af\u4e2d\uff0c\u4ed6\u4f1a\u88ab\u4e8b\u4ef6\u5faa\u73af\u8c03\u7528<strong>\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528async\u5173\u952e\u5b57\u6765\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5<\/strong>\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5728\u8c03\u7528\u65f6\u4e0d\u4f1a\u7acb\u523b\u88ab\u6267\u884c\uff0c\u800c\u662f\u4f1a\u8fd4\u56de\u4e00\u4e2a\u534f\u7a0b\u5bf9\u8c61\u3002<\/li><li>task\uff1a\u4efb\u52a1\uff0c\u8fd9\u662f\u5bf9\u534f\u7a0b\u5bf9\u8c61\u7684\u8fdb\u4e00\u6b65\u5c01\u88c5\uff0c\u5305\u542b\u534f\u7a0b\u5bf9\u8c61\u7684\u5404\u4e2a\u72b6\u6001\u3002<\/li><li>future\uff1a\u4ee3\u8868\u5c06\u6765\u6267\u884c\u6216\u8005\u6ca1\u6709\u6267\u884c\u7684\u4efb\u52a1\u7684\u7ed3\u679c\uff0c\u5b9e\u9645\u4e0a\u548ctask\u6ca1\u6709\u672c\u8d28\u533a\u522b\u3002<\/li><\/ul>\n\n\n<p>\u4e86\u89e3\u6982\u5ff5\u540e\uff0c\u6211\u4eec\u7f16\u5199\u4e00\u4e9b\u4ee3\u7801\u6765\u66f4\u6df1\u5c42\u6b21\u4e86\u89e3\u534f\u7a0b\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code>import asyncio\nasync def execute(x):\n    print('Number:', x)\ncoroutine = execute(1)\nprint('Coroutine:', coroutine)\nprint('after calling function')\nloop = asyncio.get_event_loop()\nloop.run_until_complete(coroutine)\nprint('after calling loop')\n<\/code><\/pre>\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code>Coroutine: &lt;coroutine object execute at 0x000001FA66112780>\nafter calling function\nNumber: 1\nafter calling loop<\/code><\/pre>\n\n\n<p>\u53ef\u4ee5\u770b\u51fa\uff0c\u521a\u5f00\u59cb\u8c03\u7528execute(1) \u5e76\u6ca1\u6709\u8fdb\u884c\u8f93\u51fa\uff0c\u800c\u662f\u8fd4\u56de\u4e86\u4e00\u4e2aCoroutine\u5bf9\u8c61\uff0c\u800c\u5728\u8fd0\u884cget_event_loop\u540e\u624d\u6267\u884c\u4e86\u51fd\u6570\u3002<\/p>\n\n\n<p>\u53ef\u89c1\uff0casync\u5b9a\u4e49\u7684\u65b9\u6cd5\u4f1a\u53d8\u6210\u4e00\u4e2a\u65e0\u6cd5\u6267\u884c\u7684\u534f\u7a0b\u5bf9\u8c61\uff0c\u5fc5\u987b\u5c06\u6b64\u5bf9\u8c61\u6ce8\u518c\u5230\u4e8b\u4ef6\u5faa\u73af\u4e2d\u624d\u53ef\u6267\u884c\u3002<\/p>\n\n\n<p>\u524d\u9762\u6211\u4eec\u8fd8\u63d0\u5230\u4e86task\uff0c\u5b83\u662f\u5bf9\u534f\u7a0b\u5bf9\u8c61\u7684\u8fdb\u4e00\u6b65\u5c01\u88c5\uff0c\u6bd4\u534f\u7a0b\u5bf9\u8c61\u8eb2\u4e86\u8fd0\u884c\u72b6\u6001\uff0c\u4f8b\u5982running\uff0cfinished\u7b49\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u8fd9\u4e9b\u72b6\u6001\u83b7\u53d6\u534f\u7a0b\u5bf9\u8c61\u7684\u6267\u884c\u60c5\u51b5\u3002<\/p>\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u628a\u534f\u7a0b\u5bf9\u6027\u7231\u90a3\u4e2acoroutine\u4f20\u9012\u7ed9run_until_complete\u7684\u65f6\u5019\uff0c\u5b9e\u9645\u4e0a\u662f\u5bf9\u5b83\u8fdb\u884c\u4e86\u4e00\u4e2a\u64cd\u4f5c\uff0c\u5c31\u662f\u5c06coroutine\u5c01\u88c5\u6210\u4e86task\u5bf9\u8c61\u3002\u5bf9\u6b64\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u663e\u5f0f\u7684\u58f0\u660e\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code>import asyncio\nasync def execute(x):\n    print('Number:', x)\n    return x\ncoroutine = execute(1)\nprint('Coroutine:', coroutine)\nprint('after calling funtion')\nloop = asyncio.get_event_loop()\ntask = loop.create_task(coroutine)\nprint('Task:', task)\nloop.run_until_complete(task)\nprint('Task:', task)\nprint('after calling loop')<\/code><\/pre>\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code>Coroutine: &lt;coroutine object execute at 0x000002527FF62780>\nafter calling funtion\nTask: &lt;Task pending coro=&lt;execute() running at ....\/coroutines.py:55>>\nNumber: 1\nTask: &lt;Task finished coro=&lt;execute() done, defined at....\/coroutines.py:55> result=1>\nafter calling loop<\/code><\/pre>\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u534f\u7a0b\u7684\u4e24\u79cd\u8f6c\u6001\uff1apending\u548cfinished\u3002<\/p>\n\n\n<p>\u6211\u4eec\u5f53\u7136\u4e5f\u53ef\u4ee5\u4e3atask\u7ed1\u5b9a\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code>import asyncio\nasync def execute(x):\n    print('Number:', x)\n    return x\ndef callback(task):\n    print('Status:', task.result())\ncoroutine = execute(1)\nprint('Coroutine:', coroutine)\nloop = asyncio.get_event_loop()\ntask = loop.create_task(coroutine)\ntask.add_done_callback(callback)\nprint('Task:', task)\nloop.run_until_complete(task)\nprint('Task:', task)<\/code><\/pre>\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code>Coroutine: &lt;coroutine object execute at 0x0000014BFAF93780>\nTask: &lt;Task pending coro=&lt;execute() running at ....\/coroutines.py:75> cb=[callback() at ....\/coroutines.py:80]>\nNumber: 1\nStatus: 1\nTask: &lt;Task finished coro=&lt;execute() done, defined at ....\/coroutines.py:75> result=1><\/code><\/pre>\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u5b8c\u6210\u540e\u7684callback\u51fd\u6570\u5df2\u7ecf\u88ab\u6267\u884c\u3002<\/p>\n\n\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ea\u6267\u884c\u4e86\u4e00\u6b21\u51fd\u6570\u3002\u90a3\u4e48\uff0c\u5982\u679c\u5982\u4f55\u6267\u884c\u591a\u6b21\u5462\uff1f<\/p>\n\n\n<pre class=\"wp-block-code\"><code>import asyncio\nasync def execute(x):\n    print('Number:', x)\n    return x\ntasks = [asyncio.ensure_future(execute(i)) for i in range(5)]\nprint('Task:', tasks)\nloop = asyncio.get_event_loop()\ntask = loop.create_task(asyncio.wait(tasks))\nloop.run_until_complete(task)<\/code><\/pre>\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code>Task: [&lt;Task pending coro=&lt;execute() running at ....\/coroutines.py:75>>, &lt;Task pending coro=&lt;execute() running at ....\/coroutines.py:75>>, &lt;Task pending coro=&lt;execute() running at ....\/coroutines.py:75>>, &lt;Task pending coro=&lt;execute() running at....\/coroutines.py:75>>, &lt;Task pending coro=&lt;execute() running at ....\/coroutines.py:75>>]\nNumber: 0\nNumber: 1\nNumber: 2\nNumber: 3\nNumber: 4<\/code><\/pre>\n\n\n<p>\u5230\u8fd9\u91cc\uff0c\u5176\u5b9e\u6211\u4eec\u5df2\u7ecf\u53ef\u4ee5\u4f7f\u7528\u534f\u7a0b\u4e86\uff0c\u4f46\u662f\u597d\u50cf\u5e76\u6ca1\u6709\u548c\u6b63\u5e38\u7684\u65b9\u5f0f\u6709\u4ec0\u4e48\u51fa\u5165\uff0c\u751a\u81f3\u8fd8\u66f4\u9ebb\u70e6\u4e86\uff0c\u90a3\u4e48\u534f\u7a0b\u5230\u5e95\u5728\u4ec0\u4e48\u573a\u666f\u4e0b\u4f7f\u7528\u5462\uff1f<\/p>\n\n\n<p>\u534f\u7a0b\u5728IO\u5bc6\u96c6\u578b\u4efb\u52a1\u65b9\u9762\u6709\u7740\u5f88\u5927\u7684\u6709\u4f18\u52bf\uff0c\u4e00\u822c\u5305\u62ec\u6587\u4ef6\u8bfb\u5199\uff0c\u7f51\u7edc\u8bf7\u6c42\u7b49\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u5728\u722c\u866b\uff08\u7f51\u7edc\u8bf7\u6c42\uff09\u7684\u573a\u666f\u4e0b\u7ee7\u7eed\u4ecb\u7ecd\u534f\u7a0b\u3002<\/p>\n\n\n<p><\/p>\n\n\n<p>\u8be6\u89c1<a href=\"http:\/\/www.sniper97.cn\/index.php\/note\/carwler\/4032\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"\u3010\u7f51\u7edc\u722c\u866b\u5b9e\u6218\u3011\u5f02\u6b65\u722c\u866b\uff08\u5728\u65b0\u7a97\u53e3\u6253\u5f00\uff09\">\u3010\u7f51\u7edc\u722c\u866b\u5b9e\u6218\u3011\u5f02\u6b65\u722c\u866b<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u90fd\u77e5\u9053\u6709\u7ebf\u7a0b\u548c\u8fdb\u7a0b\uff0c\u7b80\u5355\u8bf4\uff1a\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u6709\u591a\u4e2a\u7ebf\u7a0b\uff0c\u8fdb\u7a0b\u7531\u7cfb\u7edf\u8fdb\u884c\u8c03\u5ea6\uff0c\u800c\u7ebf\u7a0b\u5219\u6709\u8f6f\u4ef6\uff08\u4e5f\u5c31\u662f\u7f16 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[24],"tags":[],"views":2321,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/4033"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=4033"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/4033\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=4033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=4033"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=4033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}