{"id":23449,"date":"2024-11-21T11:18:07","date_gmt":"2024-11-21T03:18:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23449\/"},"modified":"2024-11-21T11:18:07","modified_gmt":"2024-11-21T03:18:07","slug":"swoole%e4%b8%ad%e5%a6%82%e4%bd%95%e9%ab%98%e6%95%88%e4%bd%bf%e7%94%a8%e5%8d%8f%e7%a8%8b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23449\/","title":{"rendered":"Swoole\u4e2d\u5982\u4f55\u9ad8\u6548\u4f7f\u7528\u534f\u7a0b\uff1f"},"content":{"rendered":"<p>\u4e2d\u5982\u4f55\u9ad8\u6548\u4f7f\u7528\u534f\u7a0b\uff1f<\/p>\n<p>\u534f\u7a0b\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u8fdb\u7a0b\u5185\u5e76\u53d1\u6267\u884c\u5927\u91cf\u7684\u4efb\u52a1\u3002Swoole\u4f5c\u4e3a\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u5bf9\u534f\u7a0b\u63d0\u4f9b\u4e86\u652f\u6301\u3002Swoole\u7684\u534f\u7a0b\u4e0d\u4ec5\u4ec5\u662f\u7b80\u5355\u7684\u534f\u7a0b\u8c03\u5ea6\u5668\uff0c\u8fd8\u63d0\u4f9b\u4e86\u5f88\u591a\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5982\u534f\u7a0b\u6c60\u3001\u534f\u7a0b\u539f\u5b50\u64cd\u4f5c\uff0c\u4ee5\u53ca\u5404\u79cd\u7f51\u7edc\u7f16\u7a0b\u76f8\u5173\u7684\u534f\u7a0b\u5c01\u88c5\u7b49\u7b49\uff0c\u8fd9\u4e9b\u529f\u80fd\u90fd\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u5f00\u53d1\u7f51\u7edc\u5e94\u7528\u3002<\/p>\n<p>\u5728Swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u6709\u5f88\u591a\u597d\u5904\uff0c\u9996\u5148\u662f\u5b83\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u80fd\u3002\u5728\u4f20\u7edf\u7684PHP\u5e94\u7528\u4e2d\uff0c\u6bcf\u4e2a\u8fde\u63a5\u90fd\u9700\u8981\u4e00\u4e2a\u8fdb\u7a0b\u6765\u5904\u7406\uff0c\u8fd9\u6837\u5f88\u5bb9\u6613\u5bfc\u81f4\u8fdb\u7a0b\u6570\u8fc7\u591a\uff0c\u8d44\u6e90\u5360\u7528\u8fc7\u591a\u3002\u800c\u5728Swoole\u4e2d\uff0c\u534f\u7a0b\u53ef\u4ee5\u8ba9\u6211\u4eec\u5904\u7406\u66f4\u591a\u7684\u8fde\u63a5\u6570\uff0c\u4ece\u800c\u63d0\u5347\u5e94\u7528\u7684\u5e76\u53d1\u6027\u80fd\u3002\u6b64\u5916\uff0cSwoole\u7684\u534f\u7a0b\u652f\u6301\u5f02\u6b65\u975e\u963b\u585e\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8ba9\u6211\u4eec\u66f4\u597d\u5730\u5229\u7528CPU\u8d44\u6e90\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u6548\u7387\u3002<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u770b\u770bSwoole\u4e2d\u5982\u4f55\u9ad8\u6548\u5730\u4f7f\u7528\u534f\u7a0b\u3002<\/p>\n<p>\u4e00\u3001\u534f\u7a0b\u7684\u521b\u5efa\u548c\u4f7f\u7528<\/p>\n<p>\u5728Swoole\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7swoole_coroutine_create\u51fd\u6570\u521b\u5efa\u534f\u7a0b\uff0c\u7136\u540e\u5728\u91cc\u9762\u6267\u884c\u4e00\u4e9b\u8017\u65f6\u64cd\u4f5c\u3002\u6bd4\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u534f\u7a0b\u793a\u4f8b\uff1a<\/p>\n<pre>&lt;?php go(function () {\n    for ($i = 0; $i &lt; 5; $i++) {\n        echo \"\u534f\u7a0b\u5185\u90e8\u64cd\u4f5c $i\n\";\n        sleep(1);\n    }\n});\necho \"\u4e3b\u7ebf\u7a0b\u64cd\u4f5c\n\";<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528go\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u533f\u540d\u534f\u7a0b\uff0c\u7136\u540e\u5728\u534f\u7a0b\u4e2d\u5faa\u73af\u8f93\u51fa\u4e00\u4e9b\u4fe1\u606f\u3002\u53ef\u4ee5\u770b\u5230\uff0c\u5728\u4e3b\u7ebf\u7a0b\u8f93\u51fa\u4fe1\u606f\u7684\u540c\u65f6\uff0c\u534f\u7a0b\u4e5f\u5728\u6267\u884c\u81ea\u5df1\u7684\u4efb\u52a1\u3002<\/p>\n<p>\u5728\u534f\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528swoole_coroutine_yield\u51fd\u6570\u6765\u8ba9\u51fa\u5f53\u524d\u534f\u7a0b\u7684\u6267\u884c\u6743\uff0c\u8ba9\u5176\u4ed6\u534f\u7a0b\u6216\u8005\u4e3b\u7ebf\u7a0b\u6765\u6267\u884c\u3002\u6bd4\u5982\uff0c\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\u6f14\u793a\u4e86\u5982\u4f55\u5728\u534f\u7a0b\u4e2d\u4f7f\u7528swoole_coroutine_yield\u51fd\u6570\uff1a<\/p>\n<pre>&lt;?php go(function () {\n    for ($i = 0; $i &lt; 5; $i++) {\n        echo \"\u534f\u7a0b\u5185\u90e8\u64cd\u4f5c $i\n\";\n        swoole_coroutine_yield();\n    }\n});\necho \"\u4e3b\u7ebf\u7a0b\u64cd\u4f5c\n\";<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728\u6bcf\u6b21\u5faa\u73af\u7ed3\u675f\u65f6\u8c03\u7528\u4e86swoole_coroutine_yield\u51fd\u6570\uff0c\u8ba9\u51fa\u4e86\u5f53\u524d\u534f\u7a0b\u7684\u6267\u884c\u6743\u3002\u8fd9\u6837\uff0c\u5176\u4ed6\u534f\u7a0b\u548c\u4e3b\u7ebf\u7a0b\u5c31\u53ef\u4ee5\u6709\u673a\u4f1a\u7ee7\u7eed\u6267\u884c\uff0c\u800c\u4e0d\u81f3\u4e8e\u4e00\u76f4\u88ab\u8be5\u534f\u7a0b\u5360\u7528\u3002<\/p>\n<p>\u4e8c\u3001\u534f\u7a0b\u8c03\u5ea6\u5668\u548c\u534f\u7a0b\u6c60<\/p>\n<p>Swoole\u4e2d\u7684\u534f\u7a0b\u8c03\u5ea6\u5668\u53ef\u4ee5\u6309\u7167\u4e00\u5b9a\u7684\u89c4\u5219\u8c03\u5ea6\u591a\u4e2a\u534f\u7a0b\uff0c\u8ba9\u5b83\u4eec\u4e92\u76f8\u5207\u6362\u6267\u884c\uff0c\u4ece\u800c\u8fbe\u5230\u5e76\u53d1\u7684\u6548\u679c\u3002\u5728\u7f16\u5199\u534f\u7a0b\u4ee3\u7801\u65f6\uff0c\u6211\u4eec\u65e0\u9700\u624b\u52a8\u7ba1\u7406\u534f\u7a0b\u7684\u6267\u884c\u987a\u5e8f\uff0c\u8c03\u5ea6\u5668\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u8fd9\u4e9b\u5de5\u4f5c\u3002<\/p>\n<p>\u534f\u7a0b\u6c60\u662fSwoole\u7684\u53e6\u4e00\u4e2a\u9ad8\u7ea7\u529f\u80fd\uff0c\u5b83\u5c06\u591a\u4e2a\u534f\u7a0b\u7ed1\u5b9a\u5230\u4e00\u4e2a\u6c60\u4e2d\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8fdb\u884c\u534f\u7a0b\u8c03\u5ea6\u7ba1\u7406\u3002\u4f7f\u7528\u534f\u7a0b\u6c60\u53ef\u4ee5\u907f\u514d\u9891\u7e41\u5730\u521b\u5efa\u548c\u9500\u6bc1\u534f\u7a0b\uff0c\u4ece\u800c\u63d0\u5347\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u7528\u534f\u7a0b\u6c60\u5b9e\u73b0\u7684\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>&lt;?php $pool = new SwooleCoroutineChannel(10);\nfor ($i = 0; $i &lt; 10; $i++) {\n    go(function () use ($i, $pool) {\n        echo \"\u534f\u7a0b$i\u6267\u884c\n\";\n        $pool-&gt;push($i);\n    });\n}\nfor ($i = 0; $i pop();\n    echo \"\u6536\u5230\u6570\u636e $data\n\";\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Swoole\u7684Channel\u4f5c\u4e3a\u534f\u7a0b\u6c60\uff0c\u521b\u5efa\u4e8610\u4e2a\u534f\u7a0b\u5e76\u6267\u884c\u5b83\u4eec\u3002\u6bcf\u4e2a\u534f\u7a0b\u5728\u6267\u884c\u5b8c\u6210\u540e\uff0c\u5c06\u81ea\u5df1\u7684ID\u63a8\u5165\u534f\u7a0b\u6c60\u4e2d\u3002\u5728\u4e3b\u7ebf\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u5faa\u73af\u6765\u4ece\u534f\u7a0b\u6c60\u4e2d\u53d6\u51fa\u6570\u636e\uff0c\u6700\u7ec8\u8f93\u51fa\u6bcf\u4e2a\u534f\u7a0b\u7684ID\u3002<\/p>\n<p>\u4e09\u3001\u534f\u7a0b\u4e0e\u7f51\u7edc\u7f16\u7a0b<\/p>\n<p>Swoole\u4e0d\u4ec5\u63d0\u4f9b\u4e86\u534f\u7a0b\u6c60\u7b49\u4f18\u79c0\u529f\u80fd\uff0c\u8fd8\u5c01\u88c5\u4e86\u4e00\u4e9b\u7f51\u7edc\u7f16\u7a0b\u76f8\u5173\u7684\u534f\u7a0b\uff0c\u65b9\u4fbf\u6211\u4eec\u8fdb\u884c\u7f51\u7edc\u7f16\u7a0b\u3002\u5728\u4f7f\u7528\u8fd9\u4e9b\u534f\u7a0b\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4eab\u53d7\u5230\u9ad8\u6548\u7684\u975e\u963b\u585eI\/O\u64cd\u4f5c\u548c\u5f3a\u5927\u7684\u5f02\u6b65\u7f16\u7a0b\u80fd\u529b\u3002<\/p>\n<p>\u6bd4\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Swoole\u534f\u7a0b\u5e93\u7684HTTP\u670d\u52a1\u5668\u793a\u4f8b\uff1a<\/p>\n<pre>&lt;?php $http = new SwooleHttpServer(\"0.0.0.0\", 9501);\n$http-&gt;on(\"request\", function ($request, $response) {\n    $response-&amp;gt;header(\"Content-Type\", \"text\/plain\");\n    $response-&amp;gt;end(\"Hello World\n\");\n});\n$http-&amp;gt;start();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Swoole\u7684HTTP\u670d\u52a1\u5668\u7ec4\u4ef6\uff0c\u5e76\u5728\u8bf7\u6c42\u4e8b\u4ef6\u4e2d\u8f93\u51fa\u4e86\u201cHello World\u201d\u5b57\u7b26\u4e32\u3002\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0cHTTP\u670d\u52a1\u5668\u4f1a\u81ea\u52a8\u521b\u5efa\u591a\u4e2a\u534f\u7a0b\u6765\u54cd\u5e94\u5ba2\u6237\u7aef\u8bf7\u6c42\uff0c\u8fd9\u4e9b\u534f\u7a0b\u53ef\u4ee5\u9ad8\u6548\u5730\u6267\u884cI\/O\u64cd\u4f5c\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u670d\u52a1\u5668\u7684\u5e76\u53d1\u6027\u80fd\u3002<\/p>\n<p>\u56db\u3001\u534f\u7a0b\u539f\u5b50\u64cd\u4f5c<\/p>\n<p>\u9664\u4e86\u4e0a\u8ff0\u529f\u80fd\u4e4b\u5916\uff0cSwoole\u7684\u534f\u7a0b\u8fd8\u63d0\u4f9b\u4e86\u534f\u7a0b\u539f\u5b50\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5728\u534f\u7a0b\u4e4b\u95f4\u5b9e\u73b0\u539f\u5b50\u6027\u64cd\u4f5c\uff0c\u4ece\u800c\u907f\u514d\u7ade\u6001\u548c\u9501\u7b49\u95ee\u9898\u3002<\/p>\n<p>\u6bd4\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u534f\u7a0b\u539f\u5b50\u64cd\u4f5c\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>&lt;?php $count = new SwooleAtomic(0);\nfor ($i = 0; $i &lt; 10; $i++) {\n    go(function () use ($count) {\n        for ($j = 0; $j &lt; 1000; $j++) {\n            $count-&gt;add(1);\n        }\n    });\n}\nswoole_event_wait();\necho \"count=$count\n\";<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Swoole\u7684Atomic\u7c7b\u6765\u5b9e\u73b0\u539f\u5b50\u6027\u64cd\u4f5c\uff0c\u907f\u514d\u4e8610\u4e2a\u534f\u7a0b\u4e4b\u95f4\u7684\u7ade\u6001\u95ee\u9898\u3002\u6700\u7ec8\u8f93\u51fa\u7ed3\u679c\u662fcount=10000\uff0c\u8fd9\u8bc1\u660e\u4e86\u534f\u7a0b\u539f\u5b50\u64cd\u4f5c\u7684\u53ef\u9760\u6027\u548c\u9ad8\u6548\u6027\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86Swoole\u4e2d\u534f\u7a0b\u7684\u4f7f\u7528\u548c\u4f18\u52bf\uff0c\u5305\u62ec\u534f\u7a0b\u7684\u521b\u5efa\u548c\u8c03\u5ea6\u3001\u534f\u7a0b\u6c60\u3001\u534f\u7a0b\u4e0e\u7f51\u7edc\u7f16\u7a0b\u3001\u534f\u7a0b\u539f\u5b50\u64cd\u4f5c\u7b49\u3002Swoole\u7684\u534f\u7a0b\u529f\u80fd\u975e\u5e38\u5f3a\u5927\uff0c\u53ef\u4ee5\u5f88\u597d\u5730\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u6548\u7387\u3002\u5728\u7f16\u5199Swoole\u5e94\u7528\u65f6\uff0c\u6211\u4eec\u5e94\u8be5\u5145\u5206\u5229\u7528\u534f\u7a0b\u76f8\u5173\u7684\u529f\u80fd\uff0c\u4ee5\u6b64\u6765\u4f18\u5316\u7a0b\u5e8f\u7684\u8fd0\u884c\u6548\u679c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSwoole\u4e2d\u5982\u4f55\u9ad8\u6548\u4f7f\u7528\u534f\u7a0b\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e2d\u5982\u4f55\u9ad8\u6548\u4f7f\u7528\u534f\u7a0b\uff1f \u534f\u7a0b\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u8fdb\u7a0b\u5185\u5e76\u53d1\u6267\u884c\u5927\u91cf\u7684\u4efb\u52a1\u3002Swoole\u4f5c\u4e3a\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u5bf9\u534f\u7a0b\u63d0\u4f9b\u4e86\u652f\u6301\u3002Swoole\u7684\u534f\u7a0b\u4e0d\u4ec5\u4ec5\u662f\u7b80\u5355\u7684\u534f\u7a0b\u8c03\u5ea6\u5668\uff0c\u8fd8\u63d0\u4f9b\u4e86\u5f88\u591a\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5982\u534f\u7a0b\u6c60\u3001\u534f\u7a0b\u539f\u5b50\u64cd\u4f5c\uff0c\u4ee5\u53ca\u5404\u79cd\u7f51\u7edc\u7f16\u7a0b\u76f8\u5173\u7684\u534f\u7a0b\u5c01\u88c5\u7b49\u7b49\uff0c\u8fd9\u4e9b\u529f\u80fd\u90fd\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u5f00\u53d1\u7f51\u7edc\u5e94\u7528\u3002 \u5728Swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u6709\u5f88\u591a\u597d\u5904\uff0c\u9996\u5148\u662f\u5b83\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u80fd\u3002\u5728\u4f20\u7edf\u7684PHP\u5e94\u7528\u4e2d\uff0c\u6bcf\u4e2a\u8fde\u63a5\u90fd\u9700\u8981\u4e00\u4e2a\u8fdb\u7a0b\u6765\u5904\u7406\uff0c\u8fd9\u6837\u5f88\u5bb9\u6613\u5bfc\u81f4\u8fdb\u7a0b\u6570\u8fc7\u591a\uff0c\u8d44\u6e90\u5360\u7528\u8fc7\u591a\u3002\u800c\u5728Swoole\u4e2d\uff0c\u534f\u7a0b\u53ef\u4ee5\u8ba9\u6211\u4eec\u5904\u7406\u66f4\u591a\u7684\u8fde\u63a5\u6570\uff0c\u4ece\u800c\u63d0\u5347\u5e94\u7528\u7684\u5e76\u53d1\u6027\u80fd\u3002\u6b64\u5916\uff0cSwoole\u7684\u534f\u7a0b\u652f\u6301\u5f02\u6b65\u975e\u963b\u585e\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8ba9\u6211\u4eec\u66f4\u597d\u5730\u5229\u7528CPU\u8d44\u6e90\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u6548\u7387\u3002 \u4e0b\u9762\u6211\u4eec\u6765\u770b\u770bSwoole\u4e2d\u5982\u4f55\u9ad8\u6548\u5730\u4f7f\u7528\u534f\u7a0b\u3002 \u4e00\u3001\u534f\u7a0b\u7684\u521b\u5efa\u548c\u4f7f\u7528 \u5728Swoole\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7swoole_coroutine_create\u51fd\u6570\u521b\u5efa\u534f\u7a0b\uff0c\u7136\u540e\u5728\u91cc\u9762\u6267\u884c\u4e00\u4e9b\u8017\u65f6\u64cd\u4f5c\u3002\u6bd4\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u534f\u7a0b\u793a\u4f8b\uff1a &lt;?php go(function () { for ($i = 0; $i &lt; 5; $i++) { echo &#8220;\u534f\u7a0b\u5185\u90e8\u64cd\u4f5c $i &#8220;; sleep(1); } }); echo &#8220;\u4e3b\u7ebf\u7a0b\u64cd\u4f5c &#8220;; \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528go\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u533f\u540d\u534f\u7a0b\uff0c\u7136\u540e\u5728\u534f\u7a0b\u4e2d\u5faa\u73af\u8f93\u51fa\u4e00\u4e9b\u4fe1\u606f\u3002\u53ef\u4ee5\u770b\u5230\uff0c\u5728\u4e3b\u7ebf\u7a0b\u8f93\u51fa\u4fe1\u606f\u7684\u540c\u65f6\uff0c\u534f\u7a0b\u4e5f\u5728\u6267\u884c\u81ea\u5df1\u7684\u4efb\u52a1\u3002 \u5728\u534f\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528swoole_coroutine_yield\u51fd\u6570\u6765\u8ba9\u51fa\u5f53\u524d\u534f\u7a0b\u7684\u6267\u884c\u6743\uff0c\u8ba9\u5176\u4ed6\u534f\u7a0b\u6216\u8005\u4e3b\u7ebf\u7a0b\u6765\u6267\u884c\u3002\u6bd4\u5982\uff0c\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\u6f14\u793a\u4e86\u5982\u4f55\u5728\u534f\u7a0b\u4e2d\u4f7f\u7528swoole_coroutine_yield\u51fd\u6570\uff1a &lt;?php go(function () { for ($i = 0; $i &lt; 5; $i++) { echo &#8220;\u534f\u7a0b\u5185\u90e8\u64cd\u4f5c $i &#8220;; swoole_coroutine_yield(); } }); echo &#8220;\u4e3b\u7ebf\u7a0b\u64cd\u4f5c &#8220;; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-23449","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=23449"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23449\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}