{"id":1001145,"date":"2024-12-27T09:57:31","date_gmt":"2024-12-27T01:57:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1001145.html"},"modified":"2024-12-27T09:57:34","modified_gmt":"2024-12-27T01:57:34","slug":"python-%e5%8d%8f%e7%a8%8b-%e5%a6%82%e4%bd%95%e8%bf%94%e5%9b%9e","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1001145.html","title":{"rendered":"python \u534f\u7a0b \u5982\u4f55\u8fd4\u56de"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075429\/71972dee-011d-4587-960a-886b035f1873.webp\" alt=\"python \u534f\u7a0b \u5982\u4f55\u8fd4\u56de\" \/><\/p>\n<p><p> <strong>\u5728 Python \u4e2d\uff0c\u534f\u7a0b\u53ef\u4ee5\u901a\u8fc7<code>return<\/code>\u8bed\u53e5\u3001<code>yield<\/code>\u8868\u8fbe\u5f0f\u3001\u4ee5\u53ca<code>asyncio<\/code>\u5e93\u4e2d\u7684<code>async<\/code>\u51fd\u6570\u8fd4\u56de\u7ed3\u679c\u3002\u901a\u8fc7<code>return<\/code>\u8bed\u53e5\uff0c\u534f\u7a0b\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u8fd9\u4e2a\u503c\u4f1a\u88ab\u6355\u83b7\u4e3a<code>StopIteration<\/code>\u5f02\u5e38\u7684\u503c\uff1b\u901a\u8fc7<code>yield<\/code>\u8868\u8fbe\u5f0f\uff0c\u534f\u7a0b\u53ef\u4ee5\u751f\u6210\u591a\u4e2a\u503c\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u5355\u4e00\u7684\u8fd4\u56de\u503c\uff1b\u4f7f\u7528<code>aw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t<\/code>\u8bed\u53e5\uff0c\u53ef\u4ee5\u7b49\u5f85\u53e6\u4e00\u4e2a\u534f\u7a0b\u5b8c\u6210\u5e76\u83b7\u53d6\u5176\u8fd4\u56de\u503c\u3002<\/strong>\u534f\u7a0b\u5728Python\u4e2d\u662f\u4e00\u79cd\u9ad8\u7ea7\u7684\u5e76\u53d1\u7f16\u7a0b\u6280\u672f\uff0c\u7528\u4e8e\u89e3\u51b3\u5f02\u6b65\u7f16\u7a0b\u4e2d\u7684\u8bb8\u591a\u590d\u6742\u95ee\u9898\u3002\u5728\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u4e2d\uff0c\u534f\u7a0b\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u4e00\u79cd\u5904\u7406I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u7684\u9ad8\u6548\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001PYTHON \u534f\u7a0b\u6982\u8ff0<\/p>\n<\/p>\n<p><p>\u534f\u7a0b\u662fPython\u4e2d\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5141\u8bb8\u7a0b\u5e8f\u5728\u7b49\u5f85I\/O\u64cd\u4f5c\u5b8c\u6210\u65f6\u6682\u505c\u6267\u884c\u5e76\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u6062\u590d\u6267\u884c\u3002\u4e0d\u540c\u4e8e\u4f20\u7edf\u7684\u51fd\u6570\u8c03\u7528\uff0c\u534f\u7a0b\u53ef\u4ee5\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u88ab\u6302\u8d77\u548c\u6062\u590d\u3002Python\u4e2d\u7684\u534f\u7a0b\u7531<code>async<\/code>\u548c<code>await<\/code>\u5173\u952e\u5b57\u5f15\u5165\uff0c\u4e3b\u8981\u7528\u4e8e\u5f02\u6b65\u7f16\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u534f\u7a0b\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fd4\u56de\u503c\uff0c\u5176\u4e2d\u6700\u5e38\u89c1\u7684\u5305\u62ec<code>return<\/code>\u8bed\u53e5\u3001<code>yield<\/code>\u8868\u8fbe\u5f0f\u548c<code>await<\/code>\u8bed\u53e5\u3002<code>return<\/code>\u8bed\u53e5\u7528\u4e8e\u8fd4\u56de\u4e00\u4e2a\u5355\u4e00\u7684\u503c\uff1b<code>yield<\/code>\u8868\u8fbe\u5f0f\u7528\u4e8e\u751f\u6210\u591a\u4e2a\u503c\uff0c\u4f7f\u534f\u7a0b\u53ef\u4ee5\u50cf\u751f\u6210\u5668\u4e00\u6837\u5de5\u4f5c\uff1b<code>await<\/code>\u8bed\u53e5\u7528\u4e8e\u7b49\u5f85\u53e6\u4e00\u4e2a\u534f\u7a0b\u5b8c\u6210\u5e76\u8fd4\u56de\u5176\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u534f\u7a0b\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b9a\u4e49\u534f\u7a0b<\/strong><\/li>\n<\/ol>\n<p><p>\u534f\u7a0b\u7684\u5b9a\u4e49\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u5728\u51fd\u6570\u5b9a\u4e49\u524d\u52a0\u4e0a<code>async<\/code>\u5173\u952e\u5b57\u5373\u53ef\u3002\u534f\u7a0b\u5185\u90e8\u53ef\u4ee5\u4f7f\u7528<code>await<\/code>\u5173\u952e\u5b57\u6765\u8c03\u7528\u5176\u4ed6\u534f\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def my_coroutine():<\/p>\n<p>    print(&quot;Hello, this is a coroutine&quot;)<\/p>\n<p>    return 42<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8fd0\u884c\u534f\u7a0b<\/strong><\/li>\n<\/ol>\n<p><p>\u8981\u8fd0\u884c\u4e00\u4e2a\u534f\u7a0b\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528<code>asyncio<\/code>\u5e93\u4e2d\u7684\u4e8b\u4ef6\u5faa\u73af\u3002\u4e8b\u4ef6\u5faa\u73af\u8d1f\u8d23\u8c03\u5ea6\u548c\u8fd0\u884c\u534f\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">async def main():<\/p>\n<p>    result = await my_coroutine()<\/p>\n<p>    print(f&quot;Result of coroutine: {result}&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u4e8b\u4ef6\u5faa\u73af\u5e76\u8fd0\u884c\u4e3b\u534f\u7a0b<\/strong><\/h2>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u534f\u7a0b\u8fd4\u56de\u503c\u7684\u4e0d\u540c\u65b9\u5f0f<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528 <code>return<\/code> \u8bed\u53e5<\/strong><\/li>\n<\/ol>\n<p><p>\u534f\u7a0b\u53ef\u4ee5\u4f7f\u7528<code>return<\/code>\u8bed\u53e5\u8fd4\u56de\u4e00\u4e2a\u5355\u4e00\u7684\u503c\u3002\u5f53\u534f\u7a0b\u6267\u884c\u5b8c\u6bd5\u65f6\uff0c\u8fd4\u56de\u503c\u4f1a\u88ab\u5c01\u88c5\u5728<code>StopIteration<\/code>\u5f02\u5e38\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">async def my_coroutine():<\/p>\n<p>    return &quot;Completed&quot;<\/p>\n<p>async def main():<\/p>\n<p>    result = await my_coroutine()<\/p>\n<p>    print(result)<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528 <code>yield<\/code> \u8868\u8fbe\u5f0f<\/strong><\/li>\n<\/ol>\n<p><p>\u534f\u7a0b\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>yield<\/code>\u8868\u8fbe\u5f0f\u751f\u6210\u591a\u4e2a\u503c\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u534f\u7a0b\u53ef\u4ee5\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u591a\u6b21\u8fd4\u56de\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">async def my_generator():<\/p>\n<p>    yield 1<\/p>\n<p>    yield 2<\/p>\n<p>    yield 3<\/p>\n<p>async def main():<\/p>\n<p>    async for value in my_generator():<\/p>\n<p>        print(value)<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u4f7f\u7528 <code>await<\/code> \u8bed\u53e5<\/strong><\/li>\n<\/ol>\n<p><p><code>await<\/code>\u8bed\u53e5\u7528\u4e8e\u7b49\u5f85\u53e6\u4e00\u4e2a\u534f\u7a0b\u5b8c\u6210\u5e76\u83b7\u53d6\u5176\u8fd4\u56de\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">async def another_coroutine():<\/p>\n<p>    return &quot;Result from another coroutine&quot;<\/p>\n<p>async def main():<\/p>\n<p>    result = await another_coroutine()<\/p>\n<p>    print(result)<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u534f\u7a0b\u7684\u9ad8\u7ea7\u7528\u6cd5<\/p>\n<\/p>\n<ol>\n<li><strong>\u5904\u7406\u5f02\u5e38<\/strong><\/li>\n<\/ol>\n<p><p>\u534f\u7a0b\u4e2d\u53ef\u4ee5\u4f7f\u7528\u5e38\u89c4\u7684<code>try-except<\/code>\u5757\u6765\u5904\u7406\u5f02\u5e38\u3002\u5f53\u534f\u7a0b\u4e2d\u53d1\u751f\u5f02\u5e38\u65f6\uff0c\u4e8b\u4ef6\u5faa\u73af\u4f1a\u6355\u83b7\u8be5\u5f02\u5e38\u5e76\u7ec8\u6b62\u534f\u7a0b\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">async def my_coroutine():<\/p>\n<p>    try:<\/p>\n<p>        raise ValueError(&quot;An error occurred&quot;)<\/p>\n<p>    except ValueError as e:<\/p>\n<p>        print(f&quot;Caught exception: {e}&quot;)<\/p>\n<p>async def main():<\/p>\n<p>    await my_coroutine()<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u534f\u7a0b\u5d4c\u5957<\/strong><\/li>\n<\/ol>\n<p><p>\u534f\u7a0b\u53ef\u4ee5\u5d4c\u5957\u8c03\u7528\u5176\u4ed6\u534f\u7a0b\uff0c\u5e76\u901a\u8fc7<code>await<\/code>\u8bed\u53e5\u7b49\u5f85\u5176\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">async def inner_coroutine():<\/p>\n<p>    return &quot;Inner coroutine result&quot;<\/p>\n<p>async def outer_coroutine():<\/p>\n<p>    result = await inner_coroutine()<\/p>\n<p>    print(result)<\/p>\n<p>async def main():<\/p>\n<p>    await outer_coroutine()<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5e76\u53d1\u6267\u884c\u591a\u4e2a\u534f\u7a0b<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528<code>asyncio.gather<\/code>\u53ef\u4ee5\u5e76\u53d1\u6267\u884c\u591a\u4e2a\u534f\u7a0b\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">async def task1():<\/p>\n<p>    print(&quot;Running task1&quot;)<\/p>\n<p>    return &quot;Task1 result&quot;<\/p>\n<p>async def task2():<\/p>\n<p>    print(&quot;Running task2&quot;)<\/p>\n<p>    return &quot;Task2 result&quot;<\/p>\n<p>async def main():<\/p>\n<p>    results = await asyncio.gather(task1(), task2())<\/p>\n<p>    print(results)<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u534f\u7a0b\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u573a\u666f<\/p>\n<\/p>\n<ol>\n<li><strong>\u7f51\u7edc\u8bf7\u6c42<\/strong><\/li>\n<\/ol>\n<p><p>\u534f\u7a0b\u975e\u5e38\u9002\u5408\u7528\u4e8e\u5904\u7406\u5927\u91cf\u7684\u7f51\u7edc\u8bf7\u6c42\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ef\u4ee5\u5728\u7b49\u5f85I\/O\u64cd\u4f5c\u65f6\u91ca\u653e\u7ebf\u7a0b\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6587\u4ef6\u5904\u7406<\/strong><\/li>\n<\/ol>\n<p><p>\u534f\u7a0b\u53ef\u4ee5\u7528\u4e8e\u5f02\u6b65\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\uff0c\u4ece\u800c\u907f\u514d\u963b\u585eI\/O\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5b9e\u65f6\u6570\u636e\u5904\u7406<\/strong><\/li>\n<\/ol>\n<p><p>\u534f\u7a0b\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u5b9e\u65f6\u6570\u636e\u6d41\uff0c\u5982WebSocket\u8fde\u63a5\u6216\u5b9e\u65f6\u4f20\u611f\u5668\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u534f\u7a0b\u7684\u6027\u80fd\u4f18\u5316<\/p>\n<\/p>\n<ol>\n<li><strong>\u51cf\u5c11\u4e0a\u4e0b\u6587\u5207\u6362<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u8bbe\u8ba1\u534f\u7a0b\u65f6\uff0c\u5e94\u5c3d\u91cf\u51cf\u5c11\u4e0a\u4e0b\u6587\u5207\u6362\u7684\u6b21\u6570\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528\u9002\u5f53\u7684\u6570\u636e\u7ed3\u6784<\/strong><\/li>\n<\/ol>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u534f\u7a0b\u7684\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u96c6\u5408\u800c\u4e0d\u662f\u5217\u8868\u6765\u68c0\u67e5\u5143\u7d20\u7684\u5b58\u5728\u6027\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u907f\u514d\u5168\u5c40\u53d8\u91cf<\/strong><\/li>\n<\/ol>\n<p><p>\u5168\u5c40\u53d8\u91cf\u4f1a\u5bfc\u81f4\u534f\u7a0b\u4e4b\u95f4\u7684\u7ade\u4e89\uff0c\u4ece\u800c\u964d\u4f4e\u6027\u80fd\u3002\u5e94\u5c3d\u91cf\u907f\u514d\u5728\u534f\u7a0b\u4e2d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u534f\u7a0b\u7684\u8c03\u8bd5\u4e0e\u6d4b\u8bd5<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u534f\u7a0b\u4e2d\u6dfb\u52a0\u65e5\u5fd7\u8bb0\u5f55\u6709\u52a9\u4e8e\u8c03\u8bd5\u548c\u8ddf\u8e2a\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5355\u5143\u6d4b\u8bd5<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>pytest<\/code>\u7b49\u5de5\u5177\u4e3a\u534f\u7a0b\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u5176\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u534f\u7a0b\u7684\u672a\u6765\u53d1\u5c55<\/p>\n<\/p>\n<p><p>\u968f\u7740Python\u7684\u53d1\u5c55\uff0c\u534f\u7a0b\u5728\u672a\u6765\u53ef\u80fd\u4f1a\u5f15\u5165\u66f4\u591a\u7684\u7279\u6027\u548c\u6539\u8fdb\uff0c\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u5176\u5728\u5e76\u53d1\u7f16\u7a0b\u4e2d\u7684\u5e94\u7528\u3002\u5f00\u53d1\u8005\u5e94\u6301\u7eed\u5173\u6ce8Python\u7684\u66f4\u65b0\uff0c\u4ee5\u4fbf\u5145\u5206\u5229\u7528\u534f\u7a0b\u7684\u4f18\u52bf\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c\u534f\u7a0b\u662fPython\u4e2d\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u5904\u7406\u5f02\u6b65\u4efb\u52a1\u7684\u9ad8\u6548\u65b9\u6cd5\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u534f\u7a0b\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u534f\u7a0b\u4e2d\u8fd4\u56de\u503c\uff1f<\/strong><br \/>\u5728Python\u534f\u7a0b\u4e2d\uff0c\u4f7f\u7528<code>async def<\/code>\u5b9a\u4e49\u7684\u51fd\u6570\u53ef\u4ee5\u4f7f\u7528<code>return<\/code>\u8bed\u53e5\u8fd4\u56de\u503c\u3002\u5f53\u534f\u7a0b\u88ab\u6267\u884c\u65f6\uff0c\u8fd4\u56de\u7684\u503c\u4f1a\u88ab\u5c01\u88c5\u5728<code>await<\/code>\u8868\u8fbe\u5f0f\u4e2d\u3002\u5982\u679c\u9700\u8981\u5728\u5f02\u6b65\u4e0a\u4e0b\u6587\u4e2d\u83b7\u53d6\u8fd4\u56de\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>await<\/code>\u6765\u8c03\u7528\u534f\u7a0b\u5e76\u83b7\u53d6\u7ed3\u679c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">async def my_coroutine():\n    return &quot;Hello, World!&quot;\n\nresult = await my_coroutine()\nprint(result)  # \u8f93\u51fa: Hello, World!\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528<code>asyncio<\/code>\u6a21\u5757\u5982\u4f55\u7ba1\u7406\u534f\u7a0b\u7684\u8fd4\u56de\u7ed3\u679c\uff1f<\/strong><br \/><code>asyncio<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u4e2a\u5de5\u5177\u6765\u7ba1\u7406\u534f\u7a0b\u7684\u6267\u884c\u548c\u7ed3\u679c\u3002\u53ef\u4ee5\u4f7f\u7528<code>asyncio.create_task()<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u4efb\u52a1\uff0c\u5e76\u4f7f\u7528<code>await<\/code>\u6765\u83b7\u53d6\u5176\u8fd4\u56de\u503c\u3002\u5982\u4e0b\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import asyncio\n\nasync def my_coroutine():\n    return &quot;Hello, asyncio!&quot;\n\nasync def main():\n    task = asyncio.create_task(my_coroutine())\n    result = await task\n    print(result)  # \u8f93\u51fa: Hello, asyncio!\n\nasyncio.run(main())\n<\/code><\/pre>\n<p><strong>\u534f\u7a0b\u8fd4\u56de\u503c\u4e0e\u751f\u6210\u5668\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u534f\u7a0b\u548c\u751f\u6210\u5668\u5728\u8fd4\u56de\u503c\u65b9\u9762\u6709\u663e\u8457\u7684\u4e0d\u540c\u3002\u534f\u7a0b\u4f7f\u7528<code>return<\/code>\u8bed\u53e5\u8fd4\u56de\u503c\uff0c\u800c\u751f\u6210\u5668\u4f7f\u7528<code>yield<\/code>\u8bed\u53e5\u3002\u5728\u751f\u6210\u5668\u4e2d\uff0c\u503c\u901a\u8fc7<code>yield<\/code>\u8868\u8fbe\u5f0f\u9010\u6b65\u8fd4\u56de\uff0c\u53ef\u4ee5\u5728\u8fed\u4ee3\u8fc7\u7a0b\u4e2d\u591a\u6b21\u8fd4\u56de\uff0c\u800c\u534f\u7a0b\u5219\u662f\u4e00\u6b21\u6027\u8fd4\u56de\u7ed3\u679c\u3002\u534f\u7a0b\u7684\u4e3b\u8981\u76ee\u7684\u662f\u5f02\u6b65\u7f16\u7a0b\uff0c\u800c\u751f\u6210\u5668\u5219\u4e3b\u8981\u7528\u4e8e\u8fed\u4ee3\u548c\u751f\u6210\u5e8f\u5217\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728 Python \u4e2d\uff0c\u534f\u7a0b\u53ef\u4ee5\u901a\u8fc7return\u8bed\u53e5\u3001yield\u8868\u8fbe\u5f0f\u3001\u4ee5\u53caasyncio\u5e93\u4e2d\u7684async\u51fd\u6570\u8fd4 [&hellip;]","protected":false},"author":3,"featured_media":1001158,"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\/1001145"}],"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=1001145"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001145\/revisions"}],"predecessor-version":[{"id":1001163,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001145\/revisions\/1001163"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1001158"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1001145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1001145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1001145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}