{"id":46909,"date":"2024-12-02T16:20:06","date_gmt":"2024-12-02T08:20:06","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46909\/"},"modified":"2024-12-02T16:20:06","modified_gmt":"2024-12-02T08:20:06","slug":"php-%e5%87%bd%e6%95%b0%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b%e6%a6%82%e8%a7%88","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46909\/","title":{"rendered":"PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u6982\u89c8"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u6982\u89c8<\/h1>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u52aa\u529b\u5b66\u4e60\u5427\uff01\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u6982\u89c8\u300b<\/span>\u4e3b\u8981\u8bb2\u89e3\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6211\u4f1a\u5728\u7c73\u4e91\u4e2d\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u5e76\u79ef\u6781\u7559\u8a00\u5efa\u8bae\u3002\u4e0b\u9762\u5c31\u5148\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u672c\u7bc7\u6b63\u6587\u5185\u5bb9\u5427\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p>PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u53ef\u4ee5\u901a\u8fc7\u591a\u8fdb\u7a0b\u6216\u591a\u7ebf\u7a0b\u5b9e\u73b0\u3002\u591a\u8fdb\u7a0b\u521b\u5efa\u591a\u4e2a\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u6709\u81ea\u5df1\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u53ef\u4ee5\u901a\u8fc7proc_open()\u51fd\u6570\u521b\u5efa\u8fdb\u7a0b\u3002\u591a\u7ebf\u7a0b\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\uff0c\u5b83\u4eec\u5171\u4eab\u540c\u4e00\u4e2a\u5185\u5b58\u7a7a\u95f4\uff0c\u53ef\u4ee5\u901a\u8fc7pthreads\u6269\u5c55\u5b9e\u73b0\u3002\u5e38\u89c1\u7684\u4f7f\u7528\u573a\u666f\u662f\u5f02\u6b65\u6587\u4ef6\u4e0b\u8f7d\uff0c\u53ef\u4ee5\u540c\u65f6\u4e0b\u8f7d\u591a\u4e2a\u6587\u4ef6\u4ee5\u63d0\u9ad8\u901f\u5ea6\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729854101671b7a953f2e2.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u6982\u89c8\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u6982\u89c8\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u6982\u89c8<\/strong><\/p>\n<p>\u5e76\u53d1\u7f16\u7a0b\u662f\u4e00\u79cd\u7f16\u7a0b\u8303\u5f0f\uff0c\u5b83\u5141\u8bb8\u51fd\u6570\u5728\u4e0d\u963b\u585e\u6267\u884c\u7684\u60c5\u51b5\u4e0b\u540c\u65f6\u8fdb\u884c\u3002PHP \u4e2d\u6709\u4e24\u79cd\u5b9e\u73b0\u51fd\u6570\u5e76\u53d1\u7684\u4e3b\u8981\u65b9\u6cd5\uff1a\u591a\u8fdb\u7a0b\u548c\u591a\u7ebf\u7a0b\u3002<\/p>\n<p><strong>\u591a\u8fdb\u7a0b<\/strong><\/p>\n<p>\u591a\u8fdb\u7a0b\u662f\u5e76\u53d1\u7f16\u7a0b\u7684\u4e00\u79cd\u5f62\u5f0f\uff0c\u5176\u4e2d\u521b\u5efa\u591a\u4e2a\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u6709\u81ea\u5df1\u7684\u5185\u5b58\u7a7a\u95f4\u3002\u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>proc_open()<\/code> \u51fd\u6570\u521b\u5efa\u8fdb\u7a0b\u3002<\/p>\n<pre>$process = proc_open('php script.php', ['r' =&gt; ['pipe', 'r'], 'w' =&gt; ['pipe', 'w']], $pipes);\n\/\/ \u8bfb\u53d6\u6765\u81ea\u5b50\u8fdb\u7a0b\u7684\u8f93\u51fa\n$output = stream_get_contents($pipes[1]);<\/pre>\n<p><strong>\u591a\u7ebf\u7a0b<\/strong><\/p>\n<p>\u591a\u7ebf\u7a0b\u662f\u5e76\u53d1\u7f16\u7a0b\u7684\u53e6\u4e00\u79cd\u5f62\u5f0f\uff0c\u5176\u4e2d\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\uff0c\u4f46\u5b83\u4eec\u5171\u4eab\u540c\u4e00\u4e2a\u5185\u5b58\u7a7a\u95f4\u3002PHP \u4e2d\u6ca1\u6709\u5185\u7f6e\u7684\u591a\u7ebf\u7a0b\u652f\u6301\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528\u6269\u5c55\uff0c\u5982 pthreads\uff0c\u6765\u5b9e\u73b0\u591a\u7ebf\u7a0b\u3002<\/p>\n<pre>\/\/ \u5b89\u88c5 pthreads \u6269\u5c55\ncomposer require pthreads\/pthreads\n\n\/\/ \u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\n$thread = new Thread('function_to_run');\n\n\/\/ \u542f\u52a8\u7ebf\u7a0b\n$thread-&gt;start();\n\n\/\/ \u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210\n$thread-&gt;join();<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u5f02\u6b65\u6587\u4ef6\u4e0b\u8f7d<\/strong><\/p>\n<p>\u4e00\u4e2a\u5e38\u89c1\u7684\u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\u6848\u4f8b\u662f\u5f02\u6b65\u6587\u4ef6\u4e0b\u8f7d\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u591a\u8fdb\u7a0b\u6216\u591a\u7ebf\u7a0b\u6765\u540c\u65f6\u4e0b\u8f7d\u591a\u4e2a\u6587\u4ef6\uff0c\u4ece\u800c\u63d0\u9ad8\u4e0b\u8f7d\u901f\u5ea6\u3002<\/p>\n<pre>\/\/ \u4f7f\u7528\u591a\u8fdb\u7a0b\nfor ($i = 0; $i &lt; 10; $i++) {\n    $process = proc_open('wget file' . $i . '.txt', ['r' =&gt; ['pipe', 'r'], 'w' =&gt; ['pipe', 'w']], $pipes);\n    \/\/ ...\n}\n\n\/\/ \u4f7f\u7528\u591a\u7ebf\u7a0b\nfor ($i = 0; $i &lt; 10; $i++) {\n    $thread = new Thread(function () {\n        $file = fopen('file' . $i . '.txt', 'w');\n        \/\/ ...\n        fclose($file);\n    });\n    $thread-&gt;start();\n    $thread-&gt;join();\n}<\/pre>\n<p>\u901a\u8fc7\u51fd\u6570\u5e76\u53d1\uff0c\u6211\u4eec\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8 PHP \u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u53ef\u6269\u5c55\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aPHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u6982\u89c8\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8ephp,\u5e76\u53d1\u7f16\u7a0b\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u6982\u89c8 \u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u52aa\u529b\u5b66\u4e60\u5427\uff01\u672c\u6587\u300aPHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u6982\u89c8\u300b\u4e3b\u8981\u8bb2\u89e3\u4e86\u7b49\u7b49\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6211\u4f1a\u5728\u7c73\u4e91\u4e2d\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u5e76\u79ef\u6781\u7559\u8a00\u5efa\u8bae\u3002\u4e0b\u9762\u5c31\u5148\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u672c\u7bc7\u6b63\u6587\u5185\u5bb9\u5427\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01 PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u53ef\u4ee5\u901a\u8fc7\u591a\u8fdb\u7a0b\u6216\u591a\u7ebf\u7a0b\u5b9e\u73b0\u3002\u591a\u8fdb\u7a0b\u521b\u5efa\u591a\u4e2a\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u6709\u81ea\u5df1\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u53ef\u4ee5\u901a\u8fc7proc_open()\u51fd\u6570\u521b\u5efa\u8fdb\u7a0b\u3002\u591a\u7ebf\u7a0b\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\uff0c\u5b83\u4eec\u5171\u4eab\u540c\u4e00\u4e2a\u5185\u5b58\u7a7a\u95f4\uff0c\u53ef\u4ee5\u901a\u8fc7pthreads\u6269\u5c55\u5b9e\u73b0\u3002\u5e38\u89c1\u7684\u4f7f\u7528\u573a\u666f\u662f\u5f02\u6b65\u6587\u4ef6\u4e0b\u8f7d\uff0c\u53ef\u4ee5\u540c\u65f6\u4e0b\u8f7d\u591a\u4e2a\u6587\u4ef6\u4ee5\u63d0\u9ad8\u901f\u5ea6\u3002 PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u6982\u89c8 \u5e76\u53d1\u7f16\u7a0b\u662f\u4e00\u79cd\u7f16\u7a0b\u8303\u5f0f\uff0c\u5b83\u5141\u8bb8\u51fd\u6570\u5728\u4e0d\u963b\u585e\u6267\u884c\u7684\u60c5\u51b5\u4e0b\u540c\u65f6\u8fdb\u884c\u3002PHP \u4e2d\u6709\u4e24\u79cd\u5b9e\u73b0\u51fd\u6570\u5e76\u53d1\u7684\u4e3b\u8981\u65b9\u6cd5\uff1a\u591a\u8fdb\u7a0b\u548c\u591a\u7ebf\u7a0b\u3002 \u591a\u8fdb\u7a0b \u591a\u8fdb\u7a0b\u662f\u5e76\u53d1\u7f16\u7a0b\u7684\u4e00\u79cd\u5f62\u5f0f\uff0c\u5176\u4e2d\u521b\u5efa\u591a\u4e2a\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u6709\u81ea\u5df1\u7684\u5185\u5b58\u7a7a\u95f4\u3002\u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 proc_open() \u51fd\u6570\u521b\u5efa\u8fdb\u7a0b\u3002 $process = proc_open(&#8216;php script.php&#8217;, [&#8216;r&#8217; =&gt; [&#8216;pipe&#8217;, &#8216;r&#8217;], &#8216;w&#8217; =&gt; [&#8216;pipe&#8217;, &#8216;w&#8217;]], $pipes); \/\/ \u8bfb\u53d6\u6765\u81ea\u5b50\u8fdb\u7a0b\u7684\u8f93\u51fa $output = stream_get_contents($pipes[1]); \u591a\u7ebf\u7a0b \u591a\u7ebf\u7a0b\u662f\u5e76\u53d1\u7f16\u7a0b\u7684\u53e6\u4e00\u79cd\u5f62\u5f0f\uff0c\u5176\u4e2d\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\uff0c\u4f46\u5b83\u4eec\u5171\u4eab\u540c\u4e00\u4e2a\u5185\u5b58\u7a7a\u95f4\u3002PHP \u4e2d\u6ca1\u6709\u5185\u7f6e\u7684\u591a\u7ebf\u7a0b\u652f\u6301\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528\u6269\u5c55\uff0c\u5982 pthreads\uff0c\u6765\u5b9e\u73b0\u591a\u7ebf\u7a0b\u3002 \/\/ \u5b89\u88c5 pthreads \u6269\u5c55 composer require pthreads\/pthreads \/\/ \u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b $thread = new Thread(&#8216;function_to_run&#8217;); \/\/ \u542f\u52a8\u7ebf\u7a0b $thread-&gt;start(); \/\/ \u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210 [&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-46909","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46909","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=46909"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46909\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}