{"id":1064,"date":"2025-01-19T13:43:45","date_gmt":"2025-01-19T05:43:45","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1064"},"modified":"2025-01-19T13:43:45","modified_gmt":"2025-01-19T05:43:45","slug":"swoole-%e6%a6%82%e8%bf%b0%e4%b8%8e%e5%9f%ba%e7%a1%80%e6%95%99%e7%a8%8b%ef%bc%9aphp-%e9%ab%98%e6%80%a7%e8%83%bd%e7%bc%96%e7%a8%8b%e7%9a%84%e9%9d%a9%e5%91%bd%e6%80%a7%e6%89%a9%e5%b1%95","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/19\/swoole-%e6%a6%82%e8%bf%b0%e4%b8%8e%e5%9f%ba%e7%a1%80%e6%95%99%e7%a8%8b%ef%bc%9aphp-%e9%ab%98%e6%80%a7%e8%83%bd%e7%bc%96%e7%a8%8b%e7%9a%84%e9%9d%a9%e5%91%bd%e6%80%a7%e6%89%a9%e5%b1%95\/","title":{"rendered":"Swoole \u6982\u8ff0\u4e0e\u57fa\u7840\u6559\u7a0b\uff1aPHP \u9ad8\u6027\u80fd\u7f16\u7a0b\u7684\u9769\u547d\u6027\u6269\u5c55"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Swoole \u6982\u8ff0\u4e0e\u57fa\u7840<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. <strong>Swoole \u6982\u8ff0<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684 PHP \u6269\u5c55\uff0c\u65e8\u5728\u8ba9 PHP \u5177\u5907\u9ad8\u5e76\u53d1\u548c\u5f02\u6b65\u7f16\u7a0b\u7684\u80fd\u529b\uff0c\u9002\u7528\u4e8e\u6784\u5efa\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u5e94\u7528\u3001\u5b9e\u65f6\u7cfb\u7edf\u548c\u5fae\u670d\u52a1\u67b6\u6784\u3002\u4e0e\u4f20\u7edf\u7684 PHP \u6a21\u578b\u4e0d\u540c\uff0cSwoole \u57fa\u4e8e <strong>\u591a\u8fdb\u7a0b<\/strong> \u548c <strong>\u534f\u7a0b<\/strong> \u7684\u5f02\u6b65 I\/O \u673a\u5236\uff0c\u80fd\u591f\u6709\u6548\u63d0\u5347 PHP \u5728\u5904\u7406\u9ad8\u5e76\u53d1\u8bf7\u6c42\u65f6\u7684\u6027\u80fd\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u5c24\u5176\u662f <strong>\u591a\u8fdb\u7a0b<\/strong>\u3001<strong>\u534f\u7a0b<\/strong>\u3001<strong>\u5f02\u6b65 I\/O<\/strong>\u3001<strong>\u5b9a\u65f6\u4efb\u52a1<\/strong>\u3001<strong>WebSocket<\/strong> \u548c <strong>TCP\/UDP<\/strong> \u652f\u6301\uff0c\u4f7f\u5f97 PHP \u4e0d\u518d\u4ec5\u4ec5\u662f\u4e00\u4e2a\u4f20\u7edf\u7684 Web \u5f00\u53d1\u8bed\u8a00\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e\u6784\u5efa\u9ad8\u5e76\u53d1\u3001\u9ad8\u6027\u80fd\u7684\u7cfb\u7edf\uff0c\u80fd\u591f\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8bf7\u6c42\u800c\u4e0d\u4f1a\u51fa\u73b0\u6027\u80fd\u74f6\u9888\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. <strong>\u4e3a\u4ec0\u4e48\u9009\u62e9 Swoole\uff1f<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f20\u7edf\u7684 PHP \u4f7f\u7528 <strong>\u540c\u6b65\u963b\u585e\u6a21\u578b<\/strong>\uff0c\u610f\u5473\u7740\u6bcf\u6b21 HTTP \u8bf7\u6c42\u5904\u7406\u65f6\uff0c\u670d\u52a1\u5668\u4f1a\u4e3a\u6bcf\u4e2a\u8bf7\u6c42\u5206\u914d\u4e00\u4e2a\u7ebf\u7a0b\uff08\u6216\u8fdb\u7a0b\uff09\uff0c\u8fd9\u4e9b\u7ebf\u7a0b\uff08\u6216\u8fdb\u7a0b\uff09\u4e92\u76f8\u72ec\u7acb\uff0c\u4f46\u5728\u9ad8\u5e76\u53d1\u65f6\u8fd9\u79cd\u6a21\u578b\u4f1a\u5bfc\u81f4 <strong>\u8d44\u6e90\u6d6a\u8d39<\/strong>\uff0c<strong>\u5ef6\u8fdf\u589e\u52a0<\/strong>\uff0c\u5e76\u4e14\u5bf9\u6027\u80fd\u5e26\u6765\u8f83\u5927\u7684\u5f71\u54cd\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u6539\u53d8\u4e86\u8fd9\u4e00\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5f02\u6b65 I\/O<\/strong>\uff1aSwoole \u901a\u8fc7\u4e8b\u4ef6\u9a71\u52a8\u7684\u65b9\u5f0f\u5904\u7406\u6240\u6709 I\/O \u64cd\u4f5c\uff0c\u907f\u514d\u4e86\u4f20\u7edf\u963b\u585e I\/O \u6a21\u578b\u7684\u6027\u80fd\u74f6\u9888\u3002\u8fd9\u6837\u5c31\u80fd\u540c\u65f6\u5904\u7406\u591a\u4e2a I\/O \u8bf7\u6c42\uff0c\u800c\u4e0d\u5fc5\u7b49\u5f85\u6bcf\u4e2a I\/O \u64cd\u4f5c\u5b8c\u6210\u3002<\/li>\n\n\n\n<li><strong>\u534f\u7a0b\uff08Coroutine\uff09<\/strong>\uff1aSwoole \u63d0\u4f9b\u7684\u534f\u7a0b\u53ef\u4ee5\u8ba9\u4f60\u50cf\u5199\u540c\u6b65\u4ee3\u7801\u4e00\u6837\uff0c\u5199\u5f02\u6b65\u4ee3\u7801\u3002\u8fd9\u662f\u56e0\u4e3a\u534f\u7a0b\u4f1a\u5728 I\/O \u64cd\u4f5c\u65f6\u81ea\u52a8\u6302\u8d77\uff0c\u907f\u514d\u4e86\u4f20\u7edf\u5f02\u6b65\u7f16\u7a0b\u4e2d\u56de\u8c03\u5730\u72f1\u7684\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u9ad8\u6548\u7684\u8fdb\u7a0b\u7ba1\u7406<\/strong>\uff1aSwoole \u652f\u6301\u591a\u8fdb\u7a0b\u6a21\u5f0f\uff0c\u80fd\u591f\u540c\u65f6\u5904\u7406\u5927\u91cf\u8bf7\u6c42\uff0c\u5e76\u4e14\u901a\u8fc7\u591a\u8fdb\u7a0b\u5171\u4eab\u5185\u5b58\u3001\u6d88\u606f\u961f\u5217\u7b49\u673a\u5236\u8fdb\u884c\u9ad8\u6548\u7684\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff08IPC\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u8fd9\u4e9b\u7279\u6027\uff0cSwoole \u53ef\u4ee5\u663e\u8457\u63d0\u5347\u5e94\u7528\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u80fd\uff0c\u4f7f PHP \u6210\u4e3a\u4e00\u4e2a\u9ad8\u6548\u7684\u7f51\u7edc\u7f16\u7a0b\u5de5\u5177\uff0c\u9002\u5408\u6784\u5efa\u50cf <strong>\u5373\u65f6\u901a\u8baf\u7cfb\u7edf<\/strong>\u3001<strong>\u5b9e\u65f6\u6570\u636e\u63a8\u9001<\/strong>\u3001<strong>\u5fae\u670d\u52a1\u67b6\u6784<\/strong> \u7b49\u9ad8\u5e76\u53d1\u7684\u7f51\u7edc\u5e94\u7528\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3. <strong>Swoole \u7684\u4f18\u52bf<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9ad8\u6027\u80fd<\/strong>\uff1aSwoole \u57fa\u4e8e C \u8bed\u8a00\u5b9e\u73b0\uff0c\u63d0\u4f9b\u6bd4\u4f20\u7edf PHP \u66f4\u9ad8\u6548\u7684\u6027\u80fd\uff0c\u80fd\u591f\u8f7b\u677e\u5904\u7406\u6570\u5343\u3001\u751a\u81f3\u6570\u4e07\u5e76\u53d1\u8fde\u63a5\u3002<\/li>\n\n\n\n<li><strong>\u4f4e\u5ef6\u8fdf<\/strong>\uff1a\u901a\u8fc7\u5f02\u6b65\u548c\u534f\u7a0b\u673a\u5236\uff0cSwoole \u80fd\u663e\u8457\u964d\u4f4e\u8bf7\u6c42\u7684\u5ef6\u8fdf\uff0c\u7279\u522b\u9002\u5408\u5b9e\u65f6\u901a\u8baf\u3001\u6e38\u620f\u670d\u52a1\u5668\u7b49\u9700\u8981\u4f4e\u5ef6\u8fdf\u7684\u5e94\u7528\u573a\u666f\u3002<\/li>\n\n\n\n<li><strong>\u5185\u5b58\u4f18\u5316<\/strong>\uff1aSwoole \u91c7\u7528\u5185\u5b58\u6c60\u6280\u672f\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u5185\u5b58\u5171\u4eab\u548c\u9ad8\u6548\u7684\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff0c\u4f7f\u5f97\u5185\u5b58\u7684\u4f7f\u7528\u66f4\u52a0\u9ad8\u6548\u3002<\/li>\n\n\n\n<li><strong>\u8fdb\u7a0b\u548c\u534f\u7a0b\u7ed3\u5408<\/strong>\uff1aSwoole \u652f\u6301\u591a\u8fdb\u7a0b\u548c\u534f\u7a0b\u76f8\u7ed3\u5408\u7684\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u6548\u7684\u4efb\u52a1\u5206\u914d\u548c\u5904\u7406\uff0c\u5145\u5206\u5229\u7528\u7cfb\u7edf\u8d44\u6e90\u3002<\/li>\n\n\n\n<li><strong>\u8de8\u5e73\u53f0\u652f\u6301<\/strong>\uff1aSwoole \u53ef\u5728 Linux\u3001macOS \u548c Windows \u7cfb\u7edf\u4e0a\u8fd0\u884c\uff0c\u5e76\u652f\u6301 Docker \u90e8\u7f72\uff0c\u6613\u4e8e\u96c6\u6210\u5230\u4e91\u539f\u751f\u73af\u5883\u4e2d\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">4. <strong>Swoole \u7684\u5e94\u7528\u573a\u666f<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5b9e\u65f6 Web \u5e94\u7528<\/strong>\uff1a\u4f8b\u5982\u5373\u65f6\u901a\u8baf\u7cfb\u7edf\uff08\u804a\u5929\u3001\u63a8\u9001\u901a\u77e5\u3001\u5728\u7ebf\u5ba2\u670d\uff09\u548c WebSocket \u534f\u8bae\u7684\u5b9e\u73b0\u3002<\/li>\n\n\n\n<li><strong>\u9ad8\u5e76\u53d1\u7684 Web API<\/strong>\uff1a\u9002\u7528\u4e8e\u6784\u5efa\u80fd\u591f\u627f\u53d7\u9ad8\u5e76\u53d1\u8bf7\u6c42\u7684 RESTful API \u6216 GraphQL \u670d\u52a1\u3002<\/li>\n\n\n\n<li><strong>\u5f02\u6b65\u4efb\u52a1\u5904\u7406<\/strong>\uff1a\u9002\u5408\u7528\u6765\u7ba1\u7406\u548c\u8c03\u5ea6\u540e\u53f0\u4efb\u52a1\uff0c\u652f\u6301\u5f02\u6b65\u4efb\u52a1\u3001\u5b9a\u65f6\u4efb\u52a1\u3001\u5ef6\u65f6\u961f\u5217\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u5fae\u670d\u52a1\u67b6\u6784<\/strong>\uff1a\u53ef\u4ee5\u7528\u4e8e\u6784\u5efa\u5fae\u670d\u52a1\u4e4b\u95f4\u7684\u901a\u4fe1\u5c42\uff0c\u5904\u7406\u9ad8\u9891\u7684\u8bf7\u6c42\u548c\u5b9e\u65f6\u6570\u636e\u4ea4\u6362\u3002<\/li>\n\n\n\n<li><strong>\u6d41\u5a92\u4f53\u5e94\u7528<\/strong>\uff1a\u4f8b\u5982\u76f4\u64ad\u3001\u89c6\u9891\u6d41\u3001\u97f3\u9891\u6d41\u7684\u5b9e\u65f6\u63a8\u9001\uff0cSwoole \u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u7f51\u7edc I\/O \u652f\u6301\uff0c\u80fd\u591f\u6ee1\u8db3\u6d41\u5a92\u4f53\u7684\u9ad8\u5e76\u53d1\u8981\u6c42\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>Swoole \u57fa\u7840<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">5.1 <strong>\u5b89\u88c5 Swoole<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u7684\u5b89\u88c5\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u4ee5\u4e0b\u662f\u6700\u5e38\u89c1\u7684\u4e24\u79cd\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">1. \u4f7f\u7528 PECL \u5b89\u88c5\uff1a<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>pecl install swoole<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 PECL \u5b89\u88c5\u65f6\uff0c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u4e0b\u8f7d\u5e76\u7f16\u8bd1 Swoole \u6269\u5c55\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">2. \u624b\u52a8\u7f16\u8bd1\u5b89\u88c5\uff1a<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>git clone https:\/\/github.com\/swoole\/swoole-src.git\ncd swoole-src\nphpize\n.\/configure\nmake &amp;&amp; sudo make install<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u5c06\u4ece\u6e90\u4ee3\u7801\u7f16\u8bd1\u5e76\u5b89\u88c5 Swoole \u6269\u5c55\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">3. \u914d\u7f6e <code>php.ini<\/code><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u786e\u8ba4 PHP \u914d\u7f6e\u6587\u4ef6\u4e2d\u662f\u5426\u52a0\u8f7d\u4e86 Swoole \u6269\u5c55\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php -m | grep swoole<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c Swoole \u5b89\u88c5\u6210\u529f\uff0c\u5c06\u4f1a\u663e\u793a\u51fa <code>swoole<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 <code>php.ini<\/code> \u914d\u7f6e\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>extension=swoole.so<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">5.2 <strong>\u542f\u52a8 HTTP \u670d\u52a1\u5668<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u9ad8\u6548\u7684 HTTP \u670d\u52a1\u5668\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 Swoole \u521b\u5efa HTTP \u670d\u52a1\u5668\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nuse Swoole\\HTTP\\Server;\n\n$server = new Server(\"127.0.0.1\", 9501);\n\n$server-&gt;on(\"Request\", function ($request, $response) {\n    $response-&gt;header(\"Content-Type\", \"text\/html\");\n    $response-&gt;end(\"&lt;h1&gt;Hello, Swoole!&lt;\/h1&gt;\");\n});\n\n$server-&gt;start();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Swoole\\HTTP\\Server<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a HTTP \u670d\u52a1\u5668\uff0c\u76d1\u542c <code>127.0.0.1:9501<\/code> \u5730\u5740\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>on(\"Request\", ...)<\/code> \u65b9\u6cd5\u76d1\u542c HTTP \u8bf7\u6c42\u4e8b\u4ef6\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u7b80\u5355\u7684 HTML \u9875\u9762\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u542f\u52a8\u670d\u52a1\u5668\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee <code>http:\/\/127.0.0.1:9501<\/code> \u67e5\u770b\u8f93\u51fa\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">5.3 <strong>\u4e8b\u4ef6\u5faa\u73af\u4e0e\u5f02\u6b65\u5904\u7406<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u7684\u6838\u5fc3\u662f\u57fa\u4e8e\u4e8b\u4ef6\u9a71\u52a8\u7684\u5f02\u6b65 I\/O \u6a21\u578b\u3002Swoole \u63d0\u4f9b\u4e86\u4e8b\u4ef6\u5faa\u73af\u673a\u5236\uff0c\u80fd\u591f\u5728\u4e00\u4e2a\u8fdb\u7a0b\u4e2d\u5904\u7406\u5927\u91cf\u7684\u7f51\u7edc\u8fde\u63a5\u8bf7\u6c42\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u652f\u6301\u4ee5\u4e0b\u51e0\u79cd\u4e8b\u4ef6\u7c7b\u578b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>onStart<\/code>\uff1a\u670d\u52a1\u542f\u52a8\u65f6\u89e6\u53d1\u3002<\/li>\n\n\n\n<li><code>onWorkerStart<\/code>\uff1a\u5de5\u4f5c\u8fdb\u7a0b\u542f\u52a8\u65f6\u89e6\u53d1\u3002<\/li>\n\n\n\n<li><code>onRequest<\/code>\uff1a\u6bcf\u6b21\u8bf7\u6c42\u5230\u6765\u65f6\u89e6\u53d1\u3002<\/li>\n\n\n\n<li><code>onShutdown<\/code>\uff1a\u670d\u52a1\u5173\u95ed\u65f6\u89e6\u53d1\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u4e2a\u7b80\u5355\u7684\u4e8b\u4ef6\u5faa\u73af\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nuse Swoole\\Server;\n\n$server = new Server(\"127.0.0.1\", 9501);\n\n$server-&gt;on(\"Start\", function ($server) {\n    echo \"Swoole Server started at http:\/\/127.0.0.1:9501\\n\";\n});\n\n$server-&gt;on(\"Request\", function ($request, $response) {\n    $response-&gt;end(\"Hello, Swoole!\");\n});\n\n$server-&gt;start();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">5.4 <strong>\u534f\u7a0b\u4e0e\u5f02\u6b65 I\/O<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u7684\u534f\u7a0b\uff08Coroutine\uff09\u673a\u5236\u8ba9 PHP \u5f00\u53d1\u8005\u80fd\u591f\u4ee5\u540c\u6b65\u7684\u65b9\u5f0f\u7f16\u5199\u5f02\u6b65\u4ee3\u7801\u3002\u534f\u7a0b\u662f\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u80fd\u591f\u9ad8\u6548\u5730\u8c03\u5ea6\u548c\u5207\u6362\uff0c\u907f\u514d\u4e86\u4f20\u7edf\u5f02\u6b65\u7f16\u7a0b\u4e2d\u7684\u56de\u8c03\u5730\u72f1\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c\u4f7f\u7528\u534f\u7a0b\u8fdb\u884c\u5f02\u6b65\u6570\u636e\u5e93\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Swoole\\Coroutine\\run(function () {\n    $db = new Swoole\\Coroutine\\Mysql();\n    $result = $db-&gt;query(\"SELECT * FROM users\");\n    var_dump($result);\n});<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u8ff0\u4ee3\u7801\u901a\u8fc7 Swoole \u7684\u534f\u7a0b MySQL \u5ba2\u6237\u7aef\u8fdb\u884c\u5f02\u6b65\u67e5\u8be2\uff0c\u534f\u7a0b\u4f1a\u5728\u7b49\u5f85\u67e5\u8be2\u7ed3\u679c\u65f6\u81ea\u52a8\u6302\u8d77\uff0c\u5e76\u5728\u7ed3\u679c\u8fd4\u56de\u540e\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">5.5 <strong>\u591a\u8fdb\u7a0b\u4e0e\u8fdb\u7a0b\u7ba1\u7406<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u8fdb\u7a0b\u7ba1\u7406\u80fd\u529b\uff0c\u652f\u6301\u591a\u8fdb\u7a0b\u3001\u8fdb\u7a0b\u6c60\u4ee5\u53ca\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff08IPC\uff09\u3002\u8fd9\u4f7f\u5f97\u5f00\u53d1\u8005\u80fd\u591f\u5c06\u4efb\u52a1\u5206\u914d\u7ed9\u591a\u4e2a\u8fdb\u7a0b\uff0c\u5145\u5206\u5229\u7528\u591a\u6838 CPU \u7684\u8ba1\u7b97\u80fd\u529b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u591a\u8fdb\u7a0b\u670d\u52a1\u5668\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$server = new Swoole\\Server(\"127.0.0.1\", 9501);\n\n$server-&gt;set(&#91;\n    'worker_num' =&gt; 4,  \/\/ \u8bbe\u7f6e\u5de5\u4f5c\u8fdb\u7a0b\u6570\n]);\n\n$server-&gt;on(\"WorkerStart\", function ($server, $workerId) {\n    echo \"Worker $workerId started\\n\";\n});\n\n$server-&gt;on(\"Receive\", function ($server, $fd, $fromId, $data) {\n    $server-&gt;send($fd, \"Received: $data\");\n});\n\n$server-&gt;start();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cSwoole \u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b 4 \u4e2a\u5de5\u4f5c\u8fdb\u7a0b\u7684 TCP \u670d\u52a1\u5668\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">5.6 <strong>Swoole \u7684\u9ad8\u7ea7\u7279\u6027<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>WebSocket \u652f\u6301<\/strong>\uff1aSwoole \u652f\u6301 WebSocket \u534f<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bae\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b9e\u65f6\u901a\u4fe1\u5e94\u7528\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5b9a\u65f6\u4efb\u52a1<\/strong>\uff1aSwoole \u63d0\u4f9b\u4e86\u5b9a\u65f6\u5668\u529f\u80fd\uff0c\u53ef\u4ee5\u8ba9\u4f60\u5b9a\u671f\u6267\u884c\u4efb\u52a1\u3002<\/li>\n\n\n\n<li><strong>\u4efb\u52a1\u6295\u9012\u4e0e\u961f\u5217<\/strong>\uff1a\u652f\u6301\u5f02\u6b65\u4efb\u52a1\u5904\u7406\u548c\u4efb\u52a1\u6295\u9012\u673a\u5236\u3002<\/li>\n\n\n\n<li><strong>\u5185\u5b58\u5171\u4eab<\/strong>\uff1a\u652f\u6301\u8fdb\u7a0b\u95f4\u7684\u5185\u5b58\u5171\u4eab\uff0c\u80fd\u591f\u8fdb\u884c\u9ad8\u6548\u7684\u6d88\u606f\u4f20\u9012\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u662f PHP \u4e2d\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u6269\u5c55\uff0c\u5b83\u901a\u8fc7\u591a\u8fdb\u7a0b\u3001\u534f\u7a0b\u548c\u5f02\u6b65 I\/O \u6280\u672f\u5927\u5e45\u5ea6\u63d0\u5347\u4e86 PHP \u7684\u6027\u80fd\u3002Swoole \u4e0d\u4ec5\u80fd\u591f\u5e2e\u52a9\u5f00\u53d1\u8005\u6784\u5efa\u9ad8\u5e76\u53d1\u7684 Web \u670d\u52a1\uff0c\u8fd8\u80fd\u5b9e\u73b0\u5b9e\u65f6\u63a8\u9001\u3001\u540e\u53f0\u4efb\u52a1\u5904\u7406\u3001\u5fae\u670d\u52a1\u7b49\u591a\u79cd\u5e94\u7528\u573a\u666f\u3002\u5bf9\u4e8e\u9700\u8981\u9ad8\u5e76\u53d1\u548c\u4f4e\u5ef6\u8fdf\u7684\u7cfb\u7edf\uff0cSwoole \u662f\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u9009\u62e9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Swoole \u6982\u8ff0\u4e0e\u57fa\u7840 1. Swoole \u6982\u8ff0 Swoole \u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684 PHP \u6269\u5c55\uff0c\u65e8\u5728\u8ba9 PHP [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64],"tags":[],"class_list":["post-1064","post","type-post","status-publish","format-standard","hentry","category-swoole"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1064","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1064"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1064\/revisions"}],"predecessor-version":[{"id":1065,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1064\/revisions\/1065"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}