{"id":23424,"date":"2024-11-21T10:02:09","date_gmt":"2024-11-21T02:02:09","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23424\/"},"modified":"2024-11-21T10:02:09","modified_gmt":"2024-11-21T02:02:09","slug":"swoole%e8%bf%9b%e9%98%b6%ef%bc%9a%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%a4%9a%e8%bf%9b%e7%a8%8b%e6%8f%90%e5%8d%87php%e5%a4%84%e7%90%86%e8%83%bd%e5%8a%9b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23424\/","title":{"rendered":"Swoole\u8fdb\u9636\uff1a\u5982\u4f55\u4f7f\u7528\u591a\u8fdb\u7a0b\u63d0\u5347PHP\u5904\u7406\u80fd\u529b"},"content":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u7684\u53d1\u5c55\uff0c\u5bf9\u4e8eweb\u5e94\u7528\u6027\u80fd\u548c\u5e76\u53d1\u5904\u7406\u80fd\u529b\u7684\u8981\u6c42\u4e5f\u8d8a\u6765\u8d8a\u9ad8\u3002\u800c\u5bf9\u4e8ephp\u8bed\u8a00\u7684\u5e94\u7528\u7a0b\u5e8f\u800c\u8a00\uff0c\u7531\u4e8e\u5176\u5355\u7ebf\u7a0b\u7684\u7279\u6027\uff0c\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u5f88\u5bb9\u6613\u51fa\u73b0\u6027\u80fd\u74f6\u9888\u3002\u800c\u4f5c\u4e3a\u4e00\u6b3e\u9ad8\u6027\u80fd\u7684\u5f02\u6b65\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u80fd\u591f\u6709\u6548\u5730\u63d0\u5347php\u5e94\u7528\u7a0b\u5e8f\u7684\u5904\u7406\u80fd\u529b\uff0c\u4ece\u800c\u4f7f\u5176\u80fd\u591f\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u8868\u73b0\u51fa\u8272\u3002<\/p>\n<p>\u5728Swoole\u7684\u5b98\u65b9\u6587\u6863\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0\u5b83\u652f\u6301\u591a\u8fdb\u7a0b\u7684\u7f16\u7a0b\u65b9\u5f0f\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u8ba9\u6211\u4eec\u5c06\u4e00\u4e2a\u5927\u4efb\u52a1\u5e73\u5747\u5206\u914d\u7ed9\u591a\u4e2a\u8fdb\u7a0b\u53bb\u6267\u884c\uff0c\u4ece\u800c\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u63d0\u5347\u4e86PHP\u5e94\u7528\u7a0b\u5e8f\u7684\u5904\u7406\u80fd\u529b\u3002\u540c\u65f6\uff0c\u57fa\u4e8e\u591a\u8fdb\u7a0b\u7684\u7f16\u7a0b\u65b9\u5f0f\u4e5f\u80fd\u591f\u5b9e\u73b0\u66f4\u52a0\u7075\u6d3b\u7684\u8fdb\u7a0b\u7ba1\u7406\u548c\u8d44\u6e90\u8c03\u5ea6\uff0c\u6709\u6548\u5730\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Swoole\u7684\u591a\u8fdb\u7a0b\u7f16\u7a0b\u65b9\u5f0f\u63d0\u5347PHP\u5904\u7406\u80fd\u529b\u3002<\/p>\n<h2>\u4ec0\u4e48\u662f\u591a\u8fdb\u7a0b\uff1f<\/h2>\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u8fdb\u7a0b\u5c31\u662f\u6b63\u5728\u8fd0\u884c\u7684\u7a0b\u5e8f\u3002\u591a\u8fdb\u7a0b\u5373\u6307\u4e00\u4e2a\u7a0b\u5e8f\u5206\u6210\u591a\u4e2a\u8fdb\u7a0b\u53bb\u6267\u884c\u3002<\/p>\n<p>\u5728Unix\/Linux\u7cfb\u7edf\u4e2d\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u6709\u4e00\u4e2a\u552f\u4e00\u7684\u8fdb\u7a0bID\uff0c\u901a\u8fc7\u8be5ID\u53ef\u4ee5\u533a\u5206\u4e0d\u540c\u7684\u8fdb\u7a0b\u3002\u591a\u8fdb\u7a0b\u7f16\u7a0b\u7684\u672c\u8d28\u662f\u5c06\u4e00\u4e2a\u7a0b\u5e8f\u5206\u6210\u591a\u4e2a\u8fdb\u7a0b\u53bb\u6267\u884c\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u62e5\u6709\u72ec\u7acb\u7684\u5730\u5740\u7a7a\u95f4\u3001\u6570\u636e\u6808\u3001\u6307\u4ee4\u8ba1\u6570\u5668\u7b49\u8d44\u6e90\u3002\u8fdb\u7a0b\u4e4b\u95f4\u7684\u901a\u4fe1\u53ef\u4ee5\u901a\u8fc7\u6d88\u606f\u961f\u5217\u3001\u7ba1\u9053\u3001\u5171\u4eab\u5185\u5b58\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3002<\/p>\n<h2>\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u591a\u8fdb\u7a0b\uff1f<\/h2>\n<p>\u4f7f\u7528\u591a\u8fdb\u7a0b\u7f16\u7a0b\u53ef\u4ee5\u5e26\u6765\u4ee5\u4e0b\u51e0\u4e2a\u597d\u5904\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li>\u63d0\u5347\u5904\u7406\u80fd\u529b\uff1a\u591a\u8fdb\u7a0b\u7f16\u7a0b\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5927\u4efb\u52a1\u5e73\u5747\u5206\u914d\u7ed9\u591a\u4e2a\u8fdb\u7a0b\u53bb\u6267\u884c\uff0c\u4ece\u800c\u63d0\u5347\u7a0b\u5e8f\u7684\u5904\u7406\u80fd\u529b\u548c\u5e76\u53d1\u6027\u80fd\u3002<\/li>\n<li>\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\uff1a\u591a\u8fdb\u7a0b\u7f16\u7a0b\u53ef\u4ee5\u901a\u8fc7\u8fdb\u7a0b\u7ba1\u7406\u548c\u8d44\u6e90\u8c03\u5ea6\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002<\/li>\n<li>\u5e76\u884c\u8ba1\u7b97\uff1a\u591a\u8fdb\u7a0b\u7f16\u7a0b\u53ef\u4ee5\u8ba9\u4e0d\u540c\u7684\u8fdb\u7a0b\u540c\u65f6\u6267\u884c\u8ba1\u7b97\u4efb\u52a1\uff0c\u4ece\u800c\u5b9e\u73b0\u5e76\u884c\u8ba1\u7b97\u3002<\/li>\n<\/ol>\n<h2>\u5982\u4f55\u5728Swoole\u4e2d\u4f7f\u7528\u591a\u8fdb\u7a0b\uff1f<\/h2>\n<p>\u4e0b\u9762\u4ecb\u7ecd\u57fa\u4e8eSwoole\u7684\u591a\u8fdb\u7a0b\u7f16\u7a0b\u65b9\u5f0f\uff0c\u6765\u5e2e\u52a9\u6211\u4eec\u63d0\u5347PHP\u5e94\u7528\u7a0b\u5e8f\u7684\u5904\u7406\u80fd\u529b\u3002<\/p>\n<h3>\u521b\u5efa\u5b50\u8fdb\u7a0b<\/h3>\n<p>\u5728Swoole\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528swoole_process\u7c7b\u6765\u521b\u5efa\u5b50\u8fdb\u7a0b\u3002swoole_process\u4e2d\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u6cd5\uff0c\u5305\u62ec\u521b\u5efa\u5b50\u8fdb\u7a0b\u3001\u5411\u5b50\u8fdb\u7a0b\u53d1\u9001\u6d88\u606f\u3001\u8bfb\u53d6\u5b50\u8fdb\u7a0b\u7684stdout\u548cstderr\u7b49\u7b49\u3002<\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528swoole_process\u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\uff1a<\/p>\n<pre>$process = new swoole_process(function(swoole_process $process) {\n    \/\/ \u8fd9\u91cc\u662f\u5b50\u8fdb\u7a0b\u7684\u903b\u8f91\u5904\u7406\u4ee3\u7801\n    $process-&amp;gt;write(\"Hello from child process\n\");\n});\n\n$process-&amp;gt;start();\nswoole_event_wait();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7new swoole_process()\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u5b50\u8fdb\u7a0b\uff0c\u4f20\u5165\u7684\u53c2\u6570\u662f\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4e2d\u5305\u542b\u4e86\u5b50\u8fdb\u7a0b\u7684\u5904\u7406\u903b\u8f91\u3002$process-&gt;start()\u65b9\u6cd5\u53ef\u4ee5\u542f\u52a8\u5b50\u8fdb\u7a0b\u3002swoole_event_wait()\u65b9\u6cd5\u7528\u4e8e\u7b49\u5f85\u5b50\u8fdb\u7a0b\u6267\u884c\u5b8c\u6bd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7406\u89e3\u4e3a\u5b83\u4f1a\u963b\u585e\u5f53\u524d\u8fdb\u7a0b\uff0c\u76f4\u5230\u5b50\u8fdb\u7a0b\u7ed3\u675f\u3002<\/p>\n<h3>\u7236\u5b50\u8fdb\u7a0b\u901a\u4fe1<\/h3>\n<p>\u5728\u6807\u51c6\u7684Unix\/Linux\u7cfb\u7edf\u4e2d\uff0c\u8fdb\u7a0b\u4e4b\u95f4\u7684\u901a\u4fe1\u53ef\u4ee5\u901a\u8fc7\u7ba1\u9053\uff08pipe\uff09\u3001\u6d88\u606f\u961f\u5217\uff08message queue\uff09\u3001\u5171\u4eab\u5185\u5b58\uff08shared memory\uff09\u3001\u4fe1\u53f7\uff08signal\uff09\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3002\u5728Swoole\u4e2d\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u65b9\u5f0f\u5b9e\u73b0\u7236\u5b50\u8fdb\u7a0b\u4e4b\u95f4\u7684\u901a\u4fe1\u3002<\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u5728\u7236\u5b50\u8fdb\u7a0b\u4e4b\u95f4\u901a\u8fc7\u7ba1\u9053\u901a\u4fe1\uff1a<\/p>\n<pre>$process = new swoole_process(function(swoole_process $process) {\n    \/\/ \u5b50\u8fdb\u7a0b\uff1a\u4ece\u7236\u8fdb\u7a0b\u7ba1\u9053\u4e2d\u8bfb\u53d6\u6d88\u606f\n    $msg = $process-&amp;gt;read();\n    echo \"Message from parent process: $msg\n\";\n    $process-&amp;gt;exit();\n});\n\n$process-&amp;gt;start();\n\n\/\/ \u7236\u8fdb\u7a0b\uff1a\u5411\u5b50\u8fdb\u7a0b\u7ba1\u9053\u4e2d\u5199\u5165\u6d88\u606f\n$process-&amp;gt;write(\"Hello from parent process\");\n\n\/\/ \u7b49\u5f85\u5b50\u8fdb\u7a0b\u7ed3\u675f\nswoole_process::wait();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5728\u5b50\u8fdb\u7a0b\u4e2d\u8c03\u7528$process-&gt;read()\u65b9\u6cd5\u4ece\u7236\u8fdb\u7a0b\u7ba1\u9053\u4e2d\u8bfb\u53d6\u6d88\u606f\uff0c\u7136\u540e\u901a\u8fc7echo\u6253\u5370\u51fa\u6765\u3002\u5728\u7236\u8fdb\u7a0b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7$process-&gt;write()\u65b9\u6cd5\u5411\u5b50\u8fdb\u7a0b\u7ba1\u9053\u4e2d\u5199\u5165\u6d88\u606f\u3002<\/p>\n<h3>\u8fdb\u7a0b\u6c60<\/h3>\n<p>\u5982\u679c\u6211\u4eec\u9700\u8981\u542f\u52a8\u591a\u4e2a\u5b50\u8fdb\u7a0b\u6765\u5904\u7406\u4efb\u52a1\uff0c\u90a3\u4e48\u6bcf\u4e2a\u5b50\u8fdb\u7a0b\u90fd\u9700\u8981\u5206\u522b\u521b\u5efa\u548c\u7ba1\u7406\u662f\u4e00\u4ef6\u6bd4\u8f83\u9ebb\u70e6\u7684\u4e8b\u60c5\u3002\u9488\u5bf9\u8fd9\u79cd\u60c5\u51b5\uff0cSwoole\u63d0\u4f9b\u4e86swoole_process::pool()\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fdb\u7a0b\u6c60\u7684\u6982\u5ff5\u3002\u8fdb\u7a0b\u6c60\u53ef\u4ee5\u5b9e\u73b0\u591a\u4e2a\u5b50\u8fdb\u7a0b\u7684\u590d\u7528\uff0c\u4ece\u800c\u4f18\u5316\u8fdb\u7a0b\u7ba1\u7406\u548c\u8d44\u6e90\u8c03\u5ea6\u3002<\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u8fdb\u7a0b\u6c60\u521b\u5efa\u591a\u4e2a\u5b50\u8fdb\u7a0b\u6765\u5904\u7406\u4efb\u52a1\uff1a<\/p>\n<pre>$pool = new swoole_process_pool(2, SWOOLE_IPC_UNIXSOCK, 0, true);\n\n\/\/ \u8bbe\u7f6e\u8fdb\u7a0b\u6c60\u542f\u52a8\u56de\u8c03\u51fd\u6570\n$pool-&amp;gt;on(\"workerStart\", function(swoole_process_pool $pool, $worker_id) {\n    echo \"Worker $worker_id started\n\";\n});\n\n\/\/ \u8bbe\u7f6e\u8fdb\u7a0b\u6c60\u4efb\u52a1\u5904\u7406\u51fd\u6570\n$pool-&amp;gt;on(\"message\", function(swoole_process_pool $pool, $task) {\n    echo \"Task received: $task\n\";\n});\n\n\/\/ \u542f\u52a8\u8fdb\u7a0b\u6c60\n$pool-&amp;gt;start();\n\n\/\/ \u5f80\u8fdb\u7a0b\u6c60\u4e2d\u6295\u9012\u4efb\u52a1\n$pool-&amp;gt;write(\"Hello\");\n\n\/\/ \u7b49\u5f85\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\n$pool-&amp;gt;shutdown();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7swoole_process_pool\u7c7b\u521b\u5efa\u4e86\u4e00\u4e2a\u8fdb\u7a0b\u6c60\uff0c\u5176\u4e2d\u53c2\u65702\u8868\u793a\u4f7f\u7528UNIX\u57df\u5957\u63a5\u5b57\u901a\u4fe1\uff0c\u53c2\u65700\u8868\u793a\u4e0d\u505a\u9650\u5236\u5730\u4f7f\u7528\u6240\u6709CPU\u5185\u6838\uff0c\u53c2\u6570true\u8868\u793a\u81ea\u52a8\u91cd\u542f\u6b7b\u4ea1\u7684\u5b50\u8fdb\u7a0b\u3002\u5728\u8bbe\u7f6e\u8fdb\u7a0b\u6c60\u542f\u52a8\u56de\u8c03\u51fd\u6570\u548c\u4efb\u52a1\u5904\u7406\u51fd\u6570\u540e\uff0c\u6211\u4eec\u901a\u8fc7$pool-&gt;start()\u65b9\u6cd5\u542f\u52a8\u8fdb\u7a0b\u6c60\u3002\u5728\u6295\u9012\u5b8c\u4efb\u52a1\u540e\uff0c\u6211\u4eec\u901a\u8fc7$pool-&gt;shutdown()\u65b9\u6cd5\u7b49\u5f85\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u901a\u8fc7\u4f7f\u7528Swoole\u4e2d\u7684\u591a\u8fdb\u7a0b\u7f16\u7a0b\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u5347PHP\u5e94\u7528\u7a0b\u5e8f\u7684\u5904\u7406\u80fd\u529b\u548c\u5e76\u53d1\u6027\u80fd\u3002\u4f7f\u7528\u591a\u8fdb\u7a0b\u7684\u4f18\u5316\u65b9\u5f0f\u80fd\u591f\u5b9e\u73b0\u66f4\u52a0\u7075\u6d3b\u7684\u8fdb\u7a0b\u7ba1\u7406\u548c\u8d44\u6e90\u8c03\u5ea6\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u7ed3\u5408\u81ea\u5df1\u7684\u4e1a\u52a1\u9700\u6c42\u548c\u7cfb\u7edf\u8d44\u6e90\u6765\u9009\u62e9\u5408\u9002\u7684\u8fdb\u7a0b\u6570\u548c\u4f18\u5316\u65b9\u5f0f\uff0c\u4ee5\u8fbe\u5230\u66f4\u597d\u7684\u6027\u80fd\u548c\u53ef\u9760\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSwoole\u8fdb\u9636\uff1a\u5982\u4f55\u4f7f\u7528\u591a\u8fdb\u7a0b\u63d0\u5347PHP\u5904\u7406\u80fd\u529b\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\u53d1\u5c55\uff0c\u5bf9\u4e8eweb\u5e94\u7528\u6027\u80fd\u548c\u5e76\u53d1\u5904\u7406\u80fd\u529b\u7684\u8981\u6c42\u4e5f\u8d8a\u6765\u8d8a\u9ad8\u3002\u800c\u5bf9\u4e8ephp\u8bed\u8a00\u7684\u5e94\u7528\u7a0b\u5e8f\u800c\u8a00\uff0c\u7531\u4e8e\u5176\u5355\u7ebf\u7a0b\u7684\u7279\u6027\uff0c\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u5f88\u5bb9\u6613\u51fa\u73b0\u6027\u80fd\u74f6\u9888\u3002\u800c\u4f5c\u4e3a\u4e00\u6b3e\u9ad8\u6027\u80fd\u7684\u5f02\u6b65\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u80fd\u591f\u6709\u6548\u5730\u63d0\u5347php\u5e94\u7528\u7a0b\u5e8f\u7684\u5904\u7406\u80fd\u529b\uff0c\u4ece\u800c\u4f7f\u5176\u80fd\u591f\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u8868\u73b0\u51fa\u8272\u3002 \u5728Swoole\u7684\u5b98\u65b9\u6587\u6863\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0\u5b83\u652f\u6301\u591a\u8fdb\u7a0b\u7684\u7f16\u7a0b\u65b9\u5f0f\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u8ba9\u6211\u4eec\u5c06\u4e00\u4e2a\u5927\u4efb\u52a1\u5e73\u5747\u5206\u914d\u7ed9\u591a\u4e2a\u8fdb\u7a0b\u53bb\u6267\u884c\uff0c\u4ece\u800c\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u63d0\u5347\u4e86PHP\u5e94\u7528\u7a0b\u5e8f\u7684\u5904\u7406\u80fd\u529b\u3002\u540c\u65f6\uff0c\u57fa\u4e8e\u591a\u8fdb\u7a0b\u7684\u7f16\u7a0b\u65b9\u5f0f\u4e5f\u80fd\u591f\u5b9e\u73b0\u66f4\u52a0\u7075\u6d3b\u7684\u8fdb\u7a0b\u7ba1\u7406\u548c\u8d44\u6e90\u8c03\u5ea6\uff0c\u6709\u6548\u5730\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Swoole\u7684\u591a\u8fdb\u7a0b\u7f16\u7a0b\u65b9\u5f0f\u63d0\u5347PHP\u5904\u7406\u80fd\u529b\u3002 \u4ec0\u4e48\u662f\u591a\u8fdb\u7a0b\uff1f \u7b80\u5355\u6765\u8bf4\uff0c\u8fdb\u7a0b\u5c31\u662f\u6b63\u5728\u8fd0\u884c\u7684\u7a0b\u5e8f\u3002\u591a\u8fdb\u7a0b\u5373\u6307\u4e00\u4e2a\u7a0b\u5e8f\u5206\u6210\u591a\u4e2a\u8fdb\u7a0b\u53bb\u6267\u884c\u3002 \u5728Unix\/Linux\u7cfb\u7edf\u4e2d\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u6709\u4e00\u4e2a\u552f\u4e00\u7684\u8fdb\u7a0bID\uff0c\u901a\u8fc7\u8be5ID\u53ef\u4ee5\u533a\u5206\u4e0d\u540c\u7684\u8fdb\u7a0b\u3002\u591a\u8fdb\u7a0b\u7f16\u7a0b\u7684\u672c\u8d28\u662f\u5c06\u4e00\u4e2a\u7a0b\u5e8f\u5206\u6210\u591a\u4e2a\u8fdb\u7a0b\u53bb\u6267\u884c\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u62e5\u6709\u72ec\u7acb\u7684\u5730\u5740\u7a7a\u95f4\u3001\u6570\u636e\u6808\u3001\u6307\u4ee4\u8ba1\u6570\u5668\u7b49\u8d44\u6e90\u3002\u8fdb\u7a0b\u4e4b\u95f4\u7684\u901a\u4fe1\u53ef\u4ee5\u901a\u8fc7\u6d88\u606f\u961f\u5217\u3001\u7ba1\u9053\u3001\u5171\u4eab\u5185\u5b58\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3002 \u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u591a\u8fdb\u7a0b\uff1f \u4f7f\u7528\u591a\u8fdb\u7a0b\u7f16\u7a0b\u53ef\u4ee5\u5e26\u6765\u4ee5\u4e0b\u51e0\u4e2a\u597d\u5904\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u63d0\u5347\u5904\u7406\u80fd\u529b\uff1a\u591a\u8fdb\u7a0b\u7f16\u7a0b\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5927\u4efb\u52a1\u5e73\u5747\u5206\u914d\u7ed9\u591a\u4e2a\u8fdb\u7a0b\u53bb\u6267\u884c\uff0c\u4ece\u800c\u63d0\u5347\u7a0b\u5e8f\u7684\u5904\u7406\u80fd\u529b\u548c\u5e76\u53d1\u6027\u80fd\u3002 \u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\uff1a\u591a\u8fdb\u7a0b\u7f16\u7a0b\u53ef\u4ee5\u901a\u8fc7\u8fdb\u7a0b\u7ba1\u7406\u548c\u8d44\u6e90\u8c03\u5ea6\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002 \u5e76\u884c\u8ba1\u7b97\uff1a\u591a\u8fdb\u7a0b\u7f16\u7a0b\u53ef\u4ee5\u8ba9\u4e0d\u540c\u7684\u8fdb\u7a0b\u540c\u65f6\u6267\u884c\u8ba1\u7b97\u4efb\u52a1\uff0c\u4ece\u800c\u5b9e\u73b0\u5e76\u884c\u8ba1\u7b97\u3002 \u5982\u4f55\u5728Swoole\u4e2d\u4f7f\u7528\u591a\u8fdb\u7a0b\uff1f \u4e0b\u9762\u4ecb\u7ecd\u57fa\u4e8eSwoole\u7684\u591a\u8fdb\u7a0b\u7f16\u7a0b\u65b9\u5f0f\uff0c\u6765\u5e2e\u52a9\u6211\u4eec\u63d0\u5347PHP\u5e94\u7528\u7a0b\u5e8f\u7684\u5904\u7406\u80fd\u529b\u3002 \u521b\u5efa\u5b50\u8fdb\u7a0b \u5728Swoole\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528swoole_process\u7c7b\u6765\u521b\u5efa\u5b50\u8fdb\u7a0b\u3002swoole_process\u4e2d\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u6cd5\uff0c\u5305\u62ec\u521b\u5efa\u5b50\u8fdb\u7a0b\u3001\u5411\u5b50\u8fdb\u7a0b\u53d1\u9001\u6d88\u606f\u3001\u8bfb\u53d6\u5b50\u8fdb\u7a0b\u7684stdout\u548cstderr\u7b49\u7b49\u3002 \u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528swoole_process\u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\uff1a $process = new swoole_process(function(swoole_process $process) { \/\/ \u8fd9\u91cc\u662f\u5b50\u8fdb\u7a0b\u7684\u903b\u8f91\u5904\u7406\u4ee3\u7801 $process-&amp;gt;write(&#8220;Hello from child process &#8220;); }); $process-&amp;gt;start(); swoole_event_wait(); \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7new swoole_process()\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u5b50\u8fdb\u7a0b\uff0c\u4f20\u5165\u7684\u53c2\u6570\u662f\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4e2d\u5305\u542b\u4e86\u5b50\u8fdb\u7a0b\u7684\u5904\u7406\u903b\u8f91\u3002$process-&gt;start()\u65b9\u6cd5\u53ef\u4ee5\u542f\u52a8\u5b50\u8fdb\u7a0b\u3002swoole_event_wait()\u65b9\u6cd5\u7528\u4e8e\u7b49\u5f85\u5b50\u8fdb\u7a0b\u6267\u884c\u5b8c\u6bd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7406\u89e3\u4e3a\u5b83\u4f1a\u963b\u585e\u5f53\u524d\u8fdb\u7a0b\uff0c\u76f4\u5230\u5b50\u8fdb\u7a0b\u7ed3\u675f\u3002 \u7236\u5b50\u8fdb\u7a0b\u901a\u4fe1 \u5728\u6807\u51c6\u7684Unix\/Linux\u7cfb\u7edf\u4e2d\uff0c\u8fdb\u7a0b\u4e4b\u95f4\u7684\u901a\u4fe1\u53ef\u4ee5\u901a\u8fc7\u7ba1\u9053\uff08pipe\uff09\u3001\u6d88\u606f\u961f\u5217\uff08message queue\uff09\u3001\u5171\u4eab\u5185\u5b58\uff08shared memory\uff09\u3001\u4fe1\u53f7\uff08signal\uff09\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3002\u5728Swoole\u4e2d\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u65b9\u5f0f\u5b9e\u73b0\u7236\u5b50\u8fdb\u7a0b\u4e4b\u95f4\u7684\u901a\u4fe1\u3002 \u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u5728\u7236\u5b50\u8fdb\u7a0b\u4e4b\u95f4\u901a\u8fc7\u7ba1\u9053\u901a\u4fe1\uff1a $process = new swoole_process(function(swoole_process $process) { \/\/ \u5b50\u8fdb\u7a0b\uff1a\u4ece\u7236\u8fdb\u7a0b\u7ba1\u9053\u4e2d\u8bfb\u53d6\u6d88\u606f $msg = $process-&amp;gt;read(); echo &#8220;Message from parent [&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-23424","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23424","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=23424"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23424\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}