{"id":297885,"date":"2024-05-20T16:39:16","date_gmt":"2024-05-20T08:39:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/297885.html"},"modified":"2024-05-20T16:39:35","modified_gmt":"2024-05-20T08:39:35","slug":"python3-%e7%9a%84%e7%ba%bf%e7%a8%8b%e4%b8%ad%e5%a6%82%e4%bd%95%e8%bf%90%e8%a1%8c%e5%8d%8f%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/297885.html","title":{"rendered":"Python3 \u7684\u7ebf\u7a0b\u4e2d\u5982\u4f55\u8fd0\u884c\u534f\u7a0b"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27145727\/22d4278f-6a4d-4110-8977-6ce5e3e67d31.webp\" alt=\"Python3 \u7684\u7ebf\u7a0b\u4e2d\u5982\u4f55\u8fd0\u884c\u534f\u7a0b\" \/><\/p>\n<p><p><strong>\u5728Python3\u4e2d\uff0c\u7ebf\u7a0b\u4e0e\u534f\u7a0b\u53ef\u4ee5\u76f8\u4e92\u914d\u5408\u8fd0\u884c<\/strong>\uff0c\u4f46\u9700\u8981\u901a\u8fc7\u5408\u9002\u7684\u65b9\u5f0f\u8fdb\u884c\u6574\u5408\u3002\u8981\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u8fd0\u884c\u534f\u7a0b\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>asyncio<\/code>\u5e93\u63d0\u4f9b\u7684\u4e8b\u4ef6\u5faa\u73af\u3002\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a\u521b\u5efa\u4e8b\u4ef6\u5faa\u73af\u3001\u5728\u4e8b\u4ef6\u5faa\u73af\u4e2d\u8fd0\u884c\u534f\u7a0b\u3001\u5173\u95ed\u4e8b\u4ef6\u5faa\u73af\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u4f60\u5728\u57fa\u4e8e\u7ebf\u7a0b\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5229\u7528\u534f\u7a0b\u7684\u975e\u963b\u585eIO\u4f18\u52bf\u3002<\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c<strong>\u521b\u5efa\u4e8b\u4ef6\u5faa\u73af\u662f\u7b2c\u4e00\u6b65\uff0c\u4e5f\u662f\u81f3\u5173\u91cd\u8981\u7684\u4e00\u6b65<\/strong>\u3002\u5728\u4efb\u4f55\u7ebf\u7a0b\u4e2d\u8fd0\u884c\u534f\u7a0b\u4e4b\u524d\uff0c\u4f60\u5fc5\u987b\u5148\u83b7\u53d6\u6216\u521b\u5efa\u4e00\u4e2a\u4e8b\u4ef6\u5faa\u73af\u3002\u5982\u679c\u4f60\u7684\u4ee3\u7801\u8fd0\u884c\u5728\u4e3b\u7ebf\u7a0b\u4e2d\uff0c<code>asyncio.get_event_loop()<\/code>\u4f1a\u81ea\u52a8\u521b\u5efa\u4e8b\u4ef6\u5faa\u73af\u3002\u4f46\u662f\uff0c\u5728\u5b50\u7ebf\u7a0b\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u624b\u52a8\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4e8b\u4ef6\u5faa\u73af\uff0c\u7136\u540e\u5c06\u5176\u8bbe\u7f6e\u4e3a\u5f53\u524d\u7ebf\u7a0b\u7684\u4e8b\u4ef6\u5faa\u73af\uff0c\u4ee5\u4fbf<code>asyncio<\/code>\u4e2d\u7684\u534f\u7a0b\u80fd\u627e\u5230\u5e76\u9644\u52a0\u5230\u8fd9\u4e2a\u5faa\u73af\u4e0a\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u521b\u5efa\u5e76\u8bbe\u7f6e\u4e8b\u4ef6\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u5728\u5b50\u7ebf\u7a0b\u4e2d\u8fd0\u884c\u534f\u7a0b\u4e4b\u524d\uff0c\u5fc5\u987b\u5148\u624b\u52a8\u521b\u5efa\u4e00\u4e2a\u4e8b\u4ef6\u5faa\u73af\uff0c\u5e76\u8bbe\u7f6e\u4e3a\u5f53\u524d\u7ebf\u7a0b\u7684\u4e8b\u4ef6\u5faa\u73af\u3002\u8fd9\u4e00\u6b65\u9aa4\u786e\u4fdd\u534f\u7a0b\u80fd\u5728\u6b63\u786e\u7684\u4e0a\u4e0b\u6587\u4e2d\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>import threading<\/p>\n<p>def run_coroutine_thread():<\/p>\n<p>    loop = asyncio.new_event_loop()<\/p>\n<p>    asyncio.set_event_loop(loop)<\/p>\n<p>    loop.run_until_complete(target_coroutine())<\/p>\n<p>def target_coroutine():<\/p>\n<p>    # \u8fd9\u91cc\u7f16\u5199\u534f\u7a0b\u4ee3\u7801<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>asyncio.new_event_loop()<\/code>\u521b\u5efa\u65b0\u7684\u4e8b\u4ef6\u5faa\u73af\uff0c\u5e76\u901a\u8fc7<code>asyncio.set_event_loop()<\/code>\u8bbe\u7f6e\u4e3a\u5f53\u524d\u7ebf\u7a0b\u7684\u4e8b\u4ef6\u5faa\u73af\u3002\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528<code>loop.run_until_complete()<\/code>\u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\u5e76\u8fd0\u884c\u534f\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5728\u7ebf\u7a0b\u4e2d\u8fd0\u884c\u534f\u7a0b<\/h3>\n<\/p>\n<p><p>\u4e00\u65e6\u8bbe\u7f6e\u597d\u4e8b\u4ef6\u5faa\u73af\uff0c\u63a5\u4e0b\u6765\u5c31\u53ef\u4ee5\u5728\u8fd9\u4e2a\u5faa\u73af\u4e2d\u8fd0\u884c\u534f\u7a0b\u4e86\u3002\u8fd0\u884c\u534f\u7a0b\u7684\u5173\u952e\u662f\u786e\u4fdd\u4e8b\u4ef6\u5faa\u73af\u5728\u5f53\u524d\u7ebf\u7a0b\u4e2d\u6b63\u786e\u542f\u52a8\u5e76\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def start_coroutine_in_thread():<\/p>\n<p>    thread = threading.Thread(target=run_coroutine_thread)<\/p>\n<p>    thread.start()<\/p>\n<p>    thread.join()  # \u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>threading.Thread<\/code>\u521b\u5efa\u4e00\u4e2a\u65b0\u7ebf\u7a0b\uff0c\u5e76\u628a\u4e4b\u524d\u5b9a\u4e49\u7684<code>run_coroutine_thread<\/code>\u51fd\u6570\u4f5c\u4e3a\u76ee\u6807\u51fd\u6570\u3002\u6700\u540e\uff0c\u901a\u8fc7\u8c03\u7528<code>thread.start()<\/code>\u6765\u542f\u52a8\u7ebf\u7a0b\uff0c\u5e76\u901a\u8fc7<code>thread.join()<\/code>\u7b49\u5f85\u7ebf\u7a0b\u8fd0\u884c\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5f02\u6b65\u51fd\u6570\u4e0e\u534f\u7a0b\u7684\u5f02\u6b65IO\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u534f\u7a0b\u4e2d\u8fdb\u884c\u5f02\u6b65IO\u64cd\u4f5c\u662f\u5176\u4e3b\u8981\u7528\u9014\u3002\u5229\u7528<code>async\/aw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t<\/code>\u8bed\u6cd5\u53ef\u4ee5\u7b80\u6d01\u5730\u7f16\u5199\u975e\u963b\u585e\u7684IO\u64cd\u4f5c\u4ee3\u7801\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u54cd\u5e94\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">async def async_io_operation():<\/p>\n<p>    # \u6a21\u62df\u5f02\u6b65IO\u64cd\u4f5c<\/p>\n<p>    await asyncio.sleep(1)<\/p>\n<p>    print(&quot;\u5f02\u6b65IO\u64cd\u4f5c\u5b8c\u6210&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>await asyncio.sleep(1)<\/code>\u6a21\u62df\u5f02\u6b65IO\u64cd\u4f5c\uff0c\u8fd9\u91cc\u7684<code>await<\/code>\u5173\u952e\u5b57\u6682\u505c\u534f\u7a0b\u7684\u6267\u884c\uff0c\u76f4\u5230<code>asyncio.sleep(1)<\/code>\u5b8c\u6210\u3002\u8fd9\u79cd\u975e\u963b\u585e\u7684\u65b9\u5f0f\u4f7f\u5f97\u5176\u4ed6\u534f\u7a0b\u80fd\u5728\u6b64\u671f\u95f4\u8fd0\u884c\uff0c\u4ece\u800c\u63d0\u9ad8\u6574\u4f53\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5c0f\u7ed3\u4e0e\u5b9e\u8df5\u5efa\u8bae<\/h3>\n<\/p>\n<p><p>\u5c06\u534f\u7a0b\u8fd0\u7528\u4e8e\u7ebf\u7a0b\u4e2d\uff0c\u80fd\u591f\u7ed3\u5408\u4e24\u8005\u7684\u4f18\u52bf\uff0c\u65e2\u4fdd\u7559\u4e86\u7ebf\u7a0b\u7684\u5e76\u53d1\u7279\u6027\uff0c\u4e5f\u80fd\u5229\u7528\u534f\u7a0b\u7684\u975e\u963b\u585eIO\u6267\u884c\u80fd\u529b\u3002\u5b9e\u73b0\u8fd9\u4e00\u76ee\u7684\u4e3b\u8981\u4f9d\u9760\u521b\u5efa\u548c\u8bbe\u7f6e\u4e8b\u4ef6\u5faa\u73af\u3001\u5728\u4e8b\u4ef6\u5faa\u73af\u4e2d\u8fd0\u884c\u534f\u7a0b\u3002\u5728\u5b9e\u8df5\u4e2d\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u7ebf\u7a0b\u5b89\u5168\u548c\u5e76\u53d1\u63a7\u5236\uff0c\u907f\u514d\u51fa\u73b0\u8d44\u6e90\u7ade\u4e89\u548c\u6b7b\u9501\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u8fd0\u884c\u534f\u7a0b\u7684\u6807\u51c6\u6a21\u5f0f\u662f\u5229\u7528<code>asyncio<\/code>\u5e93\u521b\u5efa\u4e8b\u4ef6\u5faa\u73af\u3002\u5728\u7f16\u5199\u4f7f\u7528\u534f\u7a0b\u7684\u591a\u7ebf\u7a0b\u5e94\u7528\u65f6\uff0c\u7406\u89e3\u4e8b\u4ef6\u5faa\u73af\u5982\u4f55\u5728\u4e0d\u540c\u7ebf\u7a0b\u4e2d\u521b\u5efa\u548c\u8fd0\u884c\u662f\u5173\u952e\u3002\u6b64\u5916\uff0c\u9002\u5f53\u5730\u7ed3\u5408\u4f7f\u7528<code>async\/await<\/code>\u8bed\u6cd5\u548c\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u5347\u5e94\u7528\u7a0b\u5e8f\u5904\u7406\u5f02\u6b65IO\u4efb\u52a1\u7684\u80fd\u529b\uff0c\u5c24\u5176\u662f\u5728IO\u5bc6\u96c6\u578b\u5e94\u7528\u4e2d\u8868\u73b0\u5c24\u4e3a\u7a81\u51fa\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4ec0\u4e48\u662f\u534f\u7a0b\uff0c\u5b83\u4e0e\u7ebf\u7a0b\u7684\u5173\u7cfb\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>\u534f\u7a0b\u662f\u4e00\u79cd\u7528\u4e8e\u5f02\u6b65\u7f16\u7a0b\u7684\u5e76\u53d1\u673a\u5236\uff0c\u5b83\u53ef\u4ee5\u5728\u4e00\u4e2a\u7ebf\u7a0b\u5185\u5b9e\u73b0\u591a\u4e2a\u4efb\u52a1\u4e4b\u95f4\u7684\u5207\u6362\uff0c\u4ece\u800c\u5b9e\u73b0\u975e\u963b\u585e\u7684\u5e76\u53d1\u3002\u4e0e\u7ebf\u7a0b\u76f8\u6bd4\uff0c\u534f\u7a0b\u907f\u514d\u4e86\u7ebf\u7a0b\u5207\u6362\u7684\u5f00\u9500\uff0c\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u7684\u5e76\u53d1\u65b9\u5f0f\u3002<\/p>\n<p><strong>2. \u5728 Python3 \u7684\u7ebf\u7a0b\u4e2d\u5982\u4f55\u8fd0\u884c\u534f\u7a0b\uff1f<\/strong><\/p>\n<p>\u8981\u5728 Python3 \u7684\u7ebf\u7a0b\u4e2d\u8fd0\u884c\u534f\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>asyncio<\/code>\u5e93\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528<code>asyncio.ensure_future()<\/code>\u51fd\u6570\u5c06\u534f\u7a0b\u5bf9\u8c61\u5305\u88c5\u6210\u4e00\u4e2a<code>Future<\/code>\u5bf9\u8c61\uff0c\u7136\u540e\u5c06\u5176\u6dfb\u52a0\u5230\u4e8b\u4ef6\u5faa\u73af\u4e2d\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5728\u7ebf\u7a0b\u4e2d\u521b\u5efa\u4e00\u4e2a\u4e8b\u4ef6\u5faa\u73af\uff0c\u7136\u540e\u5728\u4e8b\u4ef6\u5faa\u73af\u4e2d\u6267\u884c\u534f\u7a0b\u3002\u6700\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>loop.run_forever()<\/code>\u6216\u8005<code>loop.run_until_complete()<\/code>\u51fd\u6570\u6765\u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u5728\u7ebf\u7a0b\u4e2d\u5904\u7406\u534f\u7a0b\u7684\u8fd4\u56de\u503c\uff1f<\/strong><\/p>\n<p>\u5728\u7ebf\u7a0b\u4e2d\u5904\u7406\u534f\u7a0b\u7684\u8fd4\u56de\u503c\u53ef\u4ee5\u4f7f\u7528<code>asyncio.run_coroutine_thread<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>()<\/code>\u51fd\u6570\u3002\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u534f\u7a0b\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5728\u4e8b\u4ef6\u5faa\u73af\u4e2d\u6267\u884c\u3002\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0c\u5b83\u5c06\u534f\u7a0b\u7684\u8fd4\u56de\u7ed3\u679c\u5c01\u88c5\u6210\u4e00\u4e2a<code>Future<\/code>\u5bf9\u8c61\uff0c\u5e76\u8fd4\u56de\u8be5\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u8c03\u7528<code>result()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u534f\u7a0b\u7684\u8fd4\u56de\u503c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u534f\u7a0b\u8fd8\u672a\u6267\u884c\u5b8c\u6bd5\uff0c<code>result()<\/code>\u65b9\u6cd5\u4f1a\u963b\u585e\u76f4\u5230\u534f\u7a0b\u6267\u884c\u5b8c\u6210\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python3\u4e2d\uff0c\u7ebf\u7a0b\u4e0e\u534f\u7a0b\u53ef\u4ee5\u76f8\u4e92\u914d\u5408\u8fd0\u884c\uff0c\u4f46\u9700\u8981\u901a\u8fc7\u5408\u9002\u7684\u65b9\u5f0f\u8fdb\u884c\u6574\u5408\u3002\u8981\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u8fd0\u884c\u534f\u7a0b\uff0c\u4f60\u53ef\u4ee5\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":297916,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/297885"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=297885"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/297885\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/297916"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=297885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=297885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=297885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}