{"id":24935,"date":"2024-11-21T08:31:47","date_gmt":"2024-11-21T00:31:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24935\/"},"modified":"2024-11-21T08:31:47","modified_gmt":"2024-11-21T00:31:47","slug":"swoole%e5%bc%80%e5%8f%91%e6%8a%80%e5%b7%a7%ef%bc%9a%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86%e9%ab%98%e5%b9%b6%e5%8f%91%e7%9a%84%e6%95%b0%e6%8d%ae%e5%ba%93%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24935\/","title":{"rendered":"Swoole\u5f00\u53d1\u6280\u5de7\uff1a\u5982\u4f55\u5904\u7406\u9ad8\u5e76\u53d1\u7684\u6570\u636e\u5e93\u64cd\u4f5c"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169931642760187.jpg\" class=\"aligncenter\" title=\"Swoole\u5f00\u53d1\u6280\u5de7\uff1a\u5982\u4f55\u5904\u7406\u9ad8\u5e76\u53d1\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u63d2\u56fe\" alt=\"Swoole\u5f00\u53d1\u6280\u5de7\uff1a\u5982\u4f55\u5904\u7406\u9ad8\u5e76\u53d1\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u63d2\u56fe\" \/><\/p>\n<p>Swoole\u5f00\u53d1\u6280\u5de7\uff1a\u5982\u4f55\u5904\u7406\u9ad8\u5e76\u53d1\u7684\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u9700\u8981\u5177\u4f53\u4ee3\u7801\u793a\u4f8b<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>\u5728\u5f53\u4eca\u4e92\u8054\u7f51\u9ad8\u901f\u53d1\u5c55\u7684\u65f6\u4ee3\uff0c\u9ad8\u5e76\u53d1\u662f\u5404\u4e2a\u7cfb\u7edf\u67b6\u6784\u4e2d\u4e0d\u53ef\u907f\u514d\u7684\u6311\u6218\u3002\u5bf9\u4e8e\u4f7f\u7528Swoole\u8fdb\u884c\u5f00\u53d1\u7684\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u5982\u4f55\u5904\u7406\u9ad8\u5e76\u53d1\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u4e00\u4e9bSwoole\u5f00\u53d1\u4e2d\u5904\u7406\u9ad8\u5e76\u53d1\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u6280\u5de7\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u8fde\u63a5\u6c60\u7ba1\u7406<br \/>\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\uff0c\u9891\u7e41\u5730\u521b\u5efa\u548c\u9500\u6bc1\u6570\u636e\u5e93\u8fde\u63a5\u662f\u5f88\u8017\u8d39\u8d44\u6e90\u7684\u3002\u56e0\u6b64\uff0c\u4f7f\u7528\u8fde\u63a5\u6c60\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u4f18\u5316\u65b9\u5f0f\u3002\u901a\u8fc7\u8fde\u63a5\u6c60\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u53ef\u4ee5\u590d\u7528\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u51cf\u5c11\u8fde\u63a5\u7684\u521b\u5efa\u548c\u9500\u6bc1\u6b21\u6570\uff0c\u63d0\u9ad8\u7cfb\u7edf\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Swoole\u5b9e\u73b0\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>class ConnectionPool\n{\n    private static $instance;\n    private $connections = [];\n\n    private function __construct() {}\n    private function __clone() {}\n\n    public static function getInstance()\n    {\n        if (!self::$instance instanceof self) {\n            self::$instance = new self();\n        }\n        return self::$instance;\n    }\n\n    public function getConnection()\n    {\n        if (!empty($this-&amp;gt;connections)) {\n            return array_pop($this-&amp;gt;connections);\n        } else {\n            return $this-&amp;gt;createConnection();\n        }\n    }\n\n    public function releaseConnection($connection)\n    {\n        array_push($this-&amp;gt;connections, $connection);\n    }\n\n    private function createConnection()\n    {\n        $connection = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');\n        return $connection;\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cConnectionPool\u7c7b\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u5b9e\u73b0\u4e86\u4e00\u4e2a\u8fde\u63a5\u6c60\uff0cgetInstance()\u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6ConnectionPool\u7684\u5b9e\u4f8b\u3002getConnection()\u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6\u4e00\u4e2a\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u5982\u679c\u8fde\u63a5\u6c60\u4e2d\u5b58\u5728\u53ef\u7528\u8fde\u63a5\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u8be5\u8fde\u63a5\uff0c\u5426\u5219\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8fde\u63a5\u3002releaseConnection()\u65b9\u6cd5\u7528\u4e8e\u91ca\u653e\u8fde\u63a5\uff0c\u5c06\u8fde\u63a5\u653e\u56de\u8fde\u63a5\u6c60\u4e2d\u4f9b\u540e\u7eed\u4f7f\u7528\u3002<\/p>\n<p>\u4e8c\u3001\u534f\u7a0b<br \/>\u5728Swoole\u4e2d\uff0c\u534f\u7a0b\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u5728\u4e0d\u5f71\u54cd\u7a0b\u5e8f\u6b63\u5e38\u6267\u884c\u7684\u524d\u63d0\u4e0b\u5b9e\u73b0\u5e76\u53d1\u6267\u884c\u3002\u4f7f\u7528\u534f\u7a0b\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u80fd\u529b\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Swoole\u7684\u534f\u7a0b\u5b9e\u73b0\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>go(function() {\n    $pdo = ConnectionPool::getInstance()-&amp;gt;getConnection();\n    $stmt = $pdo-&amp;gt;prepare('SELECT * FROM users WHERE id = ?');\n    $stmt-&amp;gt;execute([1]);\n    $result = $stmt-&amp;gt;fetchAll(PDO::FETCH_ASSOC);\n\n    \/\/ \u4e1a\u52a1\u903b\u8f91\u64cd\u4f5c...\n    \n    ConnectionPool::getInstance()-&amp;gt;releaseConnection($pdo);\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528go(function(){})\u521b\u5efa\u4e00\u4e2a\u534f\u7a0b\uff0c\u5728\u534f\u7a0b\u5185\u90e8\u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\u3001\u6267\u884c\u67e5\u8be2\u64cd\u4f5c\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u4e1a\u52a1\u903b\u8f91\u5904\u7406\u3002\u6700\u540e\uff0c\u901a\u8fc7releaseConnection()\u65b9\u6cd5\u5c06\u8fde\u63a5\u91ca\u653e\u56de\u8fde\u63a5\u6c60\u3002<\/p>\n<p>\u4e09\u3001\u5f02\u6b65IO<br \/>\u5bf9\u4e8e\u9ad8\u5e76\u53d1\u7684\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u4f7f\u7528\u5f02\u6b65IO\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u7cfb\u7edf\u7684\u6027\u80fd\u3002Swoole\u63d0\u4f9b\u4e86\u5f02\u6b65IO\u7684\u80fd\u529b\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5e93\u64cd\u4f5c\u8f6c\u5316\u4e3a\u5f02\u6b65\u6a21\u5f0f\uff0c\u4ee5\u63d0\u9ad8\u7cfb\u7edf\u7684\u541e\u5410\u91cf\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Swoole\u7684\u5f02\u6b65IO\u5b9e\u73b0\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>go(function() {\n    $mysql = new SwooleCoroutineMySQL();\n    $pdo = ConnectionPool::getInstance()-&amp;gt;getConnection();\n    $mysql-&amp;gt;connect([\n        'host' =&amp;gt; 'localhost',\n        'user' =&amp;gt; 'username',\n        'password' =&amp;gt; 'password',\n        'database' =&amp;gt; 'test'\n    ]);\n    $result = $mysql-&amp;gt;query('SELECT * FROM users WHERE id = 1');\n\n    \/\/ \u4e1a\u52a1\u903b\u8f91\u64cd\u4f5c...\n\n    ConnectionPool::getInstance()-&amp;gt;releaseConnection($pdo);\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u521b\u5efa\u4e00\u4e2aSwooleCoroutineMySQL\u5b9e\u4f8b\uff0c\u5e76\u901a\u8fc7connect()\u65b9\u6cd5\u8fde\u63a5\u6570\u636e\u5e93\u3002\u63a5\u7740\u4f7f\u7528query()\u65b9\u6cd5\u6267\u884c\u5f02\u6b65\u7684\u67e5\u8be2\u64cd\u4f5c\uff0c\u5e76\u5728\u67e5\u8be2\u7ed3\u679c\u8fd4\u56de\u540e\u8fdb\u884c\u76f8\u5e94\u7684\u4e1a\u52a1\u903b\u8f91\u5904\u7406\u3002\u6700\u540e\uff0c\u901a\u8fc7releaseConnection()\u65b9\u6cd5\u5c06\u8fde\u63a5\u91ca\u653e\u56de\u8fde\u63a5\u6c60\u3002<\/p>\n<p>\u7ed3\u8bed\uff1a<br \/>\u901a\u8fc7\u8fde\u63a5\u6c60\u7ba1\u7406\u3001\u534f\u7a0b\u548c\u5f02\u6b65IO\u7b49\u6280\u672f\u624b\u6bb5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f18\u5316Swoole\u5f00\u53d1\u4e2d\u7684\u9ad8\u5e76\u53d1\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u5e0c\u671b\u672c\u6587\u63d0\u4f9b\u7684\u6280\u5de7\u548c\u4ee3\u7801\u793a\u4f8b\u5bf9\u4e8e\u8bfb\u8005\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u80fd\u6709\u6240\u5e2e\u52a9\u3002\u540c\u65f6\uff0c\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u8c03\u4f18\u548c\u4f18\u5316\uff0c\u4ee5\u5b9e\u73b0\u66f4\u597d\u7684\u6027\u80fd\u548c\u53ef\u9760\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSwoole\u5f00\u53d1\u6280\u5de7\uff1a\u5982\u4f55\u5904\u7406\u9ad8\u5e76\u53d1\u7684\u6570\u636e\u5e93\u64cd\u4f5c\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\u5f00\u53d1\u6280\u5de7\uff1a\u5982\u4f55\u5904\u7406\u9ad8\u5e76\u53d1\u7684\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u9700\u8981\u5177\u4f53\u4ee3\u7801\u793a\u4f8b \u5f15\u8a00\uff1a\u5728\u5f53\u4eca\u4e92\u8054\u7f51\u9ad8\u901f\u53d1\u5c55\u7684\u65f6\u4ee3\uff0c\u9ad8\u5e76\u53d1\u662f\u5404\u4e2a\u7cfb\u7edf\u67b6\u6784\u4e2d\u4e0d\u53ef\u907f\u514d\u7684\u6311\u6218\u3002\u5bf9\u4e8e\u4f7f\u7528Swoole\u8fdb\u884c\u5f00\u53d1\u7684\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u5982\u4f55\u5904\u7406\u9ad8\u5e76\u53d1\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u4e00\u4e9bSwoole\u5f00\u53d1\u4e2d\u5904\u7406\u9ad8\u5e76\u53d1\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u6280\u5de7\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u8fde\u63a5\u6c60\u7ba1\u7406\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\uff0c\u9891\u7e41\u5730\u521b\u5efa\u548c\u9500\u6bc1\u6570\u636e\u5e93\u8fde\u63a5\u662f\u5f88\u8017\u8d39\u8d44\u6e90\u7684\u3002\u56e0\u6b64\uff0c\u4f7f\u7528\u8fde\u63a5\u6c60\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u4f18\u5316\u65b9\u5f0f\u3002\u901a\u8fc7\u8fde\u63a5\u6c60\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u53ef\u4ee5\u590d\u7528\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u51cf\u5c11\u8fde\u63a5\u7684\u521b\u5efa\u548c\u9500\u6bc1\u6b21\u6570\uff0c\u63d0\u9ad8\u7cfb\u7edf\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u3002 \u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Swoole\u5b9e\u73b0\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u7684\u793a\u4f8b\u4ee3\u7801\uff1a class ConnectionPool { private static $instance; private $connections = []; private function __construct() {} private function __clone() {} public static function getInstance() { if (!self::$instance instanceof self) { self::$instance = new self(); } return self::$instance; } public function getConnection() { if (!empty($this-&amp;gt;connections)) { return array_pop($this-&amp;gt;connections); } else { return $this-&amp;gt;createConnection(); } [&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-24935","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24935","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=24935"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24935\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}