{"id":23321,"date":"2024-11-21T16:04:55","date_gmt":"2024-11-21T08:04:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23321\/"},"modified":"2024-11-21T16:04:55","modified_gmt":"2024-11-21T08:04:55","slug":"%e6%8e%a2%e7%a9%b6swoole%e4%b8%ad%e5%8d%8f%e7%a8%8b%e7%9a%84%e8%bf%90%e8%a1%8c%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23321\/","title":{"rendered":"\u63a2\u7a76Swoole\u4e2d\u534f\u7a0b\u7684\u8fd0\u884c\u673a\u5236"},"content":{"rendered":"<p>\u662f\u4e00\u4e2a\u57fa\u4e8ephp\u7684\u534f\u7a0b\u6846\u67b6\uff0c\u5b83\u7684\u5f02\u6b65io\u6027\u80fd\u975e\u5e38\u51fa\u8272\u3002swoole\u7684\u6838\u5fc3\u662f\u534f\u7a0b\uff0c\u534f\u7a0b\u662f\u4e00\u79cd\u6bd4\u7ebf\u7a0b\u66f4\u8f7b\u91cf\u7ea7\u7684\u5e76\u53d1\u673a\u5236\uff0c\u53ef\u4ee5\u5728\u540c\u4e00\u7ebf\u7a0b\u4e2d\u5207\u6362\u4efb\u52a1\u6765\u5b9e\u73b0\u5e76\u53d1\u6267\u884c\u3002\u672c\u6587\u5c06\u4f1a\u63a2\u7a76swoole\u4e2d\u534f\u7a0b\u7684\u8fd0\u884c\u673a\u5236\u3002<\/p>\n<p>\u4e00\u3001\u534f\u7a0b\u7684\u6982\u5ff5<\/p>\n<p>\u534f\u7a0b\uff0c\u53c8\u79f0\u5fae\u7ebf\u7a0b\uff0c\u662f\u4e00\u79cd\u6bd4\u7ebf\u7a0b\u66f4\u7ec6\u7c92\u5ea6\u7684\u5e76\u53d1\u673a\u5236\u3002\u534f\u7a0b\u4e0e\u7ebf\u7a0b\u7684\u533a\u522b\u5728\u4e8e\uff0c\u534f\u7a0b\u901a\u8fc7\u65f6\u95f4\u7247\u8f6e\u8f6c\u6765\u5b9e\u73b0\u4efb\u52a1\u5207\u6362\uff0c\u800c\u7ebf\u7a0b\u7531\u64cd\u4f5c\u7cfb\u7edf\u8c03\u5ea6\u5668\u8d1f\u8d23\u5207\u6362\u3002\u56e0\u6b64\uff0c\u534f\u7a0b\u5728\u6027\u80fd\u4e0a\u6bd4\u7ebf\u7a0b\u66f4\u52a0\u51fa\u8272\u3002<\/p>\n<p>\u5728Swoole\u4e2d\uff0c\u534f\u7a0b\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684PHP\u7ebf\u7a0b\u3002\u534f\u7a0b\u53ef\u4ee5\u5728\u540c\u4e00\u7ebf\u7a0b\u4e2d\u5207\u6362\u6267\u884c\u4e0d\u540c\u7684\u4efb\u52a1\uff0c\u5b9e\u73b0\u5e76\u53d1\u6267\u884c\u3002\u76f8\u6bd4\u4e8e\u4f20\u7edf\u7684\u7ebf\u7a0b\u6c60\u6a21\u5f0f\uff0c\u534f\u7a0b\u53ef\u4ee5\u907f\u514d\u7ebf\u7a0b\u4e0a\u4e0b\u6587\u5207\u6362\u7684\u5f00\u9500\uff0c\u540c\u65f6\u534f\u7a0b\u5177\u6709\u66f4\u4f4e\u7684\u5185\u5b58\u5360\u7528\u548c\u66f4\u9ad8\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<p>\u4e8c\u3001Swoole\u7684\u534f\u7a0b\u5b9e\u73b0<\/p>\n<p>Swoole\u901a\u8fc7\u534f\u7a0b\u8c03\u5ea6\u5668\u6765\u5b9e\u73b0\u534f\u7a0b\u7684\u8c03\u5ea6\u548c\u5207\u6362\uff0c\u534f\u7a0b\u8c03\u5ea6\u5668\u662fSwoole\u63d0\u4f9b\u7684\u4e00\u79cd\u534f\u7a0b\u8c03\u5ea6\u5f15\u64ce\uff0c\u5b83\u53ef\u4ee5\u57fa\u4e8e\u65f6\u95f4\u7247\u8f6e\u8f6c\u7684\u65b9\u5f0f\u6765\u5207\u6362\u534f\u7a0b\u6267\u884c\u4efb\u52a1\u3002<\/p>\n<p>\u534f\u7a0b\u8c03\u5ea6\u5668\u7684\u5b9e\u73b0\u539f\u7406\u5982\u4e0b\uff1a<\/p>\n<p>1.\u9996\u5148\uff0c\u8c03\u5ea6\u5668\u4f1a\u4e3a\u6bcf\u4e2a\u534f\u7a0b\u5206\u914d\u4e00\u4e2a\u72b6\u6001\uff0c\u4f8b\u5982\u7b49\u5f85\u3001\u6267\u884c\u3001\u4f11\u7720\u7b49\u3002<\/p>\n<p>2.\u8c03\u5ea6\u5668\u4f1a\u901a\u8fc7\u4e00\u4e2a\u4efb\u52a1\u961f\u5217\u6765\u7ba1\u7406\u6240\u6709\u534f\u7a0b\u7684\u72b6\u6001\u3002\u5f53\u5f53\u524d\u534f\u7a0b\u6267\u884c\u5b8c\u6210\u540e\uff0c\u8c03\u5ea6\u5668\u4f1a\u4f18\u5148\u9009\u62e9\u72b6\u6001\u4e3a\u7b49\u5f85\u7684\u534f\u7a0b\u6765\u6267\u884c\u3002<\/p>\n<p>3.\u534f\u7a0b\u4f1a\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u81ea\u52a8\u5224\u65ad\u5f53\u524d\u4efb\u52a1\u662f\u5426\u5b8c\u6210\u3002\u5982\u679c\u5f53\u524d\u4efb\u52a1\u672a\u5b8c\u6210\uff0c\u534f\u7a0b\u4f1a\u5c06\u5176\u6302\u8d77\uff0c\u7136\u540e\u5207\u6362\u5230\u5176\u4ed6\u534f\u7a0b\u6267\u884c\uff0c\u76f4\u5230\u4e0b\u4e00\u6b21\u8c03\u5ea6\u65f6\u518d\u7ee7\u7eed\u6267\u884c\u6302\u8d77\u4efb\u52a1\u3002<\/p>\n<p>4.\u5728\u534f\u7a0b\u6267\u884c\u671f\u95f4\uff0c\u5982\u679c\u51fa\u73b0IO\u963b\u585e\u64cd\u4f5c\uff08\u4f8b\u5982\u7f51\u7edcIO\u3001\u6587\u4ef6IO\u3001\u6570\u636e\u5e93\u67e5\u8be2\u7b49\uff09\uff0c\u534f\u7a0b\u4f1a\u81ea\u52a8\u6302\u8d77\uff0c\u5e76\u5c06\u5f53\u524d\u4efb\u52a1\u72b6\u6001\u8bbe\u7f6e\u4e3a\u4f11\u7720\u3002\u5f53IO\u963b\u585e\u5b8c\u6210\u540e\uff0c\u534f\u7a0b\u4f1a\u81ea\u52a8\u5524\u9192\uff0c\u5e76\u5c06\u4efb\u52a1\u72b6\u6001\u8bbe\u7f6e\u4e3a\u6267\u884c\u3002<\/p>\n<p>5.\u5728\u534f\u7a0b\u6267\u884c\u5b8c\u6210\u540e\uff0c\u8c03\u5ea6\u5668\u4f1a\u56de\u6536\u8d44\u6e90\uff0c\u5e76\u5c06\u534f\u7a0b\u72b6\u6001\u8bbe\u7f6e\u4e3a\u7ed3\u675f\u3002<\/p>\n<p>\u4e09\u3001Swoole\u7684\u534f\u7a0b\u4f18\u52bf<\/p>\n<p>Swoole\u7684\u534f\u7a0b\u5177\u6709\u4ee5\u4e0b\u4f18\u70b9\uff1a<\/p>\n<p>1.\u9ad8\u6548\uff1a\u534f\u7a0b\u80fd\u591f\u5728\u540c\u4e00\u7ebf\u7a0b\u4e2d\u5207\u6362\u4efb\u52a1\u6267\u884c\uff0c\u907f\u514d\u4e86\u7ebf\u7a0b\u4e0a\u4e0b\u6587\u5207\u6362\u7684\u5f00\u9500\uff0c\u540c\u65f6\u52a0\u901f\u4e86\u4ee3\u7801\u6267\u884c\u901f\u5ea6\u3002<\/p>\n<p>2.\u8f7b\u91cf\u7ea7\uff1a\u534f\u7a0b\u5360\u7528\u7684\u5185\u5b58\u8d44\u6e90\u975e\u5e38\u5c11\uff0c\u53ef\u4ee5\u540c\u65f6\u652f\u6301\u5927\u91cf\u5e76\u53d1\u8fde\u63a5\u3002<\/p>\n<p>3.\u6613\u4e8e\u8c03\u8bd5\uff1a\u534f\u7a0b\u80fd\u591f\u63d0\u4f9b\u66f4\u7ec6\u7c92\u5ea6\u7684\u8c03\u8bd5\u4fe1\u606f\uff0c\u65b9\u4fbf\u5f00\u53d1\u8005\u8fdb\u884c\u8c03\u8bd5\u3002<\/p>\n<p>4.\u6613\u4e8e\u7ef4\u62a4\uff1a\u534f\u7a0b\u7684\u4ee3\u7801\u6bd4\u4f20\u7edf\u7684\u591a\u7ebf\u7a0b\u4ee3\u7801\u66f4\u7b80\u5355\uff0c\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<p>\u56db\u3001Swoole\u534f\u7a0b\u7684\u4f7f\u7528\u6b65\u9aa4<\/p>\n<p>1.\u5f15\u5165Swoole\u7684\u534f\u7a0b\u5e93<\/p>\n<p>Swoole\u7684\u534f\u7a0b\u5e93\u53ef\u4ee5\u901a\u8fc7Composer\u76f4\u63a5\u5f15\u5165\uff0c\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<p>composer require swoole\/Coroutine<\/p>\n<p>2.\u7f16\u5199\u534f\u7a0b\u4ee3\u7801<\/p>\n<p>\u5728Swoole\u7684\u534f\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5173\u952e\u5b57yield\u6765\u5b9e\u73b0\u534f\u7a0b\u5207\u6362\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<p>function test()<br \/>{<\/p>\n<pre>echo \"coroutine starts\", PHP_EOL;\n\n$result = yield select(null, null, null, 0.5);\n\necho \"coroutine ends, selected: \", $result, PHP_EOL;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>}<\/p>\n<p>\/\/ \u542f\u52a8\u534f\u7a0b<br \/>go(function () {<\/p>\n<pre>test();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>});<\/p>\n<p>3.\u8fd0\u884cSwoole\u534f\u7a0b\u670d\u52a1<\/p>\n<p>\u4f7f\u7528Swoole\u63d0\u4f9b\u7684Server\u7c7b\u6765\u521b\u5efa\u4e00\u4e2a\u534f\u7a0b\u670d\u52a1\uff1a<\/p>\n<p> Coun(function () {<\/p>\n<pre>$server = new Server('0.0.0.0', 9501, SWOOLE_BASE);\n$server-&amp;gt;on('Connect', function ($server, $fd) {\n    echo \"Client $fd connected<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>&#8220;;<\/p>\n<pre>});\n$server-&amp;gt;on('Receive', function ($server, $fd, $from_id, $data) {\n    echo \"Client $fd: $data<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>&#8220;;<\/p>\n<pre>    $server-&amp;gt;send($fd, \"Server received <\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>&#8220;);<\/p>\n<pre>});\n$server-&amp;gt;on('Close', function ($server, $fd) {\n    echo \"Client $fd closed<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>&#8220;;<\/p>\n<pre>});\n$server-&amp;gt;start();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>});<\/p>\n<p>\u5728Swoole\u7684\u534f\u7a0b\u670d\u52a1\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528go\u5173\u952e\u5b57\u6765\u521b\u5efa\u534f\u7a0b\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>go(function () {<\/p>\n<pre>\/\/ \u534f\u7a0b\u6267\u884c\u7684\u4efb\u52a1<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>});<\/p>\n<p>\u4e94\u3001\u603b\u7ed3<\/p>\n<p>Swoole\u7684\u534f\u7a0b\u5b9e\u73b0\u662f\u4e00\u79cd\u975e\u5e38\u9ad8\u6548\u548c\u8f7b\u91cf\u7ea7\u7684\u5e76\u53d1\u673a\u5236\uff0c\u80fd\u591f\u6709\u6548\u89e3\u51b3PHP\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u7684\u6027\u80fd\u95ee\u9898\u3002\u901a\u8fc7\u672c\u6587\u6211\u4eec\u4e86\u89e3\u4e86Swoole\u4e2d\u534f\u7a0b\u7684\u8fd0\u884c\u673a\u5236\u548c\u4f7f\u7528\u65b9\u6cd5\uff0c\u76f8\u4fe1\u8bfb\u8005\u5bf9Swoole\u7684\u534f\u7a0b\u4e5f\u6709\u4e86\u66f4\u6df1\u5165\u7684\u8ba4\u8bc6\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u63a2\u7a76Swoole\u4e2d\u534f\u7a0b\u7684\u8fd0\u884c\u673a\u5236\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>\u662f\u4e00\u4e2a\u57fa\u4e8ephp\u7684\u534f\u7a0b\u6846\u67b6\uff0c\u5b83\u7684\u5f02\u6b65io\u6027\u80fd\u975e\u5e38\u51fa\u8272\u3002swoole\u7684\u6838\u5fc3\u662f\u534f\u7a0b\uff0c\u534f\u7a0b\u662f\u4e00\u79cd\u6bd4\u7ebf\u7a0b\u66f4\u8f7b\u91cf\u7ea7\u7684\u5e76\u53d1\u673a\u5236\uff0c\u53ef\u4ee5\u5728\u540c\u4e00\u7ebf\u7a0b\u4e2d\u5207\u6362\u4efb\u52a1\u6765\u5b9e\u73b0\u5e76\u53d1\u6267\u884c\u3002\u672c\u6587\u5c06\u4f1a\u63a2\u7a76swoole\u4e2d\u534f\u7a0b\u7684\u8fd0\u884c\u673a\u5236\u3002 \u4e00\u3001\u534f\u7a0b\u7684\u6982\u5ff5 \u534f\u7a0b\uff0c\u53c8\u79f0\u5fae\u7ebf\u7a0b\uff0c\u662f\u4e00\u79cd\u6bd4\u7ebf\u7a0b\u66f4\u7ec6\u7c92\u5ea6\u7684\u5e76\u53d1\u673a\u5236\u3002\u534f\u7a0b\u4e0e\u7ebf\u7a0b\u7684\u533a\u522b\u5728\u4e8e\uff0c\u534f\u7a0b\u901a\u8fc7\u65f6\u95f4\u7247\u8f6e\u8f6c\u6765\u5b9e\u73b0\u4efb\u52a1\u5207\u6362\uff0c\u800c\u7ebf\u7a0b\u7531\u64cd\u4f5c\u7cfb\u7edf\u8c03\u5ea6\u5668\u8d1f\u8d23\u5207\u6362\u3002\u56e0\u6b64\uff0c\u534f\u7a0b\u5728\u6027\u80fd\u4e0a\u6bd4\u7ebf\u7a0b\u66f4\u52a0\u51fa\u8272\u3002 \u5728Swoole\u4e2d\uff0c\u534f\u7a0b\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684PHP\u7ebf\u7a0b\u3002\u534f\u7a0b\u53ef\u4ee5\u5728\u540c\u4e00\u7ebf\u7a0b\u4e2d\u5207\u6362\u6267\u884c\u4e0d\u540c\u7684\u4efb\u52a1\uff0c\u5b9e\u73b0\u5e76\u53d1\u6267\u884c\u3002\u76f8\u6bd4\u4e8e\u4f20\u7edf\u7684\u7ebf\u7a0b\u6c60\u6a21\u5f0f\uff0c\u534f\u7a0b\u53ef\u4ee5\u907f\u514d\u7ebf\u7a0b\u4e0a\u4e0b\u6587\u5207\u6362\u7684\u5f00\u9500\uff0c\u540c\u65f6\u534f\u7a0b\u5177\u6709\u66f4\u4f4e\u7684\u5185\u5b58\u5360\u7528\u548c\u66f4\u9ad8\u7684\u6267\u884c\u6548\u7387\u3002 \u4e8c\u3001Swoole\u7684\u534f\u7a0b\u5b9e\u73b0 Swoole\u901a\u8fc7\u534f\u7a0b\u8c03\u5ea6\u5668\u6765\u5b9e\u73b0\u534f\u7a0b\u7684\u8c03\u5ea6\u548c\u5207\u6362\uff0c\u534f\u7a0b\u8c03\u5ea6\u5668\u662fSwoole\u63d0\u4f9b\u7684\u4e00\u79cd\u534f\u7a0b\u8c03\u5ea6\u5f15\u64ce\uff0c\u5b83\u53ef\u4ee5\u57fa\u4e8e\u65f6\u95f4\u7247\u8f6e\u8f6c\u7684\u65b9\u5f0f\u6765\u5207\u6362\u534f\u7a0b\u6267\u884c\u4efb\u52a1\u3002 \u534f\u7a0b\u8c03\u5ea6\u5668\u7684\u5b9e\u73b0\u539f\u7406\u5982\u4e0b\uff1a 1.\u9996\u5148\uff0c\u8c03\u5ea6\u5668\u4f1a\u4e3a\u6bcf\u4e2a\u534f\u7a0b\u5206\u914d\u4e00\u4e2a\u72b6\u6001\uff0c\u4f8b\u5982\u7b49\u5f85\u3001\u6267\u884c\u3001\u4f11\u7720\u7b49\u3002 2.\u8c03\u5ea6\u5668\u4f1a\u901a\u8fc7\u4e00\u4e2a\u4efb\u52a1\u961f\u5217\u6765\u7ba1\u7406\u6240\u6709\u534f\u7a0b\u7684\u72b6\u6001\u3002\u5f53\u5f53\u524d\u534f\u7a0b\u6267\u884c\u5b8c\u6210\u540e\uff0c\u8c03\u5ea6\u5668\u4f1a\u4f18\u5148\u9009\u62e9\u72b6\u6001\u4e3a\u7b49\u5f85\u7684\u534f\u7a0b\u6765\u6267\u884c\u3002 3.\u534f\u7a0b\u4f1a\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u81ea\u52a8\u5224\u65ad\u5f53\u524d\u4efb\u52a1\u662f\u5426\u5b8c\u6210\u3002\u5982\u679c\u5f53\u524d\u4efb\u52a1\u672a\u5b8c\u6210\uff0c\u534f\u7a0b\u4f1a\u5c06\u5176\u6302\u8d77\uff0c\u7136\u540e\u5207\u6362\u5230\u5176\u4ed6\u534f\u7a0b\u6267\u884c\uff0c\u76f4\u5230\u4e0b\u4e00\u6b21\u8c03\u5ea6\u65f6\u518d\u7ee7\u7eed\u6267\u884c\u6302\u8d77\u4efb\u52a1\u3002 4.\u5728\u534f\u7a0b\u6267\u884c\u671f\u95f4\uff0c\u5982\u679c\u51fa\u73b0IO\u963b\u585e\u64cd\u4f5c\uff08\u4f8b\u5982\u7f51\u7edcIO\u3001\u6587\u4ef6IO\u3001\u6570\u636e\u5e93\u67e5\u8be2\u7b49\uff09\uff0c\u534f\u7a0b\u4f1a\u81ea\u52a8\u6302\u8d77\uff0c\u5e76\u5c06\u5f53\u524d\u4efb\u52a1\u72b6\u6001\u8bbe\u7f6e\u4e3a\u4f11\u7720\u3002\u5f53IO\u963b\u585e\u5b8c\u6210\u540e\uff0c\u534f\u7a0b\u4f1a\u81ea\u52a8\u5524\u9192\uff0c\u5e76\u5c06\u4efb\u52a1\u72b6\u6001\u8bbe\u7f6e\u4e3a\u6267\u884c\u3002 5.\u5728\u534f\u7a0b\u6267\u884c\u5b8c\u6210\u540e\uff0c\u8c03\u5ea6\u5668\u4f1a\u56de\u6536\u8d44\u6e90\uff0c\u5e76\u5c06\u534f\u7a0b\u72b6\u6001\u8bbe\u7f6e\u4e3a\u7ed3\u675f\u3002 \u4e09\u3001Swoole\u7684\u534f\u7a0b\u4f18\u52bf Swoole\u7684\u534f\u7a0b\u5177\u6709\u4ee5\u4e0b\u4f18\u70b9\uff1a 1.\u9ad8\u6548\uff1a\u534f\u7a0b\u80fd\u591f\u5728\u540c\u4e00\u7ebf\u7a0b\u4e2d\u5207\u6362\u4efb\u52a1\u6267\u884c\uff0c\u907f\u514d\u4e86\u7ebf\u7a0b\u4e0a\u4e0b\u6587\u5207\u6362\u7684\u5f00\u9500\uff0c\u540c\u65f6\u52a0\u901f\u4e86\u4ee3\u7801\u6267\u884c\u901f\u5ea6\u3002 2.\u8f7b\u91cf\u7ea7\uff1a\u534f\u7a0b\u5360\u7528\u7684\u5185\u5b58\u8d44\u6e90\u975e\u5e38\u5c11\uff0c\u53ef\u4ee5\u540c\u65f6\u652f\u6301\u5927\u91cf\u5e76\u53d1\u8fde\u63a5\u3002 3.\u6613\u4e8e\u8c03\u8bd5\uff1a\u534f\u7a0b\u80fd\u591f\u63d0\u4f9b\u66f4\u7ec6\u7c92\u5ea6\u7684\u8c03\u8bd5\u4fe1\u606f\uff0c\u65b9\u4fbf\u5f00\u53d1\u8005\u8fdb\u884c\u8c03\u8bd5\u3002 4.\u6613\u4e8e\u7ef4\u62a4\uff1a\u534f\u7a0b\u7684\u4ee3\u7801\u6bd4\u4f20\u7edf\u7684\u591a\u7ebf\u7a0b\u4ee3\u7801\u66f4\u7b80\u5355\uff0c\u6613\u4e8e\u7ef4\u62a4\u3002 \u56db\u3001Swoole\u534f\u7a0b\u7684\u4f7f\u7528\u6b65\u9aa4 1.\u5f15\u5165Swoole\u7684\u534f\u7a0b\u5e93 Swoole\u7684\u534f\u7a0b\u5e93\u53ef\u4ee5\u901a\u8fc7Composer\u76f4\u63a5\u5f15\u5165\uff0c\u547d\u4ee4\u5982\u4e0b\uff1a composer require swoole\/Coroutine 2.\u7f16\u5199\u534f\u7a0b\u4ee3\u7801 \u5728Swoole\u7684\u534f\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5173\u952e\u5b57yield\u6765\u5b9e\u73b0\u534f\u7a0b\u5207\u6362\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a function test(){ echo &#8220;coroutine starts&#8221;, PHP_EOL; $result = yield select(null, null, null, 0.5); echo &#8220;coroutine ends, selected: &#8220;, $result, PHP_EOL; \u767b\u5f55\u540e\u590d\u5236 } \/\/ \u542f\u52a8\u534f\u7a0bgo(function () { test(); \u767b\u5f55\u540e\u590d\u5236 }); [&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-23321","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23321","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=23321"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23321\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}