{"id":23528,"date":"2024-11-21T16:49:23","date_gmt":"2024-11-21T08:49:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23528\/"},"modified":"2024-11-21T16:49:23","modified_gmt":"2024-11-21T08:49:23","slug":"swoole%e5%ae%9e%e8%b7%b5%e7%bb%8f%e9%aa%8c%ef%bc%9a%e4%bd%bf%e7%94%a8%e5%8d%8f%e7%a8%8b%e8%bf%9b%e8%a1%8c%e9%ab%98%e5%b9%b6%e5%8f%91%e9%82%ae%e4%bb%b6%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23528\/","title":{"rendered":"Swoole\u5b9e\u8df5\u7ecf\u9a8c\uff1a\u4f7f\u7528\u534f\u7a0b\u8fdb\u884c\u9ad8\u5e76\u53d1\u90ae\u4ef6\u5904\u7406"},"content":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u6280\u672f\u7684\u98de\u901f\u53d1\u5c55\u548c\u5e94\u7528\u573a\u666f\u7684\u4e0d\u65ad\u62d3\u5c55\uff0c\u90ae\u4ef6\u670d\u52a1\u5df2\u7ecf\u6210\u4e3a\u4f01\u4e1a\/\u4e2a\u4eba\u65e5\u5e38\u5de5\u4f5c\u6240\u5fc5\u4e0d\u53ef\u5c11\u7684\u4e00\u90e8\u5206\u3002\u7136\u800c\uff0c\u5728\u5927\u89c4\u6a21\u7684\u90ae\u4ef6\u53d1\u9001\u4e2d\uff0c\u9ad8\u5e76\u53d1\u5f80\u5f80\u4f1a\u6210\u4e3a\u74f6\u9888\uff0c\u4f8b\u5982\u9891\u7e41\u7684\u8fde\u63a5\u6216\u91cd\u8fdesmtp\u670d\u52a1\u5668\u3001\u6d88\u606f\u961f\u5217\u6d88\u8d39\u7b49\u64cd\u4f5c\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u90fd\u9700\u8981\u6d88\u8017\u8f83\u591a\u7684\u65f6\u95f4\u548c\u7cfb\u7edf\u8d44\u6e90\uff0c\u5f71\u54cd\u6574\u4e2a\u90ae\u4ef6\u53d1\u9001\u8fc7\u7a0b\u7684\u6548\u7387\u3002\u90a3\u4e48\uff0c\u5982\u4f55\u7528\u6700\u5c0f\u7684\u8d44\u6e90\u5b9e\u73b0\u90ae\u4ef6\u7684\u9ad8\u6548\u5904\u7406\u5462\uff1f<\/p>\n<p>\u5b9e\u8df5\u8868\u660e\uff0c\u5728Swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u8fdb\u884c\u9ad8\u5e76\u53d1\u90ae\u4ef6\u5904\u7406\u662f\u4e00\u79cd\u975e\u5e38\u53ef\u884c\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u672c\u7bc7\u6587\u7ae0\u5c06\u5c31\u5982\u4f55\u4f7f\u7528Swoole\u7684\u534f\u7a0b\u7279\u6027\uff0c\u5b9e\u73b0\u9ad8\u5e76\u53d1\u90ae\u4ef6\u5904\u7406\uff0c\u505a\u4e00\u4e9b\u5f00\u53d1\u5b9e\u8df5\u4ecb\u7ecd\u3002<\/p>\n<p>\u4e00\u3001\u534f\u7a0b\u4ecb\u7ecd<\/p>\n<p>\u534f\u7a0b\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u5373\u53ef\u4ee5\u770b\u4f5c\u662f\u8fdb\u7a0b\u548c\u7ebf\u7a0b\u4e4b\u95f4\u6298\u4e2d\u7684\u4e00\u79cd\u65b9\u6848\u3002\u534f\u7a0b\u6709\u4ee5\u4e0b\u51e0\u4e2a\u7279\u70b9\uff1a<\/p>\n<ol>\n<li>\u4f4e\u6210\u672c\u7684\u4e0a\u4e0b\u6587\u5207\u6362<\/li>\n<li>\u65e0\u9700\u9501\u673a\u5236<\/li>\n<li>\u8f7b\u91cf\u7ea7\u5360\u7528\u8d44\u6e90<\/li>\n<li>\u591a\u4efb\u52a1\u534f\u4f5c<\/li>\n<\/ol>\n<p>\u5728Swoole\u7684\u534f\u7a0b\u7279\u6027\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51fd\u6570\u8fdb\u884c\u534f\u7a0b\u64cd\u4f5c\uff1a<\/p>\n<ol>\n<li>SwooleCoroutineun(callable $fn)\uff1a\u542f\u52a8\u4e00\u4e2aSwoole\u534f\u7a0b\u3002<\/li>\n<li>SwooleCoroutinecreate(callable $fn, bool $coroutine_params)\uff1a\u521b\u5efa\u4e00\u4e2a\u534f\u7a0b\u3002<\/li>\n<li>SwooleCoroutine::yield()\uff1a\u6682\u505c\u5f53\u524d\u534f\u7a0b\u7684\u6267\u884c\uff0c\u5e76\u4e0d\u4f1a\u91ca\u653eCPU\u8d44\u6e90\u3002<\/li>\n<li>SwooleCoroutine::resume($coroutine_id)\uff1a\u6062\u590d\u6307\u5b9a\u7684\u534f\u7a0b\u3002<\/li>\n<\/ol>\n<p>\u4e8c\u3001\u5b9e\u8df5\uff1a\u4f7f\u7528Swoole\u7684\u534f\u7a0b\u7279\u6027\u5b9e\u73b0\u9ad8\u5e76\u53d1\u90ae\u4ef6\u5904\u7406<\/p>\n<ol>\n<li>\u521d\u59cb\u5316SMTP\u5ba2\u6237\u7aef<\/li>\n<\/ol>\n<p>\u4f7f\u7528Swoole\u7684\u534f\u7a0b\u7279\u6027\u6765\u5904\u7406\u90ae\u4ef6\u53d1\u9001\u662f\u76f8\u5f53\u7b80\u5355\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u6839\u636ePHPMailer\u5e93\u6765\u53d1\u9001\u90ae\u4ef6\u3002<\/p>\n<p>\u9996\u5148\u9700\u8981\u521d\u59cb\u5316SMTP\u5ba2\u6237\u7aef\uff0c\u5e76\u8bbe\u7f6eSMTP\u670d\u52a1\u5668\u76f8\u5173\u7684\u53c2\u6570\uff1a<\/p>\n<pre>try {\n   $mail = new PHPMailer;\n   $mail-&amp;gt;isSMTP();\n   $mail-&amp;gt;SMTPDebug  = 0;\n   $mail-&amp;gt;SMTPAuth  = true;\n   $mail-&amp;gt;SMTPSecure = 'tls';\n   $mail-&amp;gt;Host   = \"smtp.example.com\";\n   $mail-&amp;gt;Port   = \"465\";\n   $mail-&amp;gt;CharSet = \"utf-8\";\n   $mail-&amp;gt;Username = \"user@example.com\";\n   $mail-&amp;gt;Password = \"password\";\n   $mail-&amp;gt;setFrom('user@example.com', 'Mailer');\n   $mail-&amp;gt;addAddress('recipient@example.com', 'Recipient');\n   $mail-&amp;gt;isHTML(true);\n   $mail-&amp;gt;Subject = 'Test email';\n   $mail-&amp;gt;Body    = 'This is the HTML message body &lt;b&gt;in bold!&lt;\/b&gt;';\n   $mail-&amp;gt;AltBody = 'This is the body in plain text for non-HTML mail clients';\n}\ncatch (Exception $e) {\n   echo \"Message could not be sent. Mailer Error: {$mail-&amp;gt;ErrorInfo}\";\n   exit;\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u4f7f\u7528\u534f\u7a0b\u8fdb\u884c\u9ad8\u5e76\u53d1\u90ae\u4ef6\u53d1\u9001<\/li>\n<\/ol>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528Swoole\u7684\u534f\u7a0b\u7279\u6027\uff0c\u5e76\u53d1\u5730\u53d1\u9001\u591a\u4e2a\u90ae\u4ef6\uff1a<\/p>\n<pre>$tasks = array();\nfor ($i = 0; $i send();\n       if (!$result) {\n           echo \"Mailer Error: {$mail-&amp;gt;ErrorInfo}\n\";\n       } else {\n           echo \"Message sent successfully!\n\";\n       }\n   });\n}\nSwooleCoroutine::wait($tasks);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528SwooleCoroutine::create()\u51fd\u6570\u6765\u521b\u5efa\u591a\u4e2a\u534f\u7a0b\u3002\u8fd9\u4e9b\u534f\u7a0b\u5c06\u4f1a\u540c\u65f6\u53d1\u9001\u591a\u4e2a\u90ae\u4ef6\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528SwooleCoroutine::wait()\u51fd\u6570\u6765\u7b49\u5f85\u6240\u6709\u534f\u7a0b\u90fd\u6267\u884c\u5b8c\u6210\u3002<\/p>\n<ol>\n<li>\u5b9e\u73b0SMTP\u670d\u52a1\u5668\u72b6\u6001\u7ef4\u62a4<\/li>\n<\/ol>\n<p>\u5f53SMTP\u670d\u52a1\u5668\u6536\u5230\u5927\u91cf\u8fde\u63a5\u8bf7\u6c42\u65f6\uff0c\u5b83\u53ef\u80fd\u4f1a\u5728\u77ed\u65f6\u95f4\u5185\u62d2\u7edd\u5927\u90e8\u5206\u8bf7\u6c42\uff0c\u8fd9\u65f6\u6211\u4eec\u9700\u8981\u5728\u534f\u7a0b\u5904\u7406\u4e2d\u5b8c\u6210SMTP\u670d\u52a1\u5668\u72b6\u6001\u7684\u7ef4\u62a4\u3002\u4f8b\u5982\uff0cSMTP\u670d\u52a1\u5668\u62d2\u7edd\u8bf7\u6c42\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5ef6\u65f6\u4e00\u6bb5\u65f6\u95f4\u518d\u91cd\u8bd5\u53d1\u9001\u90ae\u4ef6\u3002\u8fd9\u65f6\u6211\u4eec\u9700\u8981\u4f7f\u7528SwooleCoroutinesleep()\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u5b9e\u73b0SMTP\u670d\u52a1\u5668\u72b6\u6001\u7684\u7ef4\u62a4\uff1a<\/p>\n<pre>$max_retry_count = 5;\nfor ($i = 0; $i send();\n           if (!$result) {\n               echo \"Mailer Error: {$mail-&amp;gt;ErrorInfo}\n\";\n               if ($retry_count &lt;p&gt;\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f1a\u91cd\u8bd5\u53d1\u9001\u90ae\u4ef6\uff0c\u5e76\u5728\u6bcf\u6b21\u5931\u8d25\u65f6\u4f11\u7720\u4e00\u6bb5\u65f6\u95f4\u3002\u6bcf\u6b21\u4f11\u7720\u65f6\u95f4\u4f1a\u968f\u7740\u5931\u8d25\u6b21\u6570\u7684\u589e\u52a0\u800c\u589e\u5927\u3002&lt;\/p&gt;&lt;p&gt;\u603b\u7ed3&lt;\/p&gt;&lt;p&gt;Swoole\u7684\u534f\u7a0b\u7279\u6027\u4e3a\u9ad8\u5e76\u53d1\u90ae\u4ef6\u5904\u7406\u63d0\u4f9b\u4e86\u66f4\u65b9\u4fbf\u3001\u5feb\u901f\u3001\u9ad8\u6548\u7684\u4e00\u79cd\u65b9\u5f0f\u3002\u5728\u5b9e\u8df5\u4e2d\uff0c\u53ea\u9700\u7b80\u5355\u7684\u51e0\u884c\u4ee3\u7801\u5373\u53ef\u901a\u8fc7\u4f7f\u7528Swoole\u7684\u534f\u7a0b\u7279\u6027\uff0c\u5b9e\u73b0\u9ad8\u5e76\u53d1\u90ae\u4ef6\u5904\u7406\u3002\u5982\u679c\u4f60\u60f3\u5f00\u53d1\u5176\u4ed6\u9ad8\u5e76\u53d1\u5e94\u7528\uff0c\u4e5f\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Swoole\u7684\u534f\u7a0b\u7279\u6027\uff0c\u5c1d\u8bd5\u5c06\u5176\u96c6\u6210\u8fdb\u4f60\u7684\u9879\u76ee\u4e2d\uff0c\u4ee5\u63d0\u9ad8\u5e94\u7528\u6027\u80fd\u3002&lt;\/p&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSwoole\u5b9e\u8df5\u7ecf\u9a8c\uff1a\u4f7f\u7528\u534f\u7a0b\u8fdb\u884c\u9ad8\u5e76\u53d1\u90ae\u4ef6\u5904\u7406\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u6280\u672f\u7684\u98de\u901f\u53d1\u5c55\u548c\u5e94\u7528\u573a\u666f\u7684\u4e0d\u65ad\u62d3\u5c55\uff0c\u90ae\u4ef6\u670d\u52a1\u5df2\u7ecf\u6210\u4e3a\u4f01\u4e1a\/\u4e2a\u4eba\u65e5\u5e38\u5de5\u4f5c\u6240\u5fc5\u4e0d\u53ef\u5c11\u7684\u4e00\u90e8\u5206\u3002\u7136\u800c\uff0c\u5728\u5927\u89c4\u6a21\u7684\u90ae\u4ef6\u53d1\u9001\u4e2d\uff0c\u9ad8\u5e76\u53d1\u5f80\u5f80\u4f1a\u6210\u4e3a\u74f6\u9888\uff0c\u4f8b\u5982\u9891\u7e41\u7684\u8fde\u63a5\u6216\u91cd\u8fdesmtp\u670d\u52a1\u5668\u3001\u6d88\u606f\u961f\u5217\u6d88\u8d39\u7b49\u64cd\u4f5c\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u90fd\u9700\u8981\u6d88\u8017\u8f83\u591a\u7684\u65f6\u95f4\u548c\u7cfb\u7edf\u8d44\u6e90\uff0c\u5f71\u54cd\u6574\u4e2a\u90ae\u4ef6\u53d1\u9001\u8fc7\u7a0b\u7684\u6548\u7387\u3002\u90a3\u4e48\uff0c\u5982\u4f55\u7528\u6700\u5c0f\u7684\u8d44\u6e90\u5b9e\u73b0\u90ae\u4ef6\u7684\u9ad8\u6548\u5904\u7406\u5462\uff1f \u5b9e\u8df5\u8868\u660e\uff0c\u5728Swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u8fdb\u884c\u9ad8\u5e76\u53d1\u90ae\u4ef6\u5904\u7406\u662f\u4e00\u79cd\u975e\u5e38\u53ef\u884c\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u672c\u7bc7\u6587\u7ae0\u5c06\u5c31\u5982\u4f55\u4f7f\u7528Swoole\u7684\u534f\u7a0b\u7279\u6027\uff0c\u5b9e\u73b0\u9ad8\u5e76\u53d1\u90ae\u4ef6\u5904\u7406\uff0c\u505a\u4e00\u4e9b\u5f00\u53d1\u5b9e\u8df5\u4ecb\u7ecd\u3002 \u4e00\u3001\u534f\u7a0b\u4ecb\u7ecd \u534f\u7a0b\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u5373\u53ef\u4ee5\u770b\u4f5c\u662f\u8fdb\u7a0b\u548c\u7ebf\u7a0b\u4e4b\u95f4\u6298\u4e2d\u7684\u4e00\u79cd\u65b9\u6848\u3002\u534f\u7a0b\u6709\u4ee5\u4e0b\u51e0\u4e2a\u7279\u70b9\uff1a \u4f4e\u6210\u672c\u7684\u4e0a\u4e0b\u6587\u5207\u6362 \u65e0\u9700\u9501\u673a\u5236 \u8f7b\u91cf\u7ea7\u5360\u7528\u8d44\u6e90 \u591a\u4efb\u52a1\u534f\u4f5c \u5728Swoole\u7684\u534f\u7a0b\u7279\u6027\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51fd\u6570\u8fdb\u884c\u534f\u7a0b\u64cd\u4f5c\uff1a SwooleCoroutineun(callable $fn)\uff1a\u542f\u52a8\u4e00\u4e2aSwoole\u534f\u7a0b\u3002 SwooleCoroutinecreate(callable $fn, bool $coroutine_params)\uff1a\u521b\u5efa\u4e00\u4e2a\u534f\u7a0b\u3002 SwooleCoroutine::yield()\uff1a\u6682\u505c\u5f53\u524d\u534f\u7a0b\u7684\u6267\u884c\uff0c\u5e76\u4e0d\u4f1a\u91ca\u653eCPU\u8d44\u6e90\u3002 SwooleCoroutine::resume($coroutine_id)\uff1a\u6062\u590d\u6307\u5b9a\u7684\u534f\u7a0b\u3002 \u4e8c\u3001\u5b9e\u8df5\uff1a\u4f7f\u7528Swoole\u7684\u534f\u7a0b\u7279\u6027\u5b9e\u73b0\u9ad8\u5e76\u53d1\u90ae\u4ef6\u5904\u7406 \u521d\u59cb\u5316SMTP\u5ba2\u6237\u7aef \u4f7f\u7528Swoole\u7684\u534f\u7a0b\u7279\u6027\u6765\u5904\u7406\u90ae\u4ef6\u53d1\u9001\u662f\u76f8\u5f53\u7b80\u5355\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u6839\u636ePHPMailer\u5e93\u6765\u53d1\u9001\u90ae\u4ef6\u3002 \u9996\u5148\u9700\u8981\u521d\u59cb\u5316SMTP\u5ba2\u6237\u7aef\uff0c\u5e76\u8bbe\u7f6eSMTP\u670d\u52a1\u5668\u76f8\u5173\u7684\u53c2\u6570\uff1a try { $mail = new PHPMailer; $mail-&amp;gt;isSMTP(); $mail-&amp;gt;SMTPDebug = 0; $mail-&amp;gt;SMTPAuth = true; $mail-&amp;gt;SMTPSecure = &#8216;tls&#8217;; $mail-&amp;gt;Host = &#8220;smtp.example.com&#8221;; $mail-&amp;gt;Port = &#8220;465&#8221;; $mail-&amp;gt;CharSet = &#8220;utf-8&#8221;; $mail-&amp;gt;Username = &#8220;user@example.com&#8221;; $mail-&amp;gt;Password = &#8220;password&#8221;; $mail-&amp;gt;setFrom(&#8216;user@example.com&#8217;, &#8216;Mailer&#8217;); $mail-&amp;gt;addAddress(&#8216;recipient@example.com&#8217;, [&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-23528","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23528","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=23528"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23528\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}