{"id":24968,"date":"2024-11-21T08:05:34","date_gmt":"2024-11-21T00:05:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24968\/"},"modified":"2024-11-21T08:05:34","modified_gmt":"2024-11-21T00:05:34","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8swoole%e5%ae%9e%e7%8e%b0%e5%a4%9a%e8%bf%9b%e7%a8%8b%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24968\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Swoole\u5b9e\u73b0\u591a\u8fdb\u7a0b\u5e76\u53d1\u7f16\u7a0b"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/465\/014\/169932676618709.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528Swoole\u5b9e\u73b0\u591a\u8fdb\u7a0b\u5e76\u53d1\u7f16\u7a0b\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528Swoole\u5b9e\u73b0\u591a\u8fdb\u7a0b\u5e76\u53d1\u7f16\u7a0b\u63d2\u56fe\" \/><\/p>\n<p>Swoole\u662fPHP\u7684\u4e00\u4e2a\u9ad8\u6027\u80fd\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u5e76\u53d1\u7f16\u7a0b\u3002\u5176\u4e2d\u6700\u91cd\u8981\u7684\u4e00\u9879\u7279\u6027\u662f\u5b83\u5bf9\u591a\u8fdb\u7a0b\u7684\u652f\u6301\uff0c\u53ef\u4ee5\u8ba9\u6211\u4eec\u901a\u8fc7\u591a\u8fdb\u7a0b\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684\u7f51\u7edc\u7f16\u7a0b\u3002<\/p>\n<p>\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Swoole\u5b9e\u73b0\u591a\u8fdb\u7a0b\u5e76\u53d1\u7f16\u7a0b\uff0c\u5305\u62ec\u591a\u8fdb\u7a0b\u7684\u521b\u5efa\u3001\u901a\u4fe1\u3001\u540c\u6b65\u7b49\u65b9\u9762\uff0c\u5e76\u4e14\u4f1a\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<ol>\n<li>\u591a\u8fdb\u7a0b\u7684\u521b\u5efa<br \/>\u5728Swoole\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528_process\u7c7b\u6765\u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/li>\n<\/ol>\n<pre>$process = new swoole_process(function(swoole_process $process) {\n    \/\/ \u5b50\u8fdb\u7a0b\u7684\u903b\u8f91\u4ee3\u7801\n    $process-&amp;gt;write(\"Hello world!\n\"); \/\/ \u5411\u4e3b\u8fdb\u7a0b\u53d1\u9001\u6d88\u606f\n    $process-&amp;gt;exit();\n});\n\n$process-&amp;gt;start();\n\n\/\/ \u7236\u8fdb\u7a0b\u63a5\u6536\u5b50\u8fdb\u7a0b\u6d88\u606f\n$msg = $process-&amp;gt;read();\necho $msg;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528swoole_process\u7c7b\u7684\u6784\u9020\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u5e76\u4e14\u901a\u8fc7\u56de\u8c03\u51fd\u6570\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u5b50\u8fdb\u7a0b\u7684\u903b\u8f91\u4ee3\u7801\u3002start()\u65b9\u6cd5\u542f\u52a8\u5b50\u8fdb\u7a0b\uff0c\u7136\u540e\u7236\u8fdb\u7a0b\u901a\u8fc7read()\u65b9\u6cd5\u63a5\u6536\u5b50\u8fdb\u7a0b\u53d1\u9001\u7684\u6d88\u606f\u3002<\/p>\n<ol>\n<li>\u591a\u8fdb\u7a0b\u7684\u901a\u4fe1<br \/>\u5728Swoole\u4e2d\uff0c\u591a\u8fdb\u7a0b\u4e4b\u95f4\u7684\u901a\u4fe1\u53ef\u4ee5\u4f7f\u7528\u7ba1\u9053\u3001\u6d88\u606f\u961f\u5217\u3001\u5171\u4eab\u5185\u5b58\u7b49\u591a\u79cd\u65b9\u5f0f\u3002\u5176\u4e2d\u6bd4\u8f83\u5e38\u7528\u7684\u662f\u7ba1\u9053\u65b9\u5f0f\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u7ba1\u9053\u8fdb\u884c\u901a\u4fe1\u7684\u793a\u4f8b\uff1a<\/li>\n<\/ol>\n<pre>$process = new swoole_process(function(swoole_process $process) {\n    $process-&amp;gt;write(\"Hello world!\n\");\n    $data = $process-&amp;gt;read();\n    echo \"Child process received: \" . $data;\n    $process-&amp;gt;exit();\n}, true); \/\/ \u542f\u7528\u7ba1\u9053\u901a\u4fe1\u6a21\u5f0f\n\n$process-&amp;gt;start();\n\n$msg = $process-&amp;gt;read();\necho $msg;\n$process-&amp;gt;write(\"I am the parent process.\n\");\n$process-&amp;gt;wait(); \/\/ \u7b49\u5f85\u5b50\u8fdb\u7a0b\u9000\u51fa<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u8c03\u7528swoole_process\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u5e76\u4f20\u5165\u4e00\u4e2a\u5e03\u5c14\u7c7b\u578b\u53c2\u6570\uff0c\u6307\u793a\u542f\u7528\u7ba1\u9053\u901a\u4fe1\u6a21\u5f0f\u3002\u7136\u540e\u5728\u7236\u8fdb\u7a0b\u4e2d\u8c03\u7528write()\u65b9\u6cd5\u5411\u5b50\u8fdb\u7a0b\u53d1\u9001\u6d88\u606f\uff0c\u5e76\u901a\u8fc7read()\u65b9\u6cd5\u63a5\u6536\u5b50\u8fdb\u7a0b\u7684\u6d88\u606f\u3002\u5728\u5b50\u8fdb\u7a0b\u4e2d\uff0c\u4e5f\u4f7f\u7528\u4e86write()\u65b9\u6cd5\u5411\u7236\u8fdb\u7a0b\u53d1\u9001\u6d88\u606f\uff0c\u5e76\u7528read()\u65b9\u6cd5\u63a5\u6536\u7236\u8fdb\u7a0b\u7684\u6d88\u606f\u3002<\/p>\n<ol>\n<li>\u591a\u8fdb\u7a0b\u7684\u540c\u6b65<br \/>\u5728\u591a\u8fdb\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u540c\u6b65\u95ee\u9898\u662f\u5fc5\u987b\u8981\u8003\u8651\u7684\u3002Swoole\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u591a\u8fdb\u7a0b\u4e4b\u95f4\u7684\u540c\u6b65\uff0c\u5176\u4e2d\u6bd4\u8f83\u5e38\u7528\u7684\u662f\u4f7f\u7528\u4fe1\u53f7\u91cf\u548c\u9501\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u9501\u8fdb\u884c\u540c\u6b65\u7684\u793a\u4f8b\uff1a<\/li>\n<\/ol>\n<pre>$lock = new swoole_lock(SWOOLE_MUTEX); \/\/ \u521b\u5efa\u4e00\u4e2a\u4e92\u65a5\u9501\n\n$process1 = new swoole_process(function(swoole_process $process) use ($lock) {\n    $lock-&amp;gt;lock(); \/\/ \u52a0\u9501\n    echo \"Process 1 acquired the lock.\n\";\n    sleep(1);\n    $lock-&amp;gt;unlock(); \/\/ \u89e3\u9501\n});\n\n$process2 = new swoole_process(function(swoole_process $process) use ($lock) {\n    $lock-&amp;gt;lock(); \/\/ \u52a0\u9501\n    echo \"Process 2 acquired the lock.\n\";\n    sleep(1);\n    $lock-&amp;gt;unlock(); \/\/ \u89e3\u9501\n});\n\n$process1-&amp;gt;start();\n$process2-&amp;gt;start();\n\n$process1-&amp;gt;wait();\n$process2-&amp;gt;wait();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528swoole_lock\u7c7b\u521b\u5efa\u4e86\u4e00\u4e2a\u4e92\u65a5\u9501\uff0c\u5e76\u5728\u4e24\u4e2a\u5b50\u8fdb\u7a0b\u4e2d\u5206\u522b\u52a0\u9501\u548c\u89e3\u9501\u3002\u5728\u7236\u8fdb\u7a0b\u4e2d\uff0c\u6211\u4eec\u8c03\u7528wait()\u65b9\u6cd5\u7b49\u5f85\u4e24\u4e2a\u5b50\u8fdb\u7a0b\u6267\u884c\u5b8c\u6bd5\u3002<\/p>\n<ol>\n<li>\u5b8c\u6574\u793a\u4f8b<br \/>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528Swoole\u5b9e\u73b0\u591a\u8fdb\u7a0b\u5e76\u53d1\u7f16\u7a0b\uff0c\u5305\u62ec\u521b\u5efa\u591a\u4e2a\u5b50\u8fdb\u7a0b\u3001\u901a\u8fc7\u7ba1\u9053\u8fdb\u884c\u901a\u4fe1\u3001\u4f7f\u7528\u4fe1\u53f7\u91cf\u8fdb\u884c\u540c\u6b65\u7b49\u3002<\/li>\n<\/ol>\n<pre>$workers = [];\n$worker_num = 3;\n\nfor ($i = 0; $i pid} is calculating the square of $num...\n\";\n        sleep(1);\n        $worker-&amp;gt;write($num * $num);\n        $worker-&amp;gt;exit();\n    }, true);\n\n    $pid = $process-&amp;gt;start();\n    $workers[$pid] = $process;\n}\n\n\/\/ \u7236\u8fdb\u7a0b\u63a5\u6536\u5b50\u8fdb\u7a0b\u8fd4\u56de\u7684\u8ba1\u7b97\u7ed3\u679c\nforeach ($workers as $pid =&amp;gt; $process) {\n    $result = $process-&amp;gt;read();\n    echo \"Worker $pid calculated the result: $result\n\";\n}\n\necho \"All workers have completed their tasks.\n\";<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e863\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u5b50\u8fdb\u7a0b\u968f\u673a\u751f\u6210\u4e00\u4e2a\u6570\u5b57\uff0c\u5e76\u8ba1\u7b97\u8fd9\u4e2a\u6570\u5b57\u7684\u5e73\u65b9\u5e76\u8fd4\u56de\u3002\u7236\u8fdb\u7a0b\u901a\u8fc7\u5faa\u73af\u63a5\u6536\u6240\u6709\u5b50\u8fdb\u7a0b\u8fd4\u56de\u7684\u7ed3\u679c\uff0c\u5e76\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002\u6700\u7ec8\uff0c\u7236\u8fdb\u7a0b\u6253\u5370\u4e00\u6761\u5b8c\u6210\u6240\u6709\u4efb\u52a1\u7684\u6d88\u606f\u3002<\/p>\n<p>\u603b\u7ed3<br \/>Swoole\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u9ad8\u6027\u80fd\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u5b83\u5bf9\u591a\u8fdb\u7a0b\u7f16\u7a0b\u63d0\u4f9b\u4e86\u826f\u597d\u7684\u652f\u6301\u3002\u5728\u4f7f\u7528Swoole\u8fdb\u884c\u591a\u8fdb\u7a0b\u7f16\u7a0b\u65f6\uff0c\u9700\u8981\u8003\u8651\u8fdb\u7a0b\u7684\u521b\u5efa\u3001\u901a\u4fe1\u3001\u540c\u6b65\u7b49\u5404\u65b9\u9762\u7684\u95ee\u9898\u3002\u672c\u6587\u63d0\u4f9b\u4e86\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1Swoole\u7684\u591a\u8fdb\u7a0b\u7f16\u7a0b\u6280\u5de7\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528Swoole\u5b9e\u73b0\u591a\u8fdb\u7a0b\u5e76\u53d1\u7f16\u7a0b\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\u662fPHP\u7684\u4e00\u4e2a\u9ad8\u6027\u80fd\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u5e76\u53d1\u7f16\u7a0b\u3002\u5176\u4e2d\u6700\u91cd\u8981\u7684\u4e00\u9879\u7279\u6027\u662f\u5b83\u5bf9\u591a\u8fdb\u7a0b\u7684\u652f\u6301\uff0c\u53ef\u4ee5\u8ba9\u6211\u4eec\u901a\u8fc7\u591a\u8fdb\u7a0b\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684\u7f51\u7edc\u7f16\u7a0b\u3002 \u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Swoole\u5b9e\u73b0\u591a\u8fdb\u7a0b\u5e76\u53d1\u7f16\u7a0b\uff0c\u5305\u62ec\u591a\u8fdb\u7a0b\u7684\u521b\u5efa\u3001\u901a\u4fe1\u3001\u540c\u6b65\u7b49\u65b9\u9762\uff0c\u5e76\u4e14\u4f1a\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u591a\u8fdb\u7a0b\u7684\u521b\u5efa\u5728Swoole\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528_process\u7c7b\u6765\u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a $process = new swoole_process(function(swoole_process $process) { \/\/ \u5b50\u8fdb\u7a0b\u7684\u903b\u8f91\u4ee3\u7801 $process-&amp;gt;write(&#8220;Hello world! &#8220;); \/\/ \u5411\u4e3b\u8fdb\u7a0b\u53d1\u9001\u6d88\u606f $process-&amp;gt;exit(); }); $process-&amp;gt;start(); \/\/ \u7236\u8fdb\u7a0b\u63a5\u6536\u5b50\u8fdb\u7a0b\u6d88\u606f $msg = $process-&amp;gt;read(); echo $msg; \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528swoole_process\u7c7b\u7684\u6784\u9020\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u5e76\u4e14\u901a\u8fc7\u56de\u8c03\u51fd\u6570\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u5b50\u8fdb\u7a0b\u7684\u903b\u8f91\u4ee3\u7801\u3002start()\u65b9\u6cd5\u542f\u52a8\u5b50\u8fdb\u7a0b\uff0c\u7136\u540e\u7236\u8fdb\u7a0b\u901a\u8fc7read()\u65b9\u6cd5\u63a5\u6536\u5b50\u8fdb\u7a0b\u53d1\u9001\u7684\u6d88\u606f\u3002 \u591a\u8fdb\u7a0b\u7684\u901a\u4fe1\u5728Swoole\u4e2d\uff0c\u591a\u8fdb\u7a0b\u4e4b\u95f4\u7684\u901a\u4fe1\u53ef\u4ee5\u4f7f\u7528\u7ba1\u9053\u3001\u6d88\u606f\u961f\u5217\u3001\u5171\u4eab\u5185\u5b58\u7b49\u591a\u79cd\u65b9\u5f0f\u3002\u5176\u4e2d\u6bd4\u8f83\u5e38\u7528\u7684\u662f\u7ba1\u9053\u65b9\u5f0f\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u7ba1\u9053\u8fdb\u884c\u901a\u4fe1\u7684\u793a\u4f8b\uff1a $process = new swoole_process(function(swoole_process $process) { $process-&amp;gt;write(&#8220;Hello world! &#8220;); $data = $process-&amp;gt;read(); echo &#8220;Child process received: &#8221; . $data; $process-&amp;gt;exit(); }, true); \/\/ \u542f\u7528\u7ba1\u9053\u901a\u4fe1\u6a21\u5f0f $process-&amp;gt;start(); $msg [&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-24968","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24968","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=24968"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24968\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}