我正在尝试通过Amazon SES及其SDK发送大量电子邮件。我可以看到他们使用GuzzleHttp
来保证诺言。
正在发送电子邮件,过了一会儿(有时在7封电子邮件之后,有时在10,000封之后),我从$promise->wait();
抛出异常,并出现以下错误:
PHP致命错误:未捕获的GuzzleHttp \ Promise \ RejectionException: Promise被拒绝的原因:调用wait回调没有 兑现...中的承诺
相关堆栈跟踪:
#0 /path/to/aws/GuzzleHttp/Promise/Promise.php(75): GuzzleHttp\Promise\exception_for()
#1 /path/to/myfile.php(725): GuzzleHttp\Promise\Promise->wait()
我的代码:
function sendMassEmails($subject, $message, $recipients)
{
$SesClient = new SesClient([
'profile' => 'default',
'version' => '2010-12-01',
'region' => 'eu-central-1'
]);
$sender_email = 'MyCompany <noreply@mycompany.com>';
$char_set = 'UTF-8';
$commands = [];
foreach ($recipients as $recipient) {
$commands[] = $SesClient->getCommand('SendEmail', [
'Destination' => [
'ToAddresses' => [$recipient],
],
'ReplyToAddresses' => [$sender_email],
'Source' => $sender_email,
'Message' => [
'Body' => [
'Html' => [
'Charset' => $char_set,
'Data' => $message,
],
'Text' => [
'Charset' => $char_set,
'Data' => convert_html_to_text($message),
],
],
'Subject' => [
'Charset' => $char_set,
'Data' => $subject,
],
],
'ConfigurationSetName' => 'MyConfigurationSetName',
]);
}
$pool = new CommandPool($SesClient, $commands, [
'concurrency' => 2,
'before' => function (CommandInterface $cmd, $iteratorId) {
$a = $cmd->toArray();
echo sprintf('About to send %d: %s' . PHP_EOL, $iteratorId, $a['Destination']['ToAddresses'][0]);
},
'fulfilled' => function (ResultInterface $result, $iteratorId) use ($commands) {
// log $commands[$iteratorId]['Destination']['ToAddresses'][0]
},
'rejected' => function (AwsException $reason, $iteratorId) use ($commands) {
$logData = sprintf(
'%s | Reason: %s' . PHP_EOL,
$commands[$iteratorId]['Destination']['ToAddresses'][0],
$reason
);
// log $logData
},
]);
$promise = $pool->promise();
$promise->wait(); // <-- this line throws the exception
$promise->then(function () {
// Log 'done'
});
}
我这样调用函数:
sendMassEmails('Subject', 'Message', ['email1@example.com', 'email2@example.com', ...]);
知道为什么会这样吗?
答案 0 :(得分:0)
wait()
方法意味着等待请求完成,即使其中一些失败。我相信在代码中您已经在EachPromise Class
中扩展了CommandPool
。
因此,您可以尝试将并发性从2增加到4,这意味着与之前的2相比,现在可以同时进行4个计算,这在我们同时处理大量请求时是很好的。