{"id":46867,"date":"2024-12-02T16:38:06","date_gmt":"2024-12-02T08:38:06","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46867\/"},"modified":"2024-12-02T16:38:06","modified_gmt":"2024-12-02T08:38:06","slug":"php%e5%87%bd%e6%95%b0%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b%ef%bc%9a%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b%e7%9a%84%e6%b7%b1%e5%85%a5%e8%a7%a3%e8%af%bb","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46867\/","title":{"rendered":"PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff1a\u5f02\u6b65\u7f16\u7a0b\u7684\u6df1\u5165\u89e3\u8bfb"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff1a\u5f02\u6b65\u7f16\u7a0b\u7684\u6df1\u5165\u89e3\u8bfb<\/h1>\n<p>\u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff1a\u5f02\u6b65\u7f16\u7a0b\u7684\u6df1\u5165\u89e3\u8bfb\u300b<\/span>\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01<\/p>\n<p>PHP \u5f15\u5165\u4e86\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff0c\u4f7f\u7528\u534f\u7a0b\u548c Generator \u5728\u5355\u4e2a\u8fdb\u7a0b\u4e2d\u5e76\u884c\u6267\u884c\u4efb\u52a1\uff0c\u53ef\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u548c\u54cd\u5e94\u80fd\u529b\u3002\u534f\u7a0b\u5141\u8bb8\u6682\u505c\u548c\u6062\u590d\u6267\u884c\uff0c\u800c Generator \u7528\u4e8e\u751f\u6210\u503c\u5e8f\u5217\u3002\u5f02\u6b65 HTTP \u8bf7\u6c42\u7b49\u5b9e\u6218\u6848\u4f8b\u5c55\u793a\u4e86\u5e76\u53d1\u7f16\u7a0b\u7684\u5b9e\u9645\u5e94\u7528\uff0c\u901a\u8fc7\u63d0\u9ad8\u751f\u4ea7\u529b\u6765\u63d0\u9ad8\u54cd\u5e94\u80fd\u529b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729851279671b6f8fe9370.jpg\" class=\"aligncenter\" title=\"PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff1a\u5f02\u6b65\u7f16\u7a0b\u7684\u6df1\u5165\u89e3\u8bfb\u63d2\u56fe\" alt=\"PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff1a\u5f02\u6b65\u7f16\u7a0b\u7684\u6df1\u5165\u89e3\u8bfb\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff1a\u5f02\u6b65\u7f16\u7a0b\u7684\u6df1\u5165\u89e3\u8bfb<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>PHP \u8bed\u8a00\u65b0\u5f15\u5165\u4e86\u534f\u7a0b\u548c Generator\uff0c\u8fd9\u4f7f\u5f97\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u6210\u4e3a\u53ef\u80fd\u3002\u5e76\u53d1\u7f16\u7a0b\u5141\u8bb8\u5728\u5355\u4e2a\u8fdb\u7a0b\u4e2d\u5e76\u884c\u6267\u884c\u591a\u4e2a\u4efb\u52a1\uff0c\u4ece\u800c\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u54cd\u5e94\u80fd\u529b\u3002<\/p>\n<p><strong>\u534f\u7a0b<\/strong><\/p>\n<p>\u534f\u7a0b\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u6682\u505c\u548c\u6062\u590d\u6267\u884c\u3002\u5b83\u4eec\u5141\u8bb8\u5728\u4e0d\u79bb\u5f00\u5f53\u524d\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\u5207\u6362\u5230\u4e0d\u540c\u7684\u4efb\u52a1\u3002\u534f\u7a0b\u53ef\u4ee5\u901a\u8fc7 <code>yield<\/code> \u5173\u952e\u5b57\u8fdb\u884c\u6682\u505c\u548c\u6062\u590d\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>function task() {\n    while (true) {\n        yield; \/\/ \u6682\u505c\u534f\u7a0b\n        echo \"Hello from task\\n\";\n    }\n}\ngo(task()); \/\/ \u542f\u52a8\u534f\u7a0b\n\n\/\/ \u4e3b\u8fdb\u7a0b\u7ee7\u7eed\u6267\u884c\nwhile (true) {\n    echo \"Hello from main\\n\";\n}<\/pre>\n<p><strong>Generator<\/strong><\/p>\n<p>Generator \u7c7b\u4f3c\u4e8e\u534f\u7a0b\uff0c\u4f46\u5b83\u6ca1\u6709\u6682\u505c\u548c\u6062\u590d\u7684\u80fd\u529b\u3002\u5b83\u4f7f\u7528 <code>yield<\/code> \u5173\u952e\u5b57\u751f\u6210\u4e00\u4e2a\u5e8f\u5217\u7684\u503c\uff0c\u800c\u65e0\u9700\u5b58\u50a8\u6574\u4e2a\u5e8f\u5217\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>function range(int $start, int $end) {\n    for ($i = $start; $i &lt;= $end; $i++) {\n        yield $i;\n    }\n}\nforeach (range(1, 10) as $number) {\n    echo $number . \"\\n\";\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u5f02\u6b65 HTTP \u8bf7\u6c42<\/strong><\/p>\n<p>\u4f7f\u7528\u534f\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5f02\u6b65\u8fdb\u884c HTTP \u8bf7\u6c42\uff0c\u800c\u4e0d\u963b\u585e\u4e3b\u8fdb\u7a0b\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>use GuzzleHttp\\CoroutineClient;\nuse GuzzleHttp\\Pool;\nuse GuzzleHttp\\Psr7\\Request;\n\n$client = new CoroutineClient();\n$requests = [\n    new Request('GET', 'https:\/\/example.com'),\n    new Request('GET', 'https:\/\/example.com\/about'),\n];\n\n$pool = new Pool($client, $requests, [\n    'concurrency' =&gt; 5, \/\/ \u5e76\u53d1\u8bf7\u6c42\u6570\n    'fulfilled' =&gt; function ($response, $request) {\n        echo $response-&gt;getBody();\n    },\n    'rejected' =&gt; function ($e, $request) {\n        echo $e-&gt;getMessage();\n    },\n]);\n\n\/\/ \u542f\u52a8\u534f\u7a0b\u6c60\n$pool-&gt;promise()-&gt;wait();<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5728 PHP \u4e2d\u5f15\u5165\u4e86\u65b0\u7684\u53ef\u80fd\u6027\uff0c\u53ef\u4ee5\u901a\u8fc7\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u548c\u54cd\u5e94\u80fd\u529b\u6765\u63d0\u9ad8\u751f\u4ea7\u529b\u3002\u534f\u7a0b\u548c Generator \u4e3a\u5e76\u884c\u6267\u884c\u4efb\u52a1\u548c\u5f02\u6b65\u7f16\u7a0b\u63d0\u4f9b\u4e86\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\u5de5\u5177\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff1a\u5f02\u6b65\u7f16\u7a0b\u7684\u6df1\u5165\u89e3\u8bfb\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u5e76\u53d1\u7f16\u7a0b,\u5f02\u6b65\u7f16\u7a0b\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff1a\u5f02\u6b65\u7f16\u7a0b\u7684\u6df1\u5165\u89e3\u8bfb \u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\u300aPHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff1a\u5f02\u6b65\u7f16\u7a0b\u7684\u6df1\u5165\u89e3\u8bfb\u300b\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9\u6587\u7ae0\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60\u6587\u7ae0\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01 PHP \u5f15\u5165\u4e86\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff0c\u4f7f\u7528\u534f\u7a0b\u548c Generator \u5728\u5355\u4e2a\u8fdb\u7a0b\u4e2d\u5e76\u884c\u6267\u884c\u4efb\u52a1\uff0c\u53ef\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u548c\u54cd\u5e94\u80fd\u529b\u3002\u534f\u7a0b\u5141\u8bb8\u6682\u505c\u548c\u6062\u590d\u6267\u884c\uff0c\u800c Generator \u7528\u4e8e\u751f\u6210\u503c\u5e8f\u5217\u3002\u5f02\u6b65 HTTP \u8bf7\u6c42\u7b49\u5b9e\u6218\u6848\u4f8b\u5c55\u793a\u4e86\u5e76\u53d1\u7f16\u7a0b\u7684\u5b9e\u9645\u5e94\u7528\uff0c\u901a\u8fc7\u63d0\u9ad8\u751f\u4ea7\u529b\u6765\u63d0\u9ad8\u54cd\u5e94\u80fd\u529b\u3002 PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\uff1a\u5f02\u6b65\u7f16\u7a0b\u7684\u6df1\u5165\u89e3\u8bfb \u7b80\u4ecb PHP \u8bed\u8a00\u65b0\u5f15\u5165\u4e86\u534f\u7a0b\u548c Generator\uff0c\u8fd9\u4f7f\u5f97\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u6210\u4e3a\u53ef\u80fd\u3002\u5e76\u53d1\u7f16\u7a0b\u5141\u8bb8\u5728\u5355\u4e2a\u8fdb\u7a0b\u4e2d\u5e76\u884c\u6267\u884c\u591a\u4e2a\u4efb\u52a1\uff0c\u4ece\u800c\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u54cd\u5e94\u80fd\u529b\u3002 \u534f\u7a0b \u534f\u7a0b\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u6682\u505c\u548c\u6062\u590d\u6267\u884c\u3002\u5b83\u4eec\u5141\u8bb8\u5728\u4e0d\u79bb\u5f00\u5f53\u524d\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\u5207\u6362\u5230\u4e0d\u540c\u7684\u4efb\u52a1\u3002\u534f\u7a0b\u53ef\u4ee5\u901a\u8fc7 yield \u5173\u952e\u5b57\u8fdb\u884c\u6682\u505c\u548c\u6062\u590d\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a function task() { while (true) { yield; \/\/ \u6682\u505c\u534f\u7a0b echo &#8220;Hello from task\\n&#8221;; } } go(task()); \/\/ \u542f\u52a8\u534f\u7a0b \/\/ \u4e3b\u8fdb\u7a0b\u7ee7\u7eed\u6267\u884c while (true) { echo &#8220;Hello from main\\n&#8221;; } Generator Generator \u7c7b\u4f3c\u4e8e\u534f\u7a0b\uff0c\u4f46\u5b83\u6ca1\u6709\u6682\u505c\u548c\u6062\u590d\u7684\u80fd\u529b\u3002\u5b83\u4f7f\u7528 yield \u5173\u952e\u5b57\u751f\u6210\u4e00\u4e2a\u5e8f\u5217\u7684\u503c\uff0c\u800c\u65e0\u9700\u5b58\u50a8\u6574\u4e2a\u5e8f\u5217\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a [&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-46867","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46867","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=46867"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46867\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}