{"id":1094,"date":"2025-01-19T14:12:26","date_gmt":"2025-01-19T06:12:26","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1094"},"modified":"2025-01-19T20:21:06","modified_gmt":"2025-01-19T12:21:06","slug":"%e6%80%8e%e6%a0%b7%e7%9b%91%e5%90%ac%e5%92%8c%e5%a4%84%e7%90%86-swoole-%e5%bc%82%e6%ad%a5%e4%bb%bb%e5%8a%a1%e7%9a%84%e5%a4%b1%e8%b4%a5","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/19\/%e6%80%8e%e6%a0%b7%e7%9b%91%e5%90%ac%e5%92%8c%e5%a4%84%e7%90%86-swoole-%e5%bc%82%e6%ad%a5%e4%bb%bb%e5%8a%a1%e7%9a%84%e5%a4%b1%e8%b4%a5\/","title":{"rendered":"\u600e\u6837\u76d1\u542c\u548c\u5904\u7406 Swoole \u5f02\u6b65\u4efb\u52a1\u7684\u5931\u8d25"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 Swoole \u4e2d\uff0c\u5f02\u6b65\u4efb\u52a1\u5904\u7406\u5931\u8d25\u7684\u60c5\u51b5\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u4e3b\u8981\u65b9\u5f0f\u6765\u8fdb\u884c\u76d1\u542c\u548c\u5904\u7406\uff1a\u901a\u8fc7 <code>task<\/code> \u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570\u548c\u4f7f\u7528\u5f02\u6b65\u4efb\u52a1\u7684 <code>onFinish<\/code> \u56de\u8c03\u6765\u6355\u6349\u5931\u8d25\u3002\u4e0b\u9762\u662f\u8be6\u7ec6\u7684\u8bf4\u660e\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/www.laixuexila.com\/wp-content\/uploads\/2025\/01\/DALL\u00b7E-2025-01-19-20.20.47-An-illustration-of-a-technical-scenario-depicting-the-concept-of-handling-and-listening-to-asynchronous-task-failures-in-Swoole.-The-image-should-incl.webp\" alt=\"\" class=\"wp-image-1115\" srcset=\"https:\/\/www.laixuexila.com\/wp-content\/uploads\/2025\/01\/DALL\u00b7E-2025-01-19-20.20.47-An-illustration-of-a-technical-scenario-depicting-the-concept-of-handling-and-listening-to-asynchronous-task-failures-in-Swoole.-The-image-should-incl.webp 1024w, https:\/\/www.laixuexila.com\/wp-content\/uploads\/2025\/01\/DALL\u00b7E-2025-01-19-20.20.47-An-illustration-of-a-technical-scenario-depicting-the-concept-of-handling-and-listening-to-asynchronous-task-failures-in-Swoole.-The-image-should-incl-300x300.webp 300w, https:\/\/www.laixuexila.com\/wp-content\/uploads\/2025\/01\/DALL\u00b7E-2025-01-19-20.20.47-An-illustration-of-a-technical-scenario-depicting-the-concept-of-handling-and-listening-to-asynchronous-task-failures-in-Swoole.-The-image-should-incl-150x150.webp 150w, https:\/\/www.laixuexila.com\/wp-content\/uploads\/2025\/01\/DALL\u00b7E-2025-01-19-20.20.47-An-illustration-of-a-technical-scenario-depicting-the-concept-of-handling-and-listening-to-asynchronous-task-failures-in-Swoole.-The-image-should-incl-768x768.webp 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u4f7f\u7528 <code>onFinish<\/code> \u56de\u8c03<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u63d0\u4f9b\u4e86\u4e00\u4e2a <code>onFinish<\/code> \u56de\u8c03\u51fd\u6570\uff0c\u5b83\u4f1a\u5728\u5f02\u6b65\u4efb\u52a1\u5b8c\u6210\u65f6\u88ab\u8c03\u7528\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u8be5\u56de\u8c03\u6765\u68c0\u67e5\u4efb\u52a1\u662f\u5426\u6210\u529f\uff0c\u6216\u8005\u6355\u83b7\u4efb\u52a1\u5931\u8d25\u7684\u60c5\u51b5\u3002\u5728 <code>onFinish<\/code> \u4e2d\uff0c\u4f60\u53ef\u4ee5\u5224\u65ad\u4efb\u52a1\u662f\u5426\u6709\u5f02\u5e38\u5e76\u8fdb\u884c\u76f8\u5e94\u5904\u7406\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u662f\u901a\u8fc7 <code>task<\/code> \u6295\u9012\u4efb\u52a1\u65f6\u6307\u5b9a\u7684\u56de\u8c03\uff0c<code>onFinish<\/code> \u56de\u8c03\u4f1a\u88ab\u8c03\u7528\uff0c\u4e0d\u8bba\u4efb\u52a1\u662f\u6210\u529f\u8fd8\u662f\u5931\u8d25\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   &lt;?php\n   $swooleServer = new Swoole\\Server(\"127.0.0.1\", 9501);\n\n   \/\/ \u8bbe\u7f6e\u4efb\u52a1\u5b8c\u6210\u7684\u56de\u8c03\u51fd\u6570\n   $swooleServer-&gt;on('finish', function($server, $taskId, $data) {\n       if ($data === false) {\n           echo \"\u4efb\u52a1 {$taskId} \u6267\u884c\u5931\u8d25\\n\";\n       } else {\n           echo \"\u4efb\u52a1 {$taskId} \u6267\u884c\u6210\u529f\uff0c\u8fd4\u56de\u6570\u636e: {$data}\\n\";\n       }\n   });\n\n   \/\/ \u8bbe\u7f6e task \u4e8b\u4ef6\n   $swooleServer-&gt;on('task', function($server, $taskId, $fromId, $data) {\n       \/\/ \u6a21\u62df\u5f02\u6b65\u4efb\u52a1\u5904\u7406\n       if ($data == 'fail') {\n           \/\/ \u6a21\u62df\u5931\u8d25\n           return false;  \/\/ \u4efb\u52a1\u5931\u8d25\n       } else {\n           \/\/ \u6a21\u62df\u6210\u529f\n           return \"\u4efb\u52a1\u6210\u529f: \" . $data; \/\/ \u4efb\u52a1\u6210\u529f\n       }\n   });\n\n   \/\/ \u542f\u52a8\u670d\u52a1\u5668\n   $swooleServer-&gt;start();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4efb\u52a1\u6570\u636e\u4e3a <code>'fail'<\/code> \u65f6\uff0c\u4efb\u52a1\u4f1a\u8fd4\u56de <code>false<\/code>\uff0c<code>onFinish<\/code> \u56de\u8c03\u4f1a\u6536\u5230\u8fd9\u4e2a\u7ed3\u679c\uff0c\u5e76\u6253\u5370\u51fa\u4efb\u52a1\u5931\u8d25\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u76d1\u542c <code>task<\/code> \u4e8b\u4ef6\u7684\u9519\u8bef<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>task<\/code> \u4e8b\u4ef6\u672c\u8eab\u5e76\u4e0d\u4f1a\u76f4\u63a5\u63d0\u4f9b\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u4f46\u662f\u53ef\u4ee5\u5728\u4efb\u52a1\u6267\u884c\u671f\u95f4\u8fdb\u884c\u5f02\u5e38\u6355\u6349\uff0c\u6765\u76d1\u542c\u4efb\u52a1\u6267\u884c\u5931\u8d25\u7684\u60c5\u51b5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5728\u4efb\u52a1\u6267\u884c\u4e2d\u4f7f\u7528\u4e86 <code>try-catch<\/code> \u6765\u6355\u83b7\u5f02\u5e38\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd4\u56de\u503c\u6216\u901a\u8fc7 <code>onFinish<\/code> \u6765\u6355\u83b7\u4efb\u52a1\u5931\u8d25\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   &lt;?php\n   $swooleServer = new Swoole\\Server(\"127.0.0.1\", 9501);\n\n   \/\/ \u8bbe\u7f6e task \u4e8b\u4ef6\n   $swooleServer-&gt;on('task', function($server, $taskId, $fromId, $data) {\n       try {\n           \/\/ \u6a21\u62df\u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801\n           if ($data === 'fail') {\n               throw new Exception('\u4efb\u52a1\u6267\u884c\u5931\u8d25');\n           }\n           \/\/ \u5982\u679c\u6ca1\u6709\u5f02\u5e38\uff0c\u5219\u4efb\u52a1\u6210\u529f\n           return \"\u4efb\u52a1\u6210\u529f: \" . $data;\n       } catch (Exception $e) {\n           \/\/ \u6355\u83b7\u5230\u5f02\u5e38\uff0c\u4efb\u52a1\u5931\u8d25\n           return false;\n       }\n   });\n\n   \/\/ \u8bbe\u7f6e\u4efb\u52a1\u5b8c\u6210\u56de\u8c03\n   $swooleServer-&gt;on('finish', function($server, $taskId, $data) {\n       if ($data === false) {\n           echo \"\u4efb\u52a1 {$taskId} \u6267\u884c\u5931\u8d25\\n\";\n       } else {\n           echo \"\u4efb\u52a1 {$taskId} \u6267\u884c\u6210\u529f\uff0c\u8fd4\u56de\u6570\u636e: {$data}\\n\";\n       }\n   });\n\n   \/\/ \u542f\u52a8\u670d\u52a1\u5668\n   $swooleServer-&gt;start();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f53\u6570\u636e\u4e3a <code>'fail'<\/code> \u65f6\uff0c\u4efb\u52a1\u4f1a\u629b\u51fa\u5f02\u5e38\uff0c<code>task<\/code> \u4e8b\u4ef6\u4e2d\u7684 <code>catch<\/code> \u4f1a\u6355\u83b7\u8be5\u5f02\u5e38\uff0c\u5e76\u8fd4\u56de <code>false<\/code>\uff0c\u8868\u660e\u4efb\u52a1\u5931\u8d25\u3002<code>onFinish<\/code> \u4f1a\u88ab\u89e6\u53d1\uff0c\u5e76\u5904\u7406\u5931\u8d25\u7684\u60c5\u51b5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u4f7f\u7528 Task \u5f02\u5e38\u673a\u5236<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u672c\u8eab\u4e5f\u63d0\u4f9b\u4e86\u9519\u8bef\u7801\u548c\u9519\u8bef\u4fe1\u606f\u7684\u8fd4\u56de\u673a\u5236\u3002\u5728\u5f02\u6b65\u4efb\u52a1\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd4\u56de\u9519\u8bef\u7801\u6765\u901a\u77e5\u4efb\u52a1\u6267\u884c\u7684\u5931\u8d25\uff0c\u4efb\u52a1\u5b8c\u6210\u540e\uff0c\u5728 <code>finish<\/code> \u56de\u8c03\u4e2d\u5904\u7406\u5931\u8d25\u60c5\u51b5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   &lt;?php\n   $swooleServer = new Swoole\\Server(\"127.0.0.1\", 9501);\n\n   \/\/ \u8bbe\u7f6e\u4efb\u52a1\u5b8c\u6210\u7684\u56de\u8c03\u51fd\u6570\n   $swooleServer-&gt;on('finish', function($server, $taskId, $data) {\n       if ($data === 'error') {\n           echo \"\u4efb\u52a1 {$taskId} \u6267\u884c\u5931\u8d25: \u9519\u8bef\u4fe1\u606f\\n\";\n       } else {\n           echo \"\u4efb\u52a1 {$taskId} \u6267\u884c\u6210\u529f: \" . $data . \"\\n\";\n       }\n   });\n\n   \/\/ \u8bbe\u7f6e\u4efb\u52a1\u4e8b\u4ef6\n   $swooleServer-&gt;on('task', function($server, $taskId, $fromId, $data) {\n       if ($data === 'fail') {\n           \/\/ \u8fd4\u56de\u9519\u8bef\u4fe1\u606f\n           return 'error';\n       }\n       return \"\u4efb\u52a1\u6210\u529f: \" . $data;\n   });\n\n   \/\/ \u542f\u52a8\u670d\u52a1\u5668\n   $swooleServer-&gt;start();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u4efb\u52a1\u8fd4\u56de <code>'error'<\/code>\uff0c<code>finish<\/code> \u4e8b\u4ef6\u5c06\u4f1a\u6355\u83b7\u5230\u8be5\u4fe1\u606f\u5e76\u8f93\u51fa\u4efb\u52a1\u5931\u8d25\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3\uff1a<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u76d1\u542c\u548c\u5904\u7406 Swoole \u5f02\u6b65\u4efb\u52a1\u7684\u5931\u8d25\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>onFinish<\/code> \u56de\u8c03\u6765\u5224\u65ad\u4efb\u52a1\u662f\u5426\u6210\u529f\uff0c\u901a\u8fc7\u8fd4\u56de\u503c\u6216\u9519\u8bef\u7801\u6765\u6807\u8bc6\u4efb\u52a1\u5931\u8d25\u3002<\/li>\n\n\n\n<li>\u5728 <code>task<\/code> \u4e8b\u4ef6\u4e2d\u901a\u8fc7\u5f02\u5e38\u6355\u83b7\u6765\u5224\u65ad\u4efb\u52a1\u6267\u884c\u5931\u8d25\u5e76\u8fd4\u56de\u9519\u8bef\u3002<\/li>\n\n\n\n<li>\u5728\u4efb\u52a1\u5b8c\u6210\u540e\u68c0\u67e5\u8fd4\u56de\u503c\u662f\u5426\u4e3a <code>false<\/code> \u6216\u81ea\u5b9a\u4e49\u9519\u8bef\u6807\u8bc6\uff0c\u6765\u5904\u7406\u5931\u8d25\u4efb\u52a1\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u6837\uff0c\u4f60\u5c31\u80fd\u5728\u4efb\u52a1\u6267\u884c\u5931\u8d25\u65f6\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u4f7f\u7528 onFinish \u56de\u8c03 Swoole \u63d0\u4f9b\u4e86\u4e00\u4e2a onFinish \u56de\u8c03\u51fd\u6570\uff0c\u5b83\u4f1a\u5728\u5f02\u6b65\u4efb\u52a1\u5b8c\u6210\u65f6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64],"tags":[],"class_list":["post-1094","post","type-post","status-publish","format-standard","hentry","category-swoole"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1094","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1094"}],"version-history":[{"count":2,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1094\/revisions"}],"predecessor-version":[{"id":1116,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1094\/revisions\/1116"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}