{"id":47138,"date":"2024-12-02T09:02:14","date_gmt":"2024-12-02T01:02:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47138\/"},"modified":"2024-12-02T09:02:14","modified_gmt":"2024-12-02T01:02:14","slug":"php-%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e5%92%8c%e4%bd%bf%e7%94%a8%e5%8d%8f%e7%a8%8b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47138\/","title":{"rendered":"PHP \u51fd\u6570\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528\u534f\u7a0b\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528\u534f\u7a0b\uff1f<\/h1>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300aPHP \u51fd\u6570\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528\u534f\u7a0b\uff1f\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p>PHP \u4e2d\u7684\u534f\u7a0b\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u53ef\u5728\u4e0d\u5360\u7528\u8fc7\u591a\u8d44\u6e90\u7684\u60c5\u51b5\u4e0b\u5e76\u53d1\u5904\u7406\u4efb\u52a1\u3002\u521b\u5efa\u534f\u7a0b\u4f7f\u7528 go() \u51fd\u6570\uff0c\u5e76\u901a\u8fc7 run() \u6216 await() \u542f\u52a8\u3002\u53ef\u901a\u8fc7 send() \u53d1\u9001\u503c\uff0c\u901a\u8fc7 recv() \u63a5\u6536\u503c\u3002\u5b9e\u6218\u7528\u4f8b\u5305\u62ec\u5f02\u6b65\u5904\u7406 HTTP \u8bf7\u6c42\uff0c\u4ece\u800c\u63d0\u5347\u670d\u52a1\u5668\u541e\u5410\u91cf\u548c\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729867853671bb04d929e9.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528\u534f\u7a0b\uff1f\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528\u534f\u7a0b\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u7684\u534f\u7a0b\uff1a\u521b\u5efa\u4e0e\u4f7f\u7528<\/strong><\/p>\n<p>\u534f\u7a0b\u662f\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u5728\u4e0d\u5360\u7528\u8fc7\u591a\u7cfb\u7edf\u8d44\u6e90\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5e76\u53d1\u5904\u7406\u591a\u4e2a\u4efb\u52a1\u3002PHP \u81ea 8.1 \u7248\u672c\u8d77\u652f\u6301\u534f\u7a0b\uff0c\u6269\u5145\u4e86\u8be5\u8bed\u8a00\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002<\/p>\n<p><strong>\u521b\u5efa\u534f\u7a0b<\/strong><\/p>\n<p>\u8981\u521b\u5efa\u534f\u7a0b\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>go()<\/code> \u51fd\u6570\uff1a<\/p>\n<pre>&lt;?php\n$coroutine = go(function () {\n    \/\/ \u534f\u7a0b\u7684\u4ee3\u7801\u5757\n});<\/pre>\n<p><code>go()<\/code> \u51fd\u6570\u4f1a\u63a5\u6536\u4e00\u4e2a\u533f\u540d\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u5373\u6784\u6210\u4e86\u534f\u7a0b\u8981\u6267\u884c\u7684\u4efb\u52a1\u3002<\/p>\n<p><strong>\u542f\u52a8\u534f\u7a0b<\/strong><\/p>\n<p>\u521b\u5efa\u534f\u7a0b\u540e\uff0c\u8fd8\u9700\u8981\u624b\u52a8\u542f\u52a8\u5b83\u3002\u6709\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u542f\u52a8\u534f\u7a0b\uff1a<\/p>\n<ol>\n<li><strong>\u540c\u6b65\u542f\u52a8<\/strong>\uff1a\u4f7f\u7528 <code>run()<\/code> \u51fd\u6570\uff1a<\/li>\n<\/ol>\n<pre>$result = $coroutine-&gt;run();<\/pre>\n<ol>\n<li><strong>\u5f02\u6b65\u542f\u52a8<\/strong>\uff1a\u4f7f\u7528 <code>await()<\/code> \u51fd\u6570\uff1a<\/li>\n<\/ol>\n<pre>$result = await $coroutine;<\/pre>\n<p>\u5f02\u6b65\u542f\u52a8\u53ef\u4ee5\u8ba9\u534f\u7a0b\u5728\u4e3b\u7ebf\u7a0b\u5b8c\u6210\u540e\u518d\u6267\u884c\uff0c\u800c\u540c\u6b65\u542f\u52a8\u5219\u4f1a\u7acb\u5373\u6267\u884c\u534f\u7a0b\u3002<\/p>\n<p><strong>\u53d1\u9001\u503c\u5230\u534f\u7a0b<\/strong><\/p>\n<p>\u53ef\u4ee5\u5411\u534f\u7a0b\u53d1\u9001\u503c\uff0c\u5c31\u50cf\u4f7f\u7528 <code>yield<\/code> \u8868\u8fbe\u5f0f\u53d1\u9001\u503c\u7ed9\u751f\u6210\u5668\u4e00\u6837\uff1a<\/p>\n<pre>$coroutine-&gt;send('Hello');<\/pre>\n<p><strong>\u4ece\u534f\u7a0b\u63a5\u6536\u503c<\/strong><\/p>\n<p>\u4e5f\u53ef\u4ee5\u4ece\u534f\u7a0b\u63a5\u6536\u503c\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7 <code>recv()<\/code> \u65b9\u6cd5\u5b9e\u73b0\uff1a<\/p>\n<pre>$value = $coroutine-&gt;recv();<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u534f\u7a0b\u5f02\u6b65\u5904\u7406 HTTP \u8bf7\u6c42\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>use Amp\\Http\\Server\\Server;\nuse Amp\\Http\\Server\\Request;\nuse Amp\\Http\\Server\\Response;\nuse Amp\\Http\\Server\\Middleware;\n\n$middleware = Middleware::wrap(function (Request $request, Response $response) {\n    \/\/ \u534f\u7a0b\uff1a\u5904\u7406\u8bf7\u6c42\u5e76\u53d1\u9001\u54cd\u5e94\n    $coroutine = go(function () use ($request, $response) {\n        \/\/ ...\n        $response-&gt;writeHead(200, ['Content-Type' =&gt; 'text\/plain']);\n        $response-&gt;end('Hello, world!');\n    });\n    return $coroutine;\n});\n\n$server = new Server('0.0.0.0', 8000, $middleware);\n$server-&gt;start();<\/pre>\n<p>\u5728\u8be5\u793a\u4f8b\u4e2d\uff0c\u534f\u7a0b\u88ab\u7528\u4f5c\u4e2d\u95f4\u4ef6\uff0c\u6765\u5f02\u6b65\u5904\u7406\u4f20\u5165\u7684 HTTP \u8bf7\u6c42\u3002\u8fd9\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u670d\u52a1\u7aef\u7684\u541e\u5410\u91cf\u548c\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aPHP \u51fd\u6570\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528\u534f\u7a0b\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528\u534f\u7a0b\uff1f IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300aPHP \u51fd\u6570\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528\u534f\u7a0b\uff1f\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01 PHP \u4e2d\u7684\u534f\u7a0b\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u53ef\u5728\u4e0d\u5360\u7528\u8fc7\u591a\u8d44\u6e90\u7684\u60c5\u51b5\u4e0b\u5e76\u53d1\u5904\u7406\u4efb\u52a1\u3002\u521b\u5efa\u534f\u7a0b\u4f7f\u7528 go() \u51fd\u6570\uff0c\u5e76\u901a\u8fc7 run() \u6216 await() \u542f\u52a8\u3002\u53ef\u901a\u8fc7 send() \u53d1\u9001\u503c\uff0c\u901a\u8fc7 recv() \u63a5\u6536\u503c\u3002\u5b9e\u6218\u7528\u4f8b\u5305\u62ec\u5f02\u6b65\u5904\u7406 HTTP \u8bf7\u6c42\uff0c\u4ece\u800c\u63d0\u5347\u670d\u52a1\u5668\u541e\u5410\u91cf\u548c\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002 PHP \u7684\u534f\u7a0b\uff1a\u521b\u5efa\u4e0e\u4f7f\u7528 \u534f\u7a0b\u662f\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u5728\u4e0d\u5360\u7528\u8fc7\u591a\u7cfb\u7edf\u8d44\u6e90\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5e76\u53d1\u5904\u7406\u591a\u4e2a\u4efb\u52a1\u3002PHP \u81ea 8.1 \u7248\u672c\u8d77\u652f\u6301\u534f\u7a0b\uff0c\u6269\u5145\u4e86\u8be5\u8bed\u8a00\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002 \u521b\u5efa\u534f\u7a0b \u8981\u521b\u5efa\u534f\u7a0b\uff0c\u53ef\u4ee5\u4f7f\u7528 go() \u51fd\u6570\uff1a &lt;?php $coroutine = go(function () { \/\/ \u534f\u7a0b\u7684\u4ee3\u7801\u5757 }); go() \u51fd\u6570\u4f1a\u63a5\u6536\u4e00\u4e2a\u533f\u540d\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u5373\u6784\u6210\u4e86\u534f\u7a0b\u8981\u6267\u884c\u7684\u4efb\u52a1\u3002 \u542f\u52a8\u534f\u7a0b \u521b\u5efa\u534f\u7a0b\u540e\uff0c\u8fd8\u9700\u8981\u624b\u52a8\u542f\u52a8\u5b83\u3002\u6709\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u542f\u52a8\u534f\u7a0b\uff1a \u540c\u6b65\u542f\u52a8\uff1a\u4f7f\u7528 run() \u51fd\u6570\uff1a $result = $coroutine-&gt;run(); \u5f02\u6b65\u542f\u52a8\uff1a\u4f7f\u7528 await() \u51fd\u6570\uff1a $result = await $coroutine; \u5f02\u6b65\u542f\u52a8\u53ef\u4ee5\u8ba9\u534f\u7a0b\u5728\u4e3b\u7ebf\u7a0b\u5b8c\u6210\u540e\u518d\u6267\u884c\uff0c\u800c\u540c\u6b65\u542f\u52a8\u5219\u4f1a\u7acb\u5373\u6267\u884c\u534f\u7a0b\u3002 [&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-47138","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47138","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=47138"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47138\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}