{"id":23589,"date":"2024-11-21T12:33:32","date_gmt":"2024-11-21T04:33:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23589\/"},"modified":"2024-11-21T12:33:32","modified_gmt":"2024-11-21T04:33:32","slug":"swoole%e8%bf%9b%e9%98%b6%ef%bc%9a%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%8d%8f%e7%a8%8b%e8%bf%9b%e8%a1%8c%e9%ab%98%e5%b9%b6%e5%8f%91%e6%9f%a5%e8%af%a2%e4%bc%98%e5%8c%96","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23589\/","title":{"rendered":"Swoole\u8fdb\u9636\uff1a\u5982\u4f55\u4f7f\u7528\u534f\u7a0b\u8fdb\u884c\u9ad8\u5e76\u53d1\u67e5\u8be2\u4f18\u5316"},"content":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u7684\u4e0d\u65ad\u53d1\u5c55\u58ee\u5927\uff0c\u9ad8\u5e76\u53d1\u5904\u7406\u6210\u4e3a\u4e86\u6bcf\u4e2a\u4e92\u8054\u7f51\u516c\u53f8\u6280\u672f\u90e8\u95e8\u5fc5\u987b\u8981\u9762\u5bf9\u7684\u96be\u9898\u3002\u800c\u5728php\u9886\u57df\uff0c\u4f5c\u4e3a\u4e00\u6b3e\u534f\u7a0b\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u80fd\u591f\u6781\u5927\u5730\u63d0\u5347php\u7684\u53ef\u6269\u5c55\u6027\u548c\u6027\u80fd\u3002\u672c\u7bc7\u6587\u7ae0\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528swoole\u7684\u534f\u7a0b\u7279\u6027\u8fdb\u884c\u9ad8\u5e76\u53d1\u67e5\u8be2\u4f18\u5316\u3002<\/p>\n<p>\u4e00\u3001\u4ec0\u4e48\u662f\u534f\u7a0b\uff1f<\/p>\n<p>\u534f\u7a0b\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u4e5f\u79f0\u4e3a\u7528\u6237\u6001\u7ebf\u7a0b\u6216\u7eff\u8272\u7ebf\u7a0b\u3002\u901a\u4fd7\u70b9\u8bf4\uff0c\u534f\u7a0b\u5c31\u662f\u8fdb\u7a0b\u4e2d\u4e00\u6bb5\u53ef\u4ee5\u50cf\u7ebf\u7a0b\u4e00\u6837\u72ec\u7acb\u8fd0\u884c\u7684\u4ee3\u7801\u5757\u3002\u534f\u7a0b\u901a\u5e38\u4f1a\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u8c03\u5ea6\u6267\u884c\uff0c\u6bd4\u7ebf\u7a0b\u66f4\u8f7b\u91cf\u7ea7\uff0c\u4e5f\u66f4\u9ad8\u6548\u3002<\/p>\n<p>\u4e8c\u3001Swoole\u7684\u534f\u7a0b\u7279\u6027<\/p>\n<p>Swoole\u662fPHP\u8bed\u8a00\u5b9e\u73b0\u7684\u534f\u7a0b\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u5b83\u652f\u6301TCP\/UDP\/UnixSocket\u534f\u8bae\uff0c\u63d0\u4f9b\u4e86\u534f\u7a0b\u3001\u5f02\u6b65IO\u3001\u65f6\u95f4\u8f6e\u5b9a\u65f6\u5668\u3001\u5f02\u6b65\u4fe1\u53f7\u7b49\u5e93\uff0c\u80fd\u591f\u4ee5\u534f\u7a0b\u65b9\u5f0f\u5b9e\u73b0\u9ad8\u5e76\u53d1\u3001\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u901a\u4fe1\u670d\u52a1\u3002<\/p>\n<p>\u6846\u67b6\u5185\u7f6e\u534f\u7a0b\u8c03\u5ea6\u5668\uff0c\u53ef\u4ee5\u5728\u534f\u7a0b\u4e4b\u95f4\u975e\u5e38\u9ad8\u6548\u5730\u5207\u6362\uff0c\u540c\u65f6\u652f\u6301\u591a\u4e2a\u534f\u7a0b\u540c\u65f6\u6267\u884c\u3002\u5728Swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u8fdb\u884c\u9ad8\u5e76\u53d1\u67e5\u8be2\u5904\u7406\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u5b9e\u73b0\u5f02\u6b65\u975e\u963b\u585e\u67e5\u8be2\uff0c\u5e76\u5229\u7528\u534f\u7a0b\u7684\u9ad8\u6548\u5207\u6362\uff0c\u5728\u5355\u4e2a\u8fdb\u7a0b\u5185\u5904\u7406\u66f4\u591a\u7684\u5e76\u53d1\u8bf7\u6c42\u3002<\/p>\n<p>\u4e09\u3001\u534f\u7a0b\u9ad8\u5e76\u53d1\u67e5\u8be2\u4f18\u5316<\/p>\n<p>\u5728\u4e00\u822c\u7684PHP\u5e94\u7528\u4e2d\uff0c\u4f7f\u7528PDO\u548cMysqli\u7b49\u6570\u636e\u5e93\u6269\u5c55\u8fdb\u884c\u6570\u636e\u5e93\u67e5\u8be2\u64cd\u4f5c\u65f6\uff0c\u901a\u5e38\u91c7\u7528\u540c\u6b65\u963b\u585e\u7684\u65b9\u5f0f\uff0c\u4e00\u4e2a\u8bf7\u6c42\u5728\u6267\u884c\u67e5\u8be2\u65f6\u5fc5\u987b\u7b49\u5230\u67e5\u8be2\u5b8c\u6210\u624d\u4f1a\u7ee7\u7eed\u6267\u884c\u4e0b\u53bb\u3002\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\uff0c\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u5bfc\u81f4\u8bf7\u6c42\u6392\u961f\u7b49\u5f85\uff0c\u54cd\u5e94\u901f\u5ea6\u53d8\u6162\uff0c\u65e0\u6cd5\u6ee1\u8db3\u9ad8\u5e76\u53d1\u7684\u9700\u6c42\u3002<\/p>\n<p>\u800c\u901a\u8fc7\u4f7f\u7528Swoole\u7684\u534f\u7a0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u534f\u7a0b\u7684\u975e\u963b\u585e\u67e5\u8be2\u65b9\u5f0f\uff0c\u5728\u67e5\u8be2\u64cd\u4f5c\u8fdb\u884c\u7684\u540c\u65f6\uff0c\u534f\u7a0b\u53ef\u4ee5\u5207\u6362\u5230\u5176\u4ed6\u8bf7\u6c42\u6267\u884c\uff0c\u4ece\u800c\u5b9e\u73b0\u9ad8\u5e76\u53d1\u67e5\u8be2\u5f02\u6b65\u4f18\u5316\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php $db = new SwooleCoroutineMySQL();\n\n$res = $db-&gt;connect([\n    'host' =&amp;gt; 'localhost',\n    'port' =&amp;gt; 3306,\n    'user' =&amp;gt; 'root',\n    'password' =&amp;gt; '123456',\n    'database' =&amp;gt; 'test',\n]);\n\n$coroutine = [];\n$coroutine[] = function () use ($db) {\n    $result = $db-&amp;gt;query('SELECT * FROM user WHERE id = 1');\n    return $result;\n};\n$coroutine[] = function () use ($db) {\n    $result = $db-&amp;gt;query('SELECT * FROM user WHERE id = 2');\n    return $result;\n};\n\n$result = [];\nforeach($coroutine as $c) {\n    $result[] = $c();\n}\n\nvar_dump($result);\n\n?&amp;gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Swoole\u7684\u534f\u7a0bMySQL\u5ba2\u6237\u7aef\u8fdb\u884c\u5f02\u6b65\u67e5\u8be2\u3002\u4f7f\u7528\u591a\u4e2a\u534f\u7a0b\u8fdb\u884c\u9ad8\u5e76\u53d1\u67e5\u8be2\u64cd\u4f5c\uff0c\u6bcf\u4e2a\u534f\u7a0b\u6267\u884c\u67e5\u8be2\u65f6\uff0c\u4f1a\u5c06\u67e5\u8be2\u8bed\u53e5\u53d1\u9001\u5230MySQL\u670d\u52a1\u5668\uff0c\u7136\u540e\u7acb\u5373\u5c06\u63a7\u5236\u6743\u8fd8\u7ed9\u534f\u7a0b\u8c03\u5ea6\u5668\uff0c\u4f7f\u5f97\u5176\u4ed6\u534f\u7a0b\u7684\u6267\u884c\u673a\u4f1a\u66f4\u591a\uff0c\u4ece\u800c\u5b9e\u73b0\u9ad8\u5e76\u53d1\u4f18\u5316\u3002<\/p>\n<p>\u56db\u3001\u603b\u7ed3<\/p>\n<p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u8bfb\u8005\u5e94\u8be5\u4e86\u89e3\u4e86Swoole\u7684\u534f\u7a0b\u7279\u6027\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528Swoole\u8fdb\u884c\u9ad8\u5e76\u53d1\u67e5\u8be2\u4f18\u5316\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7ed3\u5408Swoole\u7684\u534f\u7a0b\u7279\u6027\uff0c\u5b9e\u73b0\u66f4\u9ad8\u6548\u7684\u670d\u52a1\u5668\u7aef\u7a0b\u5e8f\u3002\u867d\u7136Swoole\u5728\u5904\u7406\u9ad8\u5e76\u53d1\u8bf7\u6c42\u65b9\u9762\u6709\u5f88\u5927\u7684\u4f18\u52bf\uff0c\u4f46\u662f\u5728\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u6839\u636e\u81ea\u8eab\u4e1a\u52a1\u573a\u666f\u548c\u9700\u6c42\u6765\u9009\u62e9\u6700\u9002\u5408\u81ea\u5df1\u7684\u6280\u672f\u65b9\u6848\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSwoole\u8fdb\u9636\uff1a\u5982\u4f55\u4f7f\u7528\u534f\u7a0b\u8fdb\u884c\u9ad8\u5e76\u53d1\u67e5\u8be2\u4f18\u5316\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\u7684\u4e0d\u65ad\u53d1\u5c55\u58ee\u5927\uff0c\u9ad8\u5e76\u53d1\u5904\u7406\u6210\u4e3a\u4e86\u6bcf\u4e2a\u4e92\u8054\u7f51\u516c\u53f8\u6280\u672f\u90e8\u95e8\u5fc5\u987b\u8981\u9762\u5bf9\u7684\u96be\u9898\u3002\u800c\u5728php\u9886\u57df\uff0c\u4f5c\u4e3a\u4e00\u6b3e\u534f\u7a0b\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u80fd\u591f\u6781\u5927\u5730\u63d0\u5347php\u7684\u53ef\u6269\u5c55\u6027\u548c\u6027\u80fd\u3002\u672c\u7bc7\u6587\u7ae0\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528swoole\u7684\u534f\u7a0b\u7279\u6027\u8fdb\u884c\u9ad8\u5e76\u53d1\u67e5\u8be2\u4f18\u5316\u3002 \u4e00\u3001\u4ec0\u4e48\u662f\u534f\u7a0b\uff1f \u534f\u7a0b\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u4e5f\u79f0\u4e3a\u7528\u6237\u6001\u7ebf\u7a0b\u6216\u7eff\u8272\u7ebf\u7a0b\u3002\u901a\u4fd7\u70b9\u8bf4\uff0c\u534f\u7a0b\u5c31\u662f\u8fdb\u7a0b\u4e2d\u4e00\u6bb5\u53ef\u4ee5\u50cf\u7ebf\u7a0b\u4e00\u6837\u72ec\u7acb\u8fd0\u884c\u7684\u4ee3\u7801\u5757\u3002\u534f\u7a0b\u901a\u5e38\u4f1a\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u8c03\u5ea6\u6267\u884c\uff0c\u6bd4\u7ebf\u7a0b\u66f4\u8f7b\u91cf\u7ea7\uff0c\u4e5f\u66f4\u9ad8\u6548\u3002 \u4e8c\u3001Swoole\u7684\u534f\u7a0b\u7279\u6027 Swoole\u662fPHP\u8bed\u8a00\u5b9e\u73b0\u7684\u534f\u7a0b\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u5b83\u652f\u6301TCP\/UDP\/UnixSocket\u534f\u8bae\uff0c\u63d0\u4f9b\u4e86\u534f\u7a0b\u3001\u5f02\u6b65IO\u3001\u65f6\u95f4\u8f6e\u5b9a\u65f6\u5668\u3001\u5f02\u6b65\u4fe1\u53f7\u7b49\u5e93\uff0c\u80fd\u591f\u4ee5\u534f\u7a0b\u65b9\u5f0f\u5b9e\u73b0\u9ad8\u5e76\u53d1\u3001\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u901a\u4fe1\u670d\u52a1\u3002 \u6846\u67b6\u5185\u7f6e\u534f\u7a0b\u8c03\u5ea6\u5668\uff0c\u53ef\u4ee5\u5728\u534f\u7a0b\u4e4b\u95f4\u975e\u5e38\u9ad8\u6548\u5730\u5207\u6362\uff0c\u540c\u65f6\u652f\u6301\u591a\u4e2a\u534f\u7a0b\u540c\u65f6\u6267\u884c\u3002\u5728Swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u8fdb\u884c\u9ad8\u5e76\u53d1\u67e5\u8be2\u5904\u7406\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u5b9e\u73b0\u5f02\u6b65\u975e\u963b\u585e\u67e5\u8be2\uff0c\u5e76\u5229\u7528\u534f\u7a0b\u7684\u9ad8\u6548\u5207\u6362\uff0c\u5728\u5355\u4e2a\u8fdb\u7a0b\u5185\u5904\u7406\u66f4\u591a\u7684\u5e76\u53d1\u8bf7\u6c42\u3002 \u4e09\u3001\u534f\u7a0b\u9ad8\u5e76\u53d1\u67e5\u8be2\u4f18\u5316 \u5728\u4e00\u822c\u7684PHP\u5e94\u7528\u4e2d\uff0c\u4f7f\u7528PDO\u548cMysqli\u7b49\u6570\u636e\u5e93\u6269\u5c55\u8fdb\u884c\u6570\u636e\u5e93\u67e5\u8be2\u64cd\u4f5c\u65f6\uff0c\u901a\u5e38\u91c7\u7528\u540c\u6b65\u963b\u585e\u7684\u65b9\u5f0f\uff0c\u4e00\u4e2a\u8bf7\u6c42\u5728\u6267\u884c\u67e5\u8be2\u65f6\u5fc5\u987b\u7b49\u5230\u67e5\u8be2\u5b8c\u6210\u624d\u4f1a\u7ee7\u7eed\u6267\u884c\u4e0b\u53bb\u3002\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\uff0c\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u5bfc\u81f4\u8bf7\u6c42\u6392\u961f\u7b49\u5f85\uff0c\u54cd\u5e94\u901f\u5ea6\u53d8\u6162\uff0c\u65e0\u6cd5\u6ee1\u8db3\u9ad8\u5e76\u53d1\u7684\u9700\u6c42\u3002 \u800c\u901a\u8fc7\u4f7f\u7528Swoole\u7684\u534f\u7a0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u534f\u7a0b\u7684\u975e\u963b\u585e\u67e5\u8be2\u65b9\u5f0f\uff0c\u5728\u67e5\u8be2\u64cd\u4f5c\u8fdb\u884c\u7684\u540c\u65f6\uff0c\u534f\u7a0b\u53ef\u4ee5\u5207\u6362\u5230\u5176\u4ed6\u8bf7\u6c42\u6267\u884c\uff0c\u4ece\u800c\u5b9e\u73b0\u9ad8\u5e76\u53d1\u67e5\u8be2\u5f02\u6b65\u4f18\u5316\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a &lt;?php $db = new SwooleCoroutineMySQL(); $res = $db-&gt;connect([ &#8216;host&#8217; =&amp;gt; &#8216;localhost&#8217;, &#8216;port&#8217; =&amp;gt; 3306, &#8216;user&#8217; =&amp;gt; &#8216;root&#8217;, &#8216;password&#8217; =&amp;gt; &#8216;123456&#8217;, &#8216;database&#8217; =&amp;gt; &#8216;test&#8217;, ]); $coroutine = []; $coroutine[] = function () use ($db) { $result = $db-&amp;gt;query(&#8216;SELECT * FROM user WHERE id = 1&#8217;); return $result; [&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-23589","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23589","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=23589"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23589\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}