{"id":46879,"date":"2024-12-02T16:40:52","date_gmt":"2024-12-02T08:40:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46879\/"},"modified":"2024-12-02T16:40:52","modified_gmt":"2024-12-02T08:40:52","slug":"php-%e5%87%bd%e6%95%b0%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b%e5%9c%a8%e5%be%ae%e6%9c%8d%e5%8a%a1%e6%9e%b6%e6%9e%84%e4%b8%ad%e7%9a%84%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46879\/","title":{"rendered":"PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\u7684\u5e94\u7528"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\u7684\u5e94\u7528<\/h1>\n<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\u7c73\u4e91\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\u7684\u5e94\u7528\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p>\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\uff0c\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u63d0\u9ad8\u6027\u80fd\u548c\u53ef\u6269\u5c55\u6027\u3002PHP \u63d0\u4f9b\u4e86\u534f\u7a0b\u548c\u5e76\u53d1\u673a\u5236\u6765\u5e76\u884c\u6267\u884c\u51fd\u6570\u3002\u534f\u7a0b\u662f\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u5171\u4eab\u5185\u5b58\u7a7a\u95f4\u3002\u5e76\u53d1\u4f7f\u7528 Task \u548c Sync \u7c7b\u3002\u51fd\u6570\u5e76\u53d1\u53ef\u7528\u4e8e API \u8bf7\u6c42\u805a\u5408\uff0c\u5c06\u8bf7\u6c42\u5e76\u884c\u5316\u4ee5\u51cf\u5c11\u54cd\u5e94\u65f6\u95f4\u3002\u5728\u56fe\u50cf\u5904\u7406\u4e2d\uff0c\u51fd\u6570\u5e76\u53d1\u53ef\u5206\u89e3\u64cd\u4f5c\u5e76\u5e76\u884c\u6267\u884c\uff0c\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\u3002\u4f7f\u7528\u51fd\u6570\u5e76\u53d1\u5177\u6709\u63d0\u9ad8\u6027\u80fd\u3001\u7b80\u5316\u4ee3\u7801\u548c\u63d0\u9ad8\u53ef\u6269\u5c55\u6027\u7b49\u4f18\u52bf\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729851941671b722526f06.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\u7684\u5e94\u7528\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\u7684\u5e94\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\u7684\u5e94\u7528<\/strong><\/p>\n<p>\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\uff0c\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u662f\u4e00\u79cd\u91cd\u8981\u7684\u6280\u672f\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u5355\u4e2a\u8fdb\u7a0b\u4e2d\u5e76\u884c\u6267\u884c\u591a\u4e2a\u51fd\u6570\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u548c\u53ef\u6269\u5c55\u6027\u3002PHP \u63d0\u4f9b\u4e86\u4e24\u79cd\u4e3b\u8981\u7684\u51fd\u6570\u5e76\u53d1\u673a\u5236\uff1a\u534f\u7a0b\u548c\u5e76\u53d1\u3002<\/p>\n<p><strong>\u534f\u7a0b<\/strong><\/p>\n<p>\u534f\u7a0b\u662f\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u5b83\u4eec\u5171\u4eab\u76f8\u540c\u7684\u5185\u5b58\u7a7a\u95f4\u3002\u5728 PHP \u4e2d\uff0c\u534f\u7a0b\u53ef\u4ee5\u4f7f\u7528 <code>Generator<\/code> \u548c <code>Fiber<\/code> \u7c7b\u8fdb\u884c\u521b\u5efa\u3002<\/p>\n<pre>function coroutine() {\n    yield 'Hello';\n    yield 'World';\n}\n\n$coroutine = new coroutine();\nforeach ($coroutine as $value) {\n    echo $value . PHP_EOL;\n}<\/pre>\n<p><strong>\u5e76\u53d1<\/strong><\/p>\n<p>PHP 8.1 \u4e2d\u5f15\u5165\u4e86\u5e76\u53d1\u7279\u6027\uff0c\u5b83\u4f7f\u7528 <code>Task<\/code> \u548c <code>Sync<\/code> \u7c7b\u6765\u5e76\u53d1\u6267\u884c\u51fd\u6570\u3002<\/p>\n<pre>use Swoole\\Coroutine;\n\nfunction task() {\n    Coroutine::sleep(1);\n    return 'Hello';\n}\n\n$task = new Task('task');\n$result = $task-&gt;getResult();\necho $result . PHP_EOL;<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>API \u8bf7\u6c42\u805a\u5408<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a \u5fae\u670d\u52a1\uff0c\u7528\u6765\u805a\u5408\u6765\u81ea\u591a\u4e2a API \u670d\u52a1\u7684\u54cd\u5e94\u3002\u4f7f\u7528\u51fd\u6570\u5e76\u53d1\uff0c\u6211\u4eec\u53ef\u4ee5\u5e76\u884c\u8bf7\u6c42\u8fd9\u4e9b\u670d\u52a1\uff0c\u4ece\u800c\u51cf\u5c11\u6574\u4f53\u54cd\u5e94\u65f6\u95f4\u3002<\/p>\n<pre>$services = [\n    'service1',\n    'service2',\n    'service3',\n];\n\n$requests = [];\nforeach ($services as $service) {\n    $requests[] = new Task(function () use ($service) {\n        \/\/ \u8bf7\u6c42\u670d\u52a1\u5e76\u8fd4\u56de\u7ed3\u679c\n    });\n}\n\n$results = [];\nforeach ($requests as $request) {\n    $results[] = $request-&gt;getResult();\n}<\/pre>\n<p><strong>\u56fe\u50cf\u5904\u7406<\/strong><\/p>\n<p>\u56fe\u50cf\u5904\u7406\u901a\u5e38\u662f CPU \u5bc6\u96c6\u578b\u7684\u4efb\u52a1\u3002\u4f7f\u7528\u51fd\u6570\u5e76\u53d1\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\u5206\u89e3\u4e3a\u591a\u4e2a\u8f83\u5c0f\u7684\u4efb\u52a1\uff0c\u5e76\u5e76\u884c\u6267\u884c\u5b83\u4eec\u3002<\/p>\n<pre>use Swoole\\Coroutine\\System;\n\n$image = imagecreatefromjpeg('image.jpg');\n\n$tasks = [];\nfor ($i = 0; $i &lt; 10; $i++) {\n    $height = imagesy($image) \/ 10;\n    $tasks[] = new Task(function () use ($image, $i, $height) {\n        \/\/ \u5904\u7406\u56fe\u50cf\u7684\u7b2c i \u4e2a\u90e8\u5206\n    });\n}\n\nforeach ($tasks as $task) {\n    System::sleep(0.001);\n}<\/pre>\n<p><strong>\u4f18\u52bf<\/strong><\/p>\n<ul>\n<li><strong>\u63d0\u9ad8\u6027\u80fd\uff1a<\/strong>\u51fd\u6570\u5e76\u53d1\u53ef\u4ee5\u51cf\u5c11\u7b49\u5f85\u65f6\u95f4\uff0c\u63d0\u9ad8\u5fae\u670d\u52a1\u7684\u6574\u4f53\u6027\u80fd\u3002<\/li>\n<li><strong>\u63d0\u9ad8\u53ef\u6269\u5c55\u6027\uff1a<\/strong>\u901a\u8fc7\u5229\u7528\u591a\u4e2a CPU \u6838\u5fc3\u8fdb\u884c\u5e76\u884c\u5904\u7406\uff0c\u51fd\u6570\u5e76\u53d1\u53ef\u4ee5\u6269\u5c55\u5fae\u670d\u52a1\u4ee5\u5904\u7406\u66f4\u9ad8\u7684\u8d1f\u8f7d\u3002<\/li>\n<li><strong>\u7b80\u5316\u4ee3\u7801\uff1a<\/strong>\u51fd\u6570\u5e76\u53d1\u907f\u514d\u4e86\u4f7f\u7528\u590d\u6742\u7684\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u540c\u6b65\u673a\u5236\uff0c\u4ece\u800c\u7b80\u5316\u4e86\u4ee3\u7801\u5f00\u53d1\u548c\u7ef4\u62a4\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aPHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\u7684\u5e94\u7528\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\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\u7684\u5e94\u7528 \u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\u7c73\u4e91\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u300aPHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\u7684\u5e94\u7528\u300b\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01 \u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\uff0c\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u63d0\u9ad8\u6027\u80fd\u548c\u53ef\u6269\u5c55\u6027\u3002PHP \u63d0\u4f9b\u4e86\u534f\u7a0b\u548c\u5e76\u53d1\u673a\u5236\u6765\u5e76\u884c\u6267\u884c\u51fd\u6570\u3002\u534f\u7a0b\u662f\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u5171\u4eab\u5185\u5b58\u7a7a\u95f4\u3002\u5e76\u53d1\u4f7f\u7528 Task \u548c Sync \u7c7b\u3002\u51fd\u6570\u5e76\u53d1\u53ef\u7528\u4e8e API \u8bf7\u6c42\u805a\u5408\uff0c\u5c06\u8bf7\u6c42\u5e76\u884c\u5316\u4ee5\u51cf\u5c11\u54cd\u5e94\u65f6\u95f4\u3002\u5728\u56fe\u50cf\u5904\u7406\u4e2d\uff0c\u51fd\u6570\u5e76\u53d1\u53ef\u5206\u89e3\u64cd\u4f5c\u5e76\u5e76\u884c\u6267\u884c\uff0c\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\u3002\u4f7f\u7528\u51fd\u6570\u5e76\u53d1\u5177\u6709\u63d0\u9ad8\u6027\u80fd\u3001\u7b80\u5316\u4ee3\u7801\u548c\u63d0\u9ad8\u53ef\u6269\u5c55\u6027\u7b49\u4f18\u52bf\u3002 PHP \u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\u7684\u5e94\u7528 \u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\uff0c\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u662f\u4e00\u79cd\u91cd\u8981\u7684\u6280\u672f\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u5355\u4e2a\u8fdb\u7a0b\u4e2d\u5e76\u884c\u6267\u884c\u591a\u4e2a\u51fd\u6570\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u548c\u53ef\u6269\u5c55\u6027\u3002PHP \u63d0\u4f9b\u4e86\u4e24\u79cd\u4e3b\u8981\u7684\u51fd\u6570\u5e76\u53d1\u673a\u5236\uff1a\u534f\u7a0b\u548c\u5e76\u53d1\u3002 \u534f\u7a0b \u534f\u7a0b\u662f\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u5b83\u4eec\u5171\u4eab\u76f8\u540c\u7684\u5185\u5b58\u7a7a\u95f4\u3002\u5728 PHP \u4e2d\uff0c\u534f\u7a0b\u53ef\u4ee5\u4f7f\u7528 Generator \u548c Fiber \u7c7b\u8fdb\u884c\u521b\u5efa\u3002 function coroutine() { yield &#8216;Hello&#8217;; yield &#8216;World&#8217;; } $coroutine = new coroutine(); foreach ($coroutine as $value) { echo $value . PHP_EOL; } \u5e76\u53d1 PHP 8.1 \u4e2d\u5f15\u5165\u4e86\u5e76\u53d1\u7279\u6027\uff0c\u5b83\u4f7f\u7528 Task \u548c Sync \u7c7b\u6765\u5e76\u53d1\u6267\u884c\u51fd\u6570\u3002 use [&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-46879","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46879","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=46879"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46879\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}