{"id":47305,"date":"2024-12-02T10:24:43","date_gmt":"2024-12-02T02:24:43","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47305\/"},"modified":"2024-12-02T10:24:43","modified_gmt":"2024-12-02T02:24:43","slug":"%e5%9c%a8-swoole-%e5%8d%8f%e7%a8%8b%e4%b8%ad%e6%93%8d%e4%bd%9c%e5%8f%98%e9%87%8f%e6%97%b6%ef%bc%8c%e6%98%af%e5%90%a6%e9%9c%80%e8%a6%81%e5%8a%a0%e9%94%81%e4%bb%a5%e7%a1%ae%e4%bf%9d%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47305\/","title":{"rendered":"\u5728 Swoole \u534f\u7a0b\u4e2d\u64cd\u4f5c\u53d8\u91cf\u65f6\uff0c\u662f\u5426\u9700\u8981\u52a0\u9501\u4ee5\u786e\u4fdd\u53d8\u91cf\u7684\u5b89\u5168\u6027\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5728 Swoole \u534f\u7a0b\u4e2d\u64cd\u4f5c\u53d8\u91cf\u65f6\uff0c\u662f\u5426\u9700\u8981\u52a0\u9501\u4ee5\u786e\u4fdd\u53d8\u91cf\u7684\u5b89\u5168\u6027\uff1f<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u600e\u4e48\u5165\u95e8\u6587\u7ae0\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u7c73\u4e91\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300a\u5728 Swoole \u534f\u7a0b\u4e2d\u64cd\u4f5c\u53d8\u91cf\u65f6\uff0c\u662f\u5426\u9700\u8981\u52a0\u9501\u4ee5\u786e\u4fdd\u53d8\u91cf\u7684\u5b89\u5168\u6027\uff1f\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729910019671c550347eaf.jpg\" class=\"aligncenter\" title=\"\u5728 Swoole \u534f\u7a0b\u4e2d\u64cd\u4f5c\u53d8\u91cf\u65f6\uff0c\u662f\u5426\u9700\u8981\u52a0\u9501\u4ee5\u786e\u4fdd\u53d8\u91cf\u7684\u5b89\u5168\u6027\uff1f\u63d2\u56fe\" alt=\"\u5728 Swoole \u534f\u7a0b\u4e2d\u64cd\u4f5c\u53d8\u91cf\u65f6\uff0c\u662f\u5426\u9700\u8981\u52a0\u9501\u4ee5\u786e\u4fdd\u53d8\u91cf\u7684\u5b89\u5168\u6027\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u534f\u7a0b\u64cd\u4f5c\u53d8\u91cf\u65f6\u662f\u5426\u9700\u8981\u52a0\u9501\uff1a\u5b89\u5168\u6027\u5206\u6790<\/strong><\/p>\n<p>\u5728 swoole \u534f\u7a0b\u4e2d\uff0c\u591a\u4e2a\u534f\u7a0b\u7684\u64cd\u4f5c\u540c\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u9700\u8981\u52a0\u9501\uff0c\u4e00\u76f4\u662f\u5f00\u53d1\u8005\u5173\u5fc3\u7684\u95ee\u9898\u3002<\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>use Swoole\\Runtime;\nuse Swoole\\Coroutine;\nuse Swoole\\Coroutine\\WaitGroup;\n\n\/\/ \u534f\u7a0b\u53d8\u91cf\u5b89\u5168\n\nRuntime::enableCoroutine();\n\n\\Swoole\\Coroutine\\run(function () {\n    $wg = new WaitGroup();\n    $count = 0;\n    foreach (range(1, 20000) as $num) {\n        $wg-&gt;add();\n        Coroutine::create(function () use ($wg, &amp;$count) {\n            Coroutine::sleep(rand(2, 5));\n            \/\/ var_dump(Coroutine::getuid());\n            $count += 1;\n            $wg-&gt;done();\n        });\n    }\n    $wg-&gt;wait();\n    var_dump($count);\n});<\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u591a\u4e2a\u534f\u7a0b\u540c\u65f6\u4fee\u6539\u540c\u4e00\u4e2a\u53d8\u91cf $count\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u8003\u8651\u662f\u5426\u9700\u8981\u52a0\u9501\u4ee5\u786e\u4fdd\u53d8\u91cf\u7684\u5b89\u5168\u3002<\/p>\n<p><strong>\u534f\u7a0b\u7684\u53d8\u91cf\u5b89\u5168\u6027<\/strong><\/p>\n<p>\u4e0e\u591a\u7ebf\u7a0b\u4e0d\u540c\uff0cswoole \u534f\u7a0b\u63d0\u4f9b\u4e86\u53d8\u91cf\u7684\u5b89\u5168\u6027\u4fdd\u8bc1\u3002\u8fd9\u610f\u5473\u7740\u5728\u534f\u7a0b\u73af\u5883\u4e2d\uff0c\u4e0d\u9700\u8981\u5bf9\u53d8\u91cf\u8fdb\u884c\u52a0\u9501\u64cd\u4f5c\u3002<\/p>\n<p>\u534f\u7a0b\u7684\u53d8\u91cf\u5b89\u5168\u673a\u5236\u662f\u57fa\u4e8e\u534f\u7a0b\u7684\u72ec\u7acb\u6267\u884c\u73af\u5883\u3002\u6bcf\u4e2a\u534f\u7a0b\u90fd\u6709\u81ea\u5df1\u7684\u6808\u548c\u5bc4\u5b58\u5668\uff0c\u8fd9\u786e\u4fdd\u4e86\u4e0d\u540c\u534f\u7a0b\u4e2d\u7684\u53d8\u91cf\u4e0d\u4f1a\u76f8\u4e92\u5f71\u54cd\u3002\u56e0\u6b64\uff0c\u5373\u4f7f\u591a\u4e2a\u534f\u7a0b\u540c\u65f6\u4fee\u6539\u540c\u4e00\u4e2a\u53d8\u91cf\uff0c\u4e5f\u4e0d\u4f1a\u51fa\u73b0\u53d8\u91cf\u5b89\u5168\u95ee\u9898\u3002<\/p>\n<p><strong>\u539f\u5b50\u6027\u64cd\u4f5c\u4f8b\u5916<\/strong><\/p>\n<p>\u7136\u800c\uff0c\u5bf9\u4e8e\u539f\u5b50\u6027\u64cd\u4f5c\uff0c\u534f\u7a0b\u7684\u53d8\u91cf\u5b89\u5168\u6027\u4e0d\u9002\u7528\u3002\u4f8b\u5982\uff0c\u5982\u679c $count \u7684\u81ea\u589e\u64cd\u4f5c\u88ab\u5b9e\u73b0\u4e3a\u4e00\u4e2a\u539f\u5b50\u6027\u64cd\u4f5c\uff0c\u90a3\u4e48\u4ecd\u7136\u9700\u8981\u52a0\u9501\u4ee5\u786e\u4fdd\u64cd\u4f5c\u7684\u539f\u5b50\u6027\u3002<\/p>\n<p>\u5728\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u81ea\u589e\u64cd\u4f5c $count += 1 \u5e76\u4e0d\u662f\u4e00\u4e2a\u539f\u5b50\u6027\u64cd\u4f5c\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u52a0\u9501\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u5728 swoole \u534f\u7a0b\u4e2d\uff0c\u5bf9\u4e8e\u666e\u901a\u7684\u53d8\u91cf\u64cd\u4f5c\uff0c\u4e0d\u9700\u8981\u52a0\u9501\u4ee5\u786e\u4fdd\u53d8\u91cf\u7684\u5b89\u5168\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u539f\u5b50\u6027\u64cd\u4f5c\uff0c\u4ecd\u7136\u9700\u8981\u52a0\u9501\u4ee5\u4fdd\u8bc1\u64cd\u4f5c\u7684\u539f\u5b50\u6027\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5728 Swoole \u534f\u7a0b\u4e2d\u64cd\u4f5c\u53d8\u91cf\u65f6\uff0c\u662f\u5426\u9700\u8981\u52a0\u9501\u4ee5\u786e\u4fdd\u53d8\u91cf\u7684\u5b89\u5168\u6027\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n<p> \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1aphp \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Swoole \u534f\u7a0b\u4e2d\u64cd\u4f5c\u53d8\u91cf\u65f6\uff0c\u662f\u5426\u9700\u8981\u52a0\u9501\u4ee5\u786e\u4fdd\u53d8\u91cf\u7684\u5b89\u5168\u6027\uff1f \u6536\u85cf \u600e\u4e48\u5165\u95e8\u6587\u7ae0\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u7c73\u4e91\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300a\u5728 Swoole \u534f\u7a0b\u4e2d\u64cd\u4f5c\u53d8\u91cf\u65f6\uff0c\u662f\u5426\u9700\u8981\u52a0\u9501\u4ee5\u786e\u4fdd\u53d8\u91cf\u7684\u5b89\u5168\u6027\uff1f\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b \u534f\u7a0b\u64cd\u4f5c\u53d8\u91cf\u65f6\u662f\u5426\u9700\u8981\u52a0\u9501\uff1a\u5b89\u5168\u6027\u5206\u6790 \u5728 swoole \u534f\u7a0b\u4e2d\uff0c\u591a\u4e2a\u534f\u7a0b\u7684\u64cd\u4f5c\u540c\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u9700\u8981\u52a0\u9501\uff0c\u4e00\u76f4\u662f\u5f00\u53d1\u8005\u5173\u5fc3\u7684\u95ee\u9898\u3002 \u793a\u4f8b\u4ee3\u7801 \u8003\u8651\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\uff1a use Swoole\\Runtime; use Swoole\\Coroutine; use Swoole\\Coroutine\\WaitGroup; \/\/ \u534f\u7a0b\u53d8\u91cf\u5b89\u5168 Runtime::enableCoroutine(); \\Swoole\\Coroutine\\run(function () { $wg = new WaitGroup(); $count = 0; foreach (range(1, 20000) as $num) { $wg-&gt;add(); Coroutine::create(function () use ($wg, &amp;$count) { Coroutine::sleep(rand(2, 5)); \/\/ var_dump(Coroutine::getuid()); $count += 1; $wg-&gt;done(); }); } [&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-47305","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47305","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=47305"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47305\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}