{"id":23474,"date":"2024-11-21T16:09:29","date_gmt":"2024-11-21T08:09:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23474\/"},"modified":"2024-11-21T16:09:29","modified_gmt":"2024-11-21T08:09:29","slug":"swoole%e7%9a%84%e5%8d%8f%e7%a8%8b%e4%b8%8e%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23474\/","title":{"rendered":"Swoole\u7684\u534f\u7a0b\u4e0e\u5f02\u6b65\u7f16\u7a0b\u5b9e\u8df5"},"content":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u5e94\u7528\u7684\u4e0d\u65ad\u53d1\u5c55\uff0c\u9ad8\u5e76\u53d1\u6210\u4e3a\u4e86\u6bcf\u4e2a\u5f00\u53d1\u8005\u5fc5\u987b\u9762\u5bf9\u7684\u6311\u6218\u3002\u4e3a\u4e86\u5e94\u5bf9\u9ad8\u5e76\u53d1\u60c5\u51b5\uff0c\u524d\u7aef\u540c\u5b66\u91c7\u7528\u524d\u7aef\u5c55\u793a\u548c\u5f02\u6b65i\/o\u7b49\u6280\u672f\uff0c\u800c\u540e\u7aef\u540c\u5b66\u91c7\u7528\u534f\u7a0b\u548c\u5f02\u6b65\u7f16\u7a0b\u6280\u672f\u3002\u5176\u4e2d\uff0c\u4f5c\u4e3aphp\u8bed\u8a00\u4e2d\u7684\u4e00\u79cd\u534f\u7a0b\u6846\u67b6\uff0c\u5176\u4f7f\u7528\u534f\u7a0b\u548c\u5f02\u6b65\u7f16\u7a0b\u601d\u60f3\uff0c\u7b80\u5316\u4e86\u9ad8\u5e76\u53d1\u4e0b\u7684\u5f00\u53d1\u548c\u8c03\u8bd5\uff0c\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u5f00\u53d1\u4f53\u9a8c\u3002<\/p>\n<p>\u4e00\u3001\u534f\u7a0b\u4e0e\u5f02\u6b65\u7f16\u7a0b\u7684\u6982\u5ff5<\/p>\n<p>\u5bf9\u4e8e\u534f\u7a0b\u7684\u7406\u89e3\uff0c\u53ef\u4ee5\u7b80\u5355\u7406\u89e3\u4e3a\u201c\u5fae\u7ebf\u7a0b\u201d\uff0c\u4e0e\u7ebf\u7a0b\u76f8\u4f3c\u7684\u6982\u5ff5\uff0c\u4f46\u662f\u4e0e\u7ebf\u7a0b\u7684\u5207\u6362\u673a\u5236\u4e0d\u540c\u3002\u534f\u7a0b\u4e0d\u662f\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u7684\u7ebf\u7a0b\uff0c\u800c\u662f\u5728\u7528\u6237\u8fdb\u7a0b\u5185\u90e8\u8fdb\u884c\u5207\u6362\u3002\u4f7f\u7528\u534f\u7a0b\u53ef\u4ee5\u8fdb\u884c\u975e\u963b\u585e\u7b49\u5f85\uff0c\u540c\u65f6\u63d0\u9ad8CPU\u5229\u7528\u7387\u548c\u51cf\u5c11\u4e0a\u4e0b\u6587\u7684\u5207\u6362\u6b21\u6570\u3002<\/p>\n<p>\u800c\u5f02\u6b65\u7f16\u7a0b\u5219\u662f\u201c\u4e8b\u4ef6\u9a71\u52a8\u201d\u7684\u4e00\u79cd\u7f16\u7a0b\u65b9\u5f0f\uff0c\u5176\u4e3b\u8981\u7279\u70b9\u662f\u91c7\u7528\u975e\u963b\u585eI\/O\uff0c\u907f\u514d\u4e86I\/O\u963b\u585e\u7b49\u5f85\u9020\u6210\u7684\u7ebf\u7a0b\u7b49\u5f85\u65f6\u95f4\uff0c\u63d0\u9ad8\u4e86\u5e76\u53d1\u91cf\u3002\u5728\u5f02\u6b65\u7f16\u7a0b\u4e2d\uff0c\u5f53\u4e8b\u4ef6\u5b8c\u6210\u540e\uff0c\u7a0b\u5e8f\u4f1a\u901a\u77e5\u76f8\u5173\u7ebf\u7a0b\u7ee7\u7eed\u5904\u7406\uff0c\u800c\u4e0d\u662f\u8ba9\u7ebf\u7a0b\u4e00\u76f4\u963b\u585e\u7b49\u5f85\u3002\u5f02\u6b65\u7f16\u7a0b\u91c7\u7528\u56de\u8c03\u65b9\u5f0f\u6765\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\uff0c\u4ee5\u6b64\u6765\u5904\u7406\u534f\u7a0b\u4e4b\u95f4\u7684\u4ea4\u66ff\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002<\/p>\n<p>\u4e8c\u3001Swoole\u7684\u534f\u7a0b\u4e0e\u5f02\u6b65\u7f16\u7a0b\u5b9e\u8df5<\/p>\n<ol>\n<li>\u534f\u7a0b<\/li>\n<\/ol>\n<p>Swoole\u534f\u7a0b\u662f\u5728PHP\u8bed\u8a00\u73af\u5883\u4e0b\uff0c\u6a21\u62df\u5b9e\u73b0\u4e86\u8fdb\u7a0b\u548c\u7ebf\u7a0b\u4e2d\u7684\u534f\u7a0b\u673a\u5236\u3002\u5728Swoole\u7684\u534f\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u534f\u7a0b\u8c03\u5ea6\u5668\uff0c\u5c06PHP\u7684\u8fd0\u884c\u63a7\u5236\u6743\u4ea4\u7ed9\u534f\u7a0b\uff0c\u907f\u514d\u4e86I\/O\u963b\u585e\u7b49\u5f85\u9020\u6210\u7684\u7ebf\u7a0b\u7b49\u5f85\u65f6\u95f4\uff0c\u63d0\u9ad8\u4e86\u8fd0\u884c\u6548\u7387\u3002\u534f\u7a0b\u501f\u52a9\u4e8eswoole_coroutine_create()\u548cswoole_coroutine_resume()\u51fd\u6570\uff0c\u5b9e\u73b0\u4e86\u534f\u7a0b\u4e4b\u95f4\u7684\u5207\u6362\u3002\u540c\u65f6\uff0cSwoole\u63d0\u4f9b\u4e86\u8bf8\u5982swoole_event_add()\u3001swoole_event_set()\u7b49\u4e8b\u4ef6\u9a71\u52a8\u51fd\u6570\uff0c\u663e\u8457\u7b80\u5316\u4e86\u534f\u7a0b\u7f16\u7a0b\u6a21\u578b\u3002<\/p>\n<p>\u4e0b\u9762\uff0c\u6211\u4eec\u4ee5\u4ee3\u7801\u5b9e\u8df5\u7684\u65b9\u5f0f\uff0c\u4e00\u6b65\u6b65\u7406\u89e3Swoole\u534f\u7a0b\u7684\u4f7f\u7528\u3002<\/p>\n<p>1\uff09\u5b89\u88c5Swoole\u6269\u5c55<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Swoole\u6269\u5c55\uff0c\u4ee5\u5b9e\u73b0Swoole\u534f\u7a0b\u7684\u5f00\u53d1\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5Swoole\u6269\u5c55\uff1a<\/p>\n<pre>$ pecl install swoole<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>2\uff09\u521b\u5efa\u534f\u7a0b<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u534f\u7a0b\uff0c\u5e76\u4f7f\u7528swoole_coroutine_resume()\u51fd\u6570\u6267\u884c\u534f\u7a0b\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php function test_coroutine(){\n    echo \"Start coroutine\n\";\n    swoole_coroutine::sleep(1);\n    echo \"End coroutine\n\";\n}\nswoole_coroutine::create(\"test_coroutine\");\necho \"Main func end\n\";<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u4ee3\u7801\u4e2d\u4f7f\u7528\u4e86swoole_coroutine_create()\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u534f\u7a0b\uff0c\u5e76\u4f20\u5165\u4e86\u4e00\u4e2atest_coroutine()\u51fd\u6570\u3002\u6b64\u65f6\uff0c\u534f\u7a0b\u8fd8\u672a\u6267\u884c\uff0c\u8c03\u7528swoole_coroutine_create()\u540e\uff0c\u7cfb\u7edf\u5c06\u8be5\u534f\u7a0b\u63d0\u4ea4\u5230\u534f\u7a0b\u8c03\u5ea6\u5668\u4e2d\uff0c\u7b49\u5f85\u6267\u884c\u3002\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7\u8c03\u7528swoole_coroutine_resume()\u51fd\u6570\uff0c\u6267\u884ctest_coroutine()\u51fd\u6570\uff0c\u5e76\u8f93\u51fa\u76f8\u5173\u7ed3\u679c\u3002<\/p>\n<p>3\uff09\u534f\u7a0b\u95f4\u5207\u6362<\/p>\n<p>\u5728\u534f\u7a0b\u4e2d\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528swoole_coroutine_yield()\u51fd\u6570\u6765\u624b\u52a8\u5207\u6362\u534f\u7a0b\u3002\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php function test_coroutine(){\n    for ($i=0; $i&lt;5; $i++){\n        echo \"Coroutine $i\n\";\n        swoole_coroutine::yield();\n    }\n}\n$c = swoole_coroutine::create(\"test_coroutine\");\nfor ($i=0; $i&lt;5; $i++){\n    swoole_coroutine::resume($c);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u4e0a\u9762\u4ee3\u7801\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u534f\u7a0b\uff0c\u5e76\u5728test_coroutine()\u51fd\u6570\u4e2d\u5faa\u73af5\u6b21\uff0c\u8f93\u51fa\u534f\u7a0b\u7f16\u53f7\u3002\u901a\u8fc7swoole_coroutine_yield()\u51fd\u6570\uff0c\u624b\u52a8\u5207\u6362\u534f\u7a0b\uff0c\u4f7f\u5f97\u591a\u4e2a\u534f\u7a0b\u80fd\u516c\u5e73\u5730\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<ol>\n<li>\u5f02\u6b65\u7f16\u7a0b<\/li>\n<\/ol>\n<p>Swoole\u7684\u5f02\u6b65\u7f16\u7a0b\u4e3b\u8981\u57fa\u4e8ewoole_event_add()\u3001swoole_event_set()\u548cswoole_event_wait()\u7b49\u4e8b\u4ef6\u9a71\u52a8\u51fd\u6570\u5b9e\u73b0\u3002\u5177\u4f53\u800c\u8a00\uff0cwoole_event_add()\u548cswoole_event_set()\u51fd\u6570\u7528\u4e8e\u6dfb\u52a0I\/O\u4e8b\u4ef6\u5230\u4e8b\u4ef6\u5faa\u73af\u4e2d\uff0c\u800cswoole_event_wait()\u51fd\u6570\u5219\u7528\u4e8e\u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\u3002<\/p>\n<p>\u4e0b\u9762\uff0c\u6211\u4eec\u901a\u8fc7\u4ee3\u7801\u7684\u65b9\u5f0f\uff0c\u4e00\u6b65\u6b65\u7406\u89e3Swoole\u7684\u5f02\u6b65\u7f16\u7a0b\u5b9e\u8df5\u3002<\/p>\n<p>1\uff09\u5b89\u88c5Swoole\u6269\u5c55<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Swoole\u6269\u5c55\uff0c\u4ee5\u5b9e\u73b0Swoole\u5f02\u6b65\u7f16\u7a0b\u7684\u5f00\u53d1\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5Swoole\u6269\u5c55\uff1a<\/p>\n<pre>$ pecl install swoole<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>2\uff09\u5f02\u6b65TCP\u901a\u4fe1<\/p>\n<p>\u5728Swoole\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7swoole_client\u548cswoole_server\u5b9e\u73b0\u7cfb\u7edf\u95f4\u7684\u652f\u6301\u5f02\u6b65TCP\u901a\u4fe1\u3002\u5728\u5f02\u6b65TCP\u901a\u4fe1\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528SwooleServer\u542f\u52a8\u4e00\u4e2aTCP\u670d\u52a1\uff0c\u5e76\u5728\u670d\u52a1\u5668\u7aef\u4f7f\u7528swoole_event_add()\u51fd\u6570\u4e3a\u8be5\u670d\u52a1\u6dfb\u52a0\u4e00\u4e2aI\/O\u4e8b\u4ef6\u3002\u6d88\u606f\u53d1\u9001\u8005\u91c7\u7528swoole_client\u5b9e\u73b0\u5f02\u6b65\u901a\u4fe1\u3002\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php \/\/\u5f02\u6b65TCP\u670d\u52a1\u7aef\n$serv = new swoole_server(\"127.0.0.1\", 9501);\n$serv-&gt;set(array(\n    'worker_num' =&amp;gt; 4,\n    'daemonize' =&amp;gt; false,\n));\n\n$serv-&amp;gt;on('Receive', function ($serv, $fd, $from_id, $data) {\n    $serv-&amp;gt;send($fd, 'Server: '.$data);\n    $serv-&amp;gt;close($fd);\n});\n\n$serv-&amp;gt;start();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>&lt;?php \/\/\u5f02\u6b65TCP\u5ba2\u6237\u7aef\n$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);\n\n$client-&gt;on(\"connect\", function($cli) {\n    $cli-&amp;gt;send(\"hello world\n\");\n});\n\n$client-&amp;gt;on(\"receive\", function($cli, $data){\n    echo \"Received: \".$data.\"\n\";\n});\n\n$client-&amp;gt;on(\"error\", function($cli){\n    echo \"Connect failed\n\";\n});\n\n$client-&amp;gt;on(\"close\", function($cli){\n    echo \"Connection closed\n\";\n});\n\n$client-&amp;gt;connect('127.0.0.1', 9501);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u5f02\u6b65TCP\u901a\u4fe1\u7684\u4f8b\u5b50\u3002\u5f53\u5ba2\u6237\u7aef\u53d1\u9001\u4e00\u4e2a\u6d88\u606f\u540e\uff0c\u670d\u52a1\u7aef\u63a5\u6536\u5230\u6d88\u606f\u5e76\u8fd4\u56de\u5904\u7406\u7ed3\u679c\u3002<\/p>\n<p>\u603b\u7ed3\uff1a<\/p>\n<p>\u672c\u6587\u4e3b\u8981\u8bb2\u89e3\u4e86Swoole\u534f\u7a0b\u4e0e\u5f02\u6b65\u7f16\u7a0b\u7684\u5b9e\u8df5\u3002\u5728\u9ad8\u5e76\u53d1\u7684\u4e92\u8054\u7f51\u5e94\u7528\u5f00\u53d1\u4e2d\uff0c\u91c7\u7528\u5f02\u6b65\u7f16\u7a0b\u548c\u534f\u7a0b\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u9ad8\u7cfb\u7edf\u6027\u80fd\uff0c\u540c\u65f6\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002Swoole\u6846\u67b6\u63d0\u4f9b\u4e86\u826f\u597d\u7684\u534f\u7a0b\u548c\u5f02\u6b65\u7f16\u7a0b\u652f\u6301\uff0c\u4f7f\u5f97\u7a0b\u5e8f\u5458\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u9ad8\u6548\u7684\u5f02\u6b65\u5904\u7406\u548c\u534f\u7a0b\u8c03\u5ea6\uff0c\u4ece\u800c\u63d0\u9ad8\u7cfb\u7edf\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSwoole\u7684\u534f\u7a0b\u4e0e\u5f02\u6b65\u7f16\u7a0b\u5b9e\u8df5\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>\u968f\u7740\u4e92\u8054\u7f51\u5e94\u7528\u7684\u4e0d\u65ad\u53d1\u5c55\uff0c\u9ad8\u5e76\u53d1\u6210\u4e3a\u4e86\u6bcf\u4e2a\u5f00\u53d1\u8005\u5fc5\u987b\u9762\u5bf9\u7684\u6311\u6218\u3002\u4e3a\u4e86\u5e94\u5bf9\u9ad8\u5e76\u53d1\u60c5\u51b5\uff0c\u524d\u7aef\u540c\u5b66\u91c7\u7528\u524d\u7aef\u5c55\u793a\u548c\u5f02\u6b65i\/o\u7b49\u6280\u672f\uff0c\u800c\u540e\u7aef\u540c\u5b66\u91c7\u7528\u534f\u7a0b\u548c\u5f02\u6b65\u7f16\u7a0b\u6280\u672f\u3002\u5176\u4e2d\uff0c\u4f5c\u4e3aphp\u8bed\u8a00\u4e2d\u7684\u4e00\u79cd\u534f\u7a0b\u6846\u67b6\uff0c\u5176\u4f7f\u7528\u534f\u7a0b\u548c\u5f02\u6b65\u7f16\u7a0b\u601d\u60f3\uff0c\u7b80\u5316\u4e86\u9ad8\u5e76\u53d1\u4e0b\u7684\u5f00\u53d1\u548c\u8c03\u8bd5\uff0c\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u5f00\u53d1\u4f53\u9a8c\u3002 \u4e00\u3001\u534f\u7a0b\u4e0e\u5f02\u6b65\u7f16\u7a0b\u7684\u6982\u5ff5 \u5bf9\u4e8e\u534f\u7a0b\u7684\u7406\u89e3\uff0c\u53ef\u4ee5\u7b80\u5355\u7406\u89e3\u4e3a\u201c\u5fae\u7ebf\u7a0b\u201d\uff0c\u4e0e\u7ebf\u7a0b\u76f8\u4f3c\u7684\u6982\u5ff5\uff0c\u4f46\u662f\u4e0e\u7ebf\u7a0b\u7684\u5207\u6362\u673a\u5236\u4e0d\u540c\u3002\u534f\u7a0b\u4e0d\u662f\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u7684\u7ebf\u7a0b\uff0c\u800c\u662f\u5728\u7528\u6237\u8fdb\u7a0b\u5185\u90e8\u8fdb\u884c\u5207\u6362\u3002\u4f7f\u7528\u534f\u7a0b\u53ef\u4ee5\u8fdb\u884c\u975e\u963b\u585e\u7b49\u5f85\uff0c\u540c\u65f6\u63d0\u9ad8CPU\u5229\u7528\u7387\u548c\u51cf\u5c11\u4e0a\u4e0b\u6587\u7684\u5207\u6362\u6b21\u6570\u3002 \u800c\u5f02\u6b65\u7f16\u7a0b\u5219\u662f\u201c\u4e8b\u4ef6\u9a71\u52a8\u201d\u7684\u4e00\u79cd\u7f16\u7a0b\u65b9\u5f0f\uff0c\u5176\u4e3b\u8981\u7279\u70b9\u662f\u91c7\u7528\u975e\u963b\u585eI\/O\uff0c\u907f\u514d\u4e86I\/O\u963b\u585e\u7b49\u5f85\u9020\u6210\u7684\u7ebf\u7a0b\u7b49\u5f85\u65f6\u95f4\uff0c\u63d0\u9ad8\u4e86\u5e76\u53d1\u91cf\u3002\u5728\u5f02\u6b65\u7f16\u7a0b\u4e2d\uff0c\u5f53\u4e8b\u4ef6\u5b8c\u6210\u540e\uff0c\u7a0b\u5e8f\u4f1a\u901a\u77e5\u76f8\u5173\u7ebf\u7a0b\u7ee7\u7eed\u5904\u7406\uff0c\u800c\u4e0d\u662f\u8ba9\u7ebf\u7a0b\u4e00\u76f4\u963b\u585e\u7b49\u5f85\u3002\u5f02\u6b65\u7f16\u7a0b\u91c7\u7528\u56de\u8c03\u65b9\u5f0f\u6765\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\uff0c\u4ee5\u6b64\u6765\u5904\u7406\u534f\u7a0b\u4e4b\u95f4\u7684\u4ea4\u66ff\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002 \u4e8c\u3001Swoole\u7684\u534f\u7a0b\u4e0e\u5f02\u6b65\u7f16\u7a0b\u5b9e\u8df5 \u534f\u7a0b Swoole\u534f\u7a0b\u662f\u5728PHP\u8bed\u8a00\u73af\u5883\u4e0b\uff0c\u6a21\u62df\u5b9e\u73b0\u4e86\u8fdb\u7a0b\u548c\u7ebf\u7a0b\u4e2d\u7684\u534f\u7a0b\u673a\u5236\u3002\u5728Swoole\u7684\u534f\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u534f\u7a0b\u8c03\u5ea6\u5668\uff0c\u5c06PHP\u7684\u8fd0\u884c\u63a7\u5236\u6743\u4ea4\u7ed9\u534f\u7a0b\uff0c\u907f\u514d\u4e86I\/O\u963b\u585e\u7b49\u5f85\u9020\u6210\u7684\u7ebf\u7a0b\u7b49\u5f85\u65f6\u95f4\uff0c\u63d0\u9ad8\u4e86\u8fd0\u884c\u6548\u7387\u3002\u534f\u7a0b\u501f\u52a9\u4e8eswoole_coroutine_create()\u548cswoole_coroutine_resume()\u51fd\u6570\uff0c\u5b9e\u73b0\u4e86\u534f\u7a0b\u4e4b\u95f4\u7684\u5207\u6362\u3002\u540c\u65f6\uff0cSwoole\u63d0\u4f9b\u4e86\u8bf8\u5982swoole_event_add()\u3001swoole_event_set()\u7b49\u4e8b\u4ef6\u9a71\u52a8\u51fd\u6570\uff0c\u663e\u8457\u7b80\u5316\u4e86\u534f\u7a0b\u7f16\u7a0b\u6a21\u578b\u3002 \u4e0b\u9762\uff0c\u6211\u4eec\u4ee5\u4ee3\u7801\u5b9e\u8df5\u7684\u65b9\u5f0f\uff0c\u4e00\u6b65\u6b65\u7406\u89e3Swoole\u534f\u7a0b\u7684\u4f7f\u7528\u3002 1\uff09\u5b89\u88c5Swoole\u6269\u5c55 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Swoole\u6269\u5c55\uff0c\u4ee5\u5b9e\u73b0Swoole\u534f\u7a0b\u7684\u5f00\u53d1\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5Swoole\u6269\u5c55\uff1a $ pecl install swoole \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 2\uff09\u521b\u5efa\u534f\u7a0b \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u534f\u7a0b\uff0c\u5e76\u4f7f\u7528swoole_coroutine_resume()\u51fd\u6570\u6267\u884c\u534f\u7a0b\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a &lt;?php function test_coroutine(){ echo &#8220;Start coroutine &#8220;; swoole_coroutine::sleep(1); echo &#8220;End coroutine &#8220;; } swoole_coroutine::create(&#8220;test_coroutine&#8221;); echo &#8220;Main func end &#8220;; \u767b\u5f55\u540e\u590d\u5236 \u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u4ee3\u7801\u4e2d\u4f7f\u7528\u4e86swoole_coroutine_create()\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u534f\u7a0b\uff0c\u5e76\u4f20\u5165\u4e86\u4e00\u4e2atest_coroutine()\u51fd\u6570\u3002\u6b64\u65f6\uff0c\u534f\u7a0b\u8fd8\u672a\u6267\u884c\uff0c\u8c03\u7528swoole_coroutine_create()\u540e\uff0c\u7cfb\u7edf\u5c06\u8be5\u534f\u7a0b\u63d0\u4ea4\u5230\u534f\u7a0b\u8c03\u5ea6\u5668\u4e2d\uff0c\u7b49\u5f85\u6267\u884c\u3002\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7\u8c03\u7528swoole_coroutine_resume()\u51fd\u6570\uff0c\u6267\u884ctest_coroutine()\u51fd\u6570\uff0c\u5e76\u8f93\u51fa\u76f8\u5173\u7ed3\u679c\u3002 3\uff09\u534f\u7a0b\u95f4\u5207\u6362 \u5728\u534f\u7a0b\u4e2d\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528swoole_coroutine_yield()\u51fd\u6570\u6765\u624b\u52a8\u5207\u6362\u534f\u7a0b\u3002\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a &lt;?php function test_coroutine(){ for ($i=0; $i&lt;5; $i++){ echo &#8220;Coroutine $i &#8220;; swoole_coroutine::yield(); } } [&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-23474","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23474","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=23474"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23474\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}