{"id":24992,"date":"2024-11-21T12:07:01","date_gmt":"2024-11-21T04:07:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24992\/"},"modified":"2024-11-21T12:07:01","modified_gmt":"2024-11-21T04:07:01","slug":"swoole%e5%ae%9e%e6%88%98%ef%bc%9a%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%8d%8f%e7%a8%8b%e6%8f%90%e5%8d%87%e5%ba%94%e7%94%a8%e7%9a%84%e6%80%a7%e8%83%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24992\/","title":{"rendered":"Swoole\u5b9e\u6218\uff1a\u5982\u4f55\u4f7f\u7528\u534f\u7a0b\u63d0\u5347\u5e94\u7528\u7684\u6027\u80fd"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169934097880536.jpg\" class=\"aligncenter\" title=\"Swoole\u5b9e\u6218\uff1a\u5982\u4f55\u4f7f\u7528\u534f\u7a0b\u63d0\u5347\u5e94\u7528\u7684\u6027\u80fd\u63d2\u56fe\" alt=\"Swoole\u5b9e\u6218\uff1a\u5982\u4f55\u4f7f\u7528\u534f\u7a0b\u63d0\u5347\u5e94\u7528\u7684\u6027\u80fd\u63d2\u56fe\" \/><\/p>\n<p>Swoole\u5b9e\u6218\uff1a\u5982\u4f55\u4f7f\u7528\u534f\u7a0b\u63d0\u5347\u5e94\u7528\u7684\u6027\u80fd<\/p>\n<p>\u968f\u7740\u4e92\u8054\u7f51\u5e94\u7528\u8d8a\u6765\u8d8a\u590d\u6742\uff0c\u6027\u80fd\u6210\u4e3a\u4e86\u4e00\u4e2a\u8d8a\u6765\u8d8a\u91cd\u8981\u7684\u95ee\u9898\u3002\u800cSwoole\u4f5c\u4e3a\u4e00\u4e2a\u9762\u5411\u534f\u7a0b\u7684\u9ad8\u6027\u80fd\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u53ef\u4ee5\u5f88\u597d\u5730\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u672c\u6587\u5c06\u4ecb\u7ecdSwoole\u534f\u7a0b\u7684\u4e00\u4e9b\u57fa\u7840\u6982\u5ff5\uff0c\u5e76\u4ee5\u5b9e\u4f8b\u4e3a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528\u534f\u7a0b\u63d0\u5347\u5e94\u7528\u7684\u6027\u80fd\u3002<\/p>\n<p>\u4e00\u3001\u4ec0\u4e48\u662f\u534f\u7a0b<\/p>\n<p>\u534f\u7a0b\uff08Coroutine\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u5728\u5355\u7ebf\u7a0b\u4e0a\u5b9e\u73b0\u591a\u4efb\u52a1\u534f\u4f5c\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u534f\u7a0b\u4e4b\u95f4\u81ea\u7531\u5207\u6362\u3002\u5728Swoole\u4e2d\uff0c\u534f\u7a0b\u53ef\u4ee5\u7b80\u5316\u5f02\u6b65\u7f16\u7a0b\u7684\u590d\u6742\u6027\uff0c\u901a\u8fc7\u534f\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u50cf\u7f16\u5199\u540c\u6b65\u4ee3\u7801\u4e00\u6837\u7f16\u5199\u5f02\u6b65\u4ee3\u7801\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p>\u4e8c\u3001\u534f\u7a0b\u7684\u57fa\u7840\u4f7f\u7528<\/p>\n<p>\u5728Swoole\u4e2d\uff0c\u534f\u7a0b\u662f\u901a\u8fc7_coroutine_create\u51fd\u6570\u6765\u521b\u5efa\u7684\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>\/\/\u521b\u5efa\u534f\u7a0b\n$cid = swoole_coroutine_create(function(){\n    echo \"Hello Coroutine\n\";\n});\n\n\/\/\u82e5\u4e3b\u7ebf\u7a0b\u4e0d\u963b\u585e\uff0c\u5219\u534f\u7a0b\u65e0\u6cd5\u6267\u884c<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u521b\u5efa\u534f\u7a0b\u540e\uff0c\u9700\u8981\u4f7f\u7528swoole_event_wait\u51fd\u6570\u6765\u7b49\u5f85\u534f\u7a0b\u7684\u6267\u884c\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>\/\/\u521b\u5efa\u534f\u7a0b\n$cid = swoole_coroutine_create(function(){\n    echo \"Hello Coroutine\n\";\n});\n\n\/\/\u7b49\u5f85\u534f\u7a0b\u6267\u884c\nswoole_event_wait();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u53ef\u4ee5\u8f93\u51fa&#8221;Hello Coroutine&#8221;\uff0c\u8bf4\u660e\u534f\u7a0b\u5df2\u7ecf\u6210\u529f\u6267\u884c\u3002<\/p>\n<p>\u5728\u534f\u7a0b\u4e2d\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528swoole_coroutine_yield\u51fd\u6570\u6765\u5c06\u5f53\u524d\u534f\u7a0b\u8ba9\u51fa\uff0c\u8ba9\u5176\u4ed6\u534f\u7a0b\u6267\u884c\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>$cid1 = swoole_coroutine_create(function(){\n    echo \"Coroutine 1\n\";\n    swoole_coroutine_yield();\n    echo \"Coroutine 1 resume\n\";\n});\n\n$cid2 = swoole_coroutine_create(function(){\n    echo \"Coroutine 2\n\";\n    swoole_coroutine_yield();\n    echo \"Coroutine 2 resume\n\";\n});\n\nswoole_coroutine_resume($cid1);\nswoole_coroutine_resume($cid2);\nswoole_event_wait();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u5148\u521b\u5efa\u4e86\u4e24\u4e2a\u534f\u7a0b\uff0c\u7136\u540e\u4f7f\u7528swoole_coroutine_resume\u51fd\u6570\u5c06\u4e24\u4e2a\u534f\u7a0b\u5206\u522b\u6062\u590d\u6267\u884c\uff0c\u7531\u4e8e\u534f\u7a0b\u4e2d\u8c03\u7528\u4e86swoole_coroutine_yield\u51fd\u6570\uff0c\u56e0\u6b64\u534f\u7a0b\u4f1a\u5206\u522b\u8f93\u51fa&#8221;Coroutine 1&#8243;\u548c&#8221;Coroutine 2&#8243;\uff0c\u7136\u540e\u90fd\u6682\u505c\u6267\u884c\u5e76\u8ba9\u51faCPU\uff0c\u6700\u540e\u534f\u7a0b\u6267\u884c\u5b8c\u4e86\u4e00\u6b21\u5faa\u73af\u540e\uff0c\u83b7\u5f97CPU\uff0c\u5206\u522b\u8f93\u51fa&#8221;Coroutine 1 resume&#8221;\u548c&#8221;Coroutine 2 resume&#8221;\u3002<\/p>\n<p>\u4e09\u3001\u534f\u7a0b\u5e38\u7528\u6280\u5de7<\/p>\n<ol>\n<li>\u5e76\u53d1\u4efb\u52a1\u7684\u5904\u7406<\/li>\n<\/ol>\n<p>\u5728Swoole\u4e2d\uff0c\u4f7f\u7528swoole_coroutine_wait\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u591a\u4e2a\u534f\u7a0b\u7684\u5e76\u53d1\u6267\u884c\u548c\u540c\u6b65\u5904\u7406\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>$tasks = [\n    \"task1\" =&amp;gt; function () {\n        sleep(1);\n        return \"Task 1 Done\n\";\n    },\n    \"task2\" =&amp;gt; function () {\n        sleep(1);\n        return \"Task 2 Done\n\";\n    },\n];\n\n$results = [];\n\nforeach ($tasks as $name =&amp;gt; $task) {\n    $cid = swoole_coroutine_create(function () use ($name, $task, &amp;amp;$results) {\n        $result = $task();\n        $results[$name] = $result;\n    });\n    swoole_coroutine_resume($cid);\n}\n\nswoole_coroutine_wait();\nprint_r($results);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u5148\u5b9a\u4e49\u4e86\u4e24\u4e2a\u4efb\u52a1\uff0c\u6267\u884c\u4efb\u52a1\u7684\u51fd\u6570\u4e2d\u90fd\u52a0\u4e86sleep(1)\u6765\u6a21\u62df\u4efb\u52a1\u6267\u884c\u7684\u65f6\u95f4\uff0c\u7136\u540e\u4f7f\u7528foreach\u5faa\u73af\u521b\u5efa\u4e24\u4e2a\u534f\u7a0b\uff0c\u5c06\u6267\u884c\u7ed3\u679c\u4fdd\u5b58\u5728$results\u6570\u7ec4\u4e2d\uff0c\u6700\u540e\u8c03\u7528swoole_coroutine_wait\u51fd\u6570\u6765\u7b49\u5f85\u534f\u7a0b\u7684\u5b8c\u6210\uff0c\u83b7\u5f97\u6267\u884c\u7ed3\u679c\u3002<\/p>\n<ol>\n<li>IO\u4efb\u52a1\u7684\u5904\u7406<\/li>\n<\/ol>\n<p>\u5728Swoole\u4e2d\uff0c\u4f7f\u7528swoole_coroutine_system_exec\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u5f02\u6b65\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\u5e76\u8fd4\u56de\u7ed3\u679c\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>function async_exec($cmd)\n{\n    $fp = popen($cmd, \"r\");\n    if ($fp) {\n        while (!feof($fp)) {\n            yield fread($fp, 8192);\n        }\n        pclose($fp);\n    }\n}\n\n$s = microtime(true);\n$coroutine = async_exec('ls \/');\nforeach ($coroutine as $stdout) {\n    echo $stdout;\n}\n$e = microtime(true);\necho \"Time used: \" . ($e - $s) . \"s\n\";<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528async_exec\u51fd\u6570\u5f02\u6b65\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\uff0c\u5e76\u4f7f\u7528yield\u9010\u6b65\u8bfb\u53d6\u8f93\u51fa\u7ed3\u679c\uff0c\u6700\u540e\u8f93\u51fa\u547d\u4ee4\u6267\u884c\u7684\u65f6\u95f4\u3002<\/p>\n<ol>\n<li>\u8fde\u63a5\u6c60\u6280\u672f<\/li>\n<\/ol>\n<p>\u5728Swoole\u4e2d\uff0c\u5185\u7f6e\u4e86\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u901a\u8fc7swoole_mysql\u534f\u7a0b\u5ba2\u6237\u7aef\u7b80\u5316\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u5e76\u63d0\u9ad8\u5e76\u53d1\u6267\u884c\u6548\u7387\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>$pool = new SwooleCoroutineChannel(10);\nfor ($i = 0; $i connect([\n        'host' =&amp;gt; '127.0.0.1',\n        'user' =&amp;gt; 'root',\n        'password' =&amp;gt; '123456',\n        'database' =&amp;gt; 'test',\n    ]);\n    $pool-&amp;gt;push($conn);\n}\n\ngo(function () use ($pool) {\n    $conn = $pool-&amp;gt;pop();\n    $result = $conn-&amp;gt;query('select * from users');\n    \/\/...\n    $pool-&amp;gt;push($conn);\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u8fde\u63a5\u6c60\uff0c\u4f7f\u7528for\u5faa\u73af\u521b\u5efa10\u4e2aMySQL\u8fde\u63a5\u5e76\u52a0\u5165\u8fde\u63a5\u6c60\uff0c\u7136\u540e\u4f7f\u7528go\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u534f\u7a0b\uff0c\u5e76\u4ece\u8fde\u63a5\u6c60\u4e2d\u53d6\u51fa\u4e00\u4e2a\u8fde\u63a5\u6267\u884c\u6570\u636e\u5e93\u67e5\u8be2\u64cd\u4f5c\uff0c\u6700\u540e\u5c06\u8be5\u8fde\u63a5\u653e\u56de\u8fde\u63a5\u6c60\u3002\u8fde\u63a5\u6c60\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u51cf\u5c11\u8fde\u63a5\u521b\u5efa\u548c\u9500\u6bc1\u5e26\u6765\u7684\u5f00\u9500\uff0c\u63d0\u9ad8\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u6027\u80fd\u3002<\/p>\n<p>\u56db\u3001\u4f7f\u7528Swoole\u63d0\u5347\u5fae\u670d\u52a1\u5e94\u7528\u6027\u80fd\u7684\u5b9e\u4f8b<\/p>\n<p>Swoole\u7684\u534f\u7a0b\u7f16\u7a0b\u80fd\u529b\u975e\u5e38\u5f3a\uff0c\u80fd\u591f\u5e2e\u52a9\u5f00\u53d1\u8005\u9ad8\u6548\u5730\u7f16\u5199\u51fa\u9ad8\u6027\u80fd\u548c\u9ad8\u5e76\u53d1\u7684\u670d\u52a1\u7aef\u5e94\u7528\u7a0b\u5e8f\u3002\u4e0b\u9762\u4ee5\u5fae\u670d\u52a1\u5e94\u7528\u4e3a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528Swoole\u7684\u534f\u7a0b\u6280\u672f\u6765\u63d0\u5347\u5e94\u7528\u7684\u6027\u80fd\u3002<\/p>\n<ol>\n<li>\u5fae\u670d\u52a1\u6a21\u62df\u7a0b\u5e8f<\/li>\n<\/ol>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u5fae\u670d\u52a1\u7a0b\u5e8f\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php \/\/\u5fae\u670d\u52a11\nfunction service1($str)\n{\n    $result = file_get_contents(\"http:\/\/127.0.0.1:8888\/service2?str=\".$str);\n    return strtoupper($result);\n}\n\n\/\/\u5fae\u670d\u52a12\nfunction service2($str)\n{\n    return md5($str);\n}\n\n\/\/\u8def\u7531\u5904\u7406\n$uri = $_SERVER['REQUEST_URI'];\nif (preg_match(\"\/^\/service1?str=(.*)$\/\", $uri, $match)) {\n    echo service1($match[1]);\n} elseif (preg_match(\"\/^\/service2?str=(.*)$\/\", $uri, $match)) {\n    echo service2($match[1]);\n} else {\n    echo \"Unknown Request: \".$uri;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5fae\u670d\u52a1\u6a21\u62df\u7a0b\u5e8f\uff0c\u5206\u522b\u63d0\u4f9b\u4e86\u4e24\u4e2a\u670d\u52a1\uff1aservice1\u548cservice2\u3002\u670d\u52a11\u4f1a\u8c03\u7528\u670d\u52a12\u5e76\u5c06\u8fd4\u56de\u7ed3\u679c\u8f6c\u6362\u4e3a\u5927\u5199\u683c\u5f0f\uff0c\u670d\u52a12\u4f1a\u5bf9\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u8fdb\u884cMD5\u52a0\u5bc6\u5e76\u8fd4\u56de\u3002<\/p>\n<ol>\n<li>\u4f7f\u7528Swoole\u5b9e\u73b0\u5fae\u670d\u52a1\u7684\u534f\u7a0b\u7248\u672c<\/li>\n<\/ol>\n<p>\u4f7f\u7528Swoole\u5b9e\u73b0\u5fae\u670d\u52a1\u7684\u534f\u7a0b\u7248\u672c\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php $http = new SwooleHttpServer(\"0.0.0.0\", 8888);\n\n$http-&gt;on(\"request\", function ($request, $response) {\n    $uri = $request-&amp;gt;server['request_uri'];\n    if (preg_match(\"\/^\/service1?str=(.*)$\/\", $uri, $match)) {\n        $result = go(function () use ($match) {\n            $str = $match[1];\n            $result = await service2($str);\n            return strtoupper($result);\n        });\n        $response-&amp;gt;end($result);\n    } elseif (preg_match(\"\/^\/service2?str=(.*)$\/\", $uri, $match)) {\n        $result = go(function () use ($match) {\n            $str = $match[1];\n            $result = await service2($str);\n            return $result;\n        });\n        $response-&amp;gt;end($result);\n    } else {\n        $response-&amp;gt;end(\"Unknown Request: \".$uri);\n    }\n});\n\n$http-&amp;gt;start();\n\nasync function service1($str)\n{\n    $result = await service2($str);\n    return strtoupper($result);\n}\n\nasync function service2($str)\n{\n    \/\/\u6a21\u62df\u5f02\u6b65IO\u64cd\u4f5c\n    usleep(1000000);\n    return md5($str);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528Swoole\u7684HTTP\u534f\u7a0b\u670d\u52a1\u5668\u63d0\u4f9b\u5fae\u670d\u52a1\uff0c\u5bf9\u8bf7\u6c42\u7684URI\u8fdb\u884c\u89e3\u6790\u5e76\u8c03\u7528\u76f8\u5e94\u7684\u670d\u52a1\u3002\u5728\u670d\u52a11\u5904\u7406\u4e2d\uff0c\u8c03\u7528\u4e86service2\u670d\u52a1\u5e76\u5c06\u7ed3\u679c\u8fd4\u56de\uff0c\u4f46\u662f\u901a\u8fc7go\u51fd\u6570\u5c06\u8c03\u7528\u65b9\u6cd5\u5f02\u6b65\u5316\uff0c\u4f7f\u7528await\u5173\u952e\u5b57\u7b49\u5f85\u5f02\u6b65\u8c03\u7528\u7ed3\u679c\u3002\u5728\u5404\u5fae\u670d\u52a1\u5b9e\u73b0\u4e2d\uff0c\u4f7f\u7528\u4e86\u5f02\u6b65IO\u64cd\u4f5c\u6765\u6a21\u62df\u771f\u5b9e\u7684\u7f51\u7edcIO\uff0c\u4ee5\u66f4\u597d\u5730\u4f53\u73b0Swoole\u534f\u7a0b\u7684\u7279\u6027\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>Swoole\u63d0\u4f9b\u4e86\u975e\u5e38\u5f3a\u5927\u7684\u534f\u7a0b\u7f16\u7a0b\u80fd\u529b\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u7f16\u5199\u9ad8\u6548\u7684\u3001\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u901a\u4fe1\u7a0b\u5e8f\u3002\u5728\u5e94\u7528\u7a0b\u5e8f\u5b9e\u73b0\u4e2d\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u91c7\u7528\u534f\u7a0b\u6280\u672f\u6765\u7b80\u5316\u5f02\u6b65\u7f16\u7a0b\u3001\u63d0\u9ad8\u5e76\u53d1\u6267\u884c\u6548\u7387\u7b49\u3002\u5728\u4ee5\u4e0a\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Swoole\u5b9e\u73b0\u4e86\u4e00\u4e2a\u5fae\u670d\u52a1\u7684\u534f\u7a0b\u7248\u672c\uff0c\u5927\u5927\u63d0\u5347\u4e86\u5e94\u7528\u7684\u6027\u80fd\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5728\u5b9e\u9645\u5b9e\u73b0\u4e2d\uff0c\u8fd8\u9700\u8981\u6839\u636e\u5e94\u7528\u7684\u7279\u6027\u9009\u62e9\u5408\u9002\u7684\u5f02\u6b65IO\u64cd\u4f5c\u3001\u8fde\u63a5\u6c60\u7b49\u6280\u672f\uff0c\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSwoole\u5b9e\u6218\uff1a\u5982\u4f55\u4f7f\u7528\u534f\u7a0b\u63d0\u5347\u5e94\u7528\u7684\u6027\u80fd\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>Swoole\u5b9e\u6218\uff1a\u5982\u4f55\u4f7f\u7528\u534f\u7a0b\u63d0\u5347\u5e94\u7528\u7684\u6027\u80fd \u968f\u7740\u4e92\u8054\u7f51\u5e94\u7528\u8d8a\u6765\u8d8a\u590d\u6742\uff0c\u6027\u80fd\u6210\u4e3a\u4e86\u4e00\u4e2a\u8d8a\u6765\u8d8a\u91cd\u8981\u7684\u95ee\u9898\u3002\u800cSwoole\u4f5c\u4e3a\u4e00\u4e2a\u9762\u5411\u534f\u7a0b\u7684\u9ad8\u6027\u80fd\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u53ef\u4ee5\u5f88\u597d\u5730\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u672c\u6587\u5c06\u4ecb\u7ecdSwoole\u534f\u7a0b\u7684\u4e00\u4e9b\u57fa\u7840\u6982\u5ff5\uff0c\u5e76\u4ee5\u5b9e\u4f8b\u4e3a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528\u534f\u7a0b\u63d0\u5347\u5e94\u7528\u7684\u6027\u80fd\u3002 \u4e00\u3001\u4ec0\u4e48\u662f\u534f\u7a0b \u534f\u7a0b\uff08Coroutine\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u5728\u5355\u7ebf\u7a0b\u4e0a\u5b9e\u73b0\u591a\u4efb\u52a1\u534f\u4f5c\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u534f\u7a0b\u4e4b\u95f4\u81ea\u7531\u5207\u6362\u3002\u5728Swoole\u4e2d\uff0c\u534f\u7a0b\u53ef\u4ee5\u7b80\u5316\u5f02\u6b65\u7f16\u7a0b\u7684\u590d\u6742\u6027\uff0c\u901a\u8fc7\u534f\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u50cf\u7f16\u5199\u540c\u6b65\u4ee3\u7801\u4e00\u6837\u7f16\u5199\u5f02\u6b65\u4ee3\u7801\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002 \u4e8c\u3001\u534f\u7a0b\u7684\u57fa\u7840\u4f7f\u7528 \u5728Swoole\u4e2d\uff0c\u534f\u7a0b\u662f\u901a\u8fc7_coroutine_create\u51fd\u6570\u6765\u521b\u5efa\u7684\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a \/\/\u521b\u5efa\u534f\u7a0b $cid = swoole_coroutine_create(function(){ echo &#8220;Hello Coroutine &#8220;; }); \/\/\u82e5\u4e3b\u7ebf\u7a0b\u4e0d\u963b\u585e\uff0c\u5219\u534f\u7a0b\u65e0\u6cd5\u6267\u884c \u767b\u5f55\u540e\u590d\u5236 \u521b\u5efa\u534f\u7a0b\u540e\uff0c\u9700\u8981\u4f7f\u7528swoole_event_wait\u51fd\u6570\u6765\u7b49\u5f85\u534f\u7a0b\u7684\u6267\u884c\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a \/\/\u521b\u5efa\u534f\u7a0b $cid = swoole_coroutine_create(function(){ echo &#8220;Hello Coroutine &#8220;; }); \/\/\u7b49\u5f85\u534f\u7a0b\u6267\u884c swoole_event_wait(); \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u4ee3\u7801\u53ef\u4ee5\u8f93\u51fa&#8221;Hello Coroutine&#8221;\uff0c\u8bf4\u660e\u534f\u7a0b\u5df2\u7ecf\u6210\u529f\u6267\u884c\u3002 \u5728\u534f\u7a0b\u4e2d\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528swoole_coroutine_yield\u51fd\u6570\u6765\u5c06\u5f53\u524d\u534f\u7a0b\u8ba9\u51fa\uff0c\u8ba9\u5176\u4ed6\u534f\u7a0b\u6267\u884c\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a $cid1 = swoole_coroutine_create(function(){ echo &#8220;Coroutine 1 &#8220;; swoole_coroutine_yield(); echo &#8220;Coroutine 1 resume &#8220;; }); $cid2 = swoole_coroutine_create(function(){ echo &#8220;Coroutine 2 &#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-24992","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24992","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=24992"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24992\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}