Php 该承诺被拒绝,原因是:调用等待回调无法解析该承诺

Php 该承诺被拒绝,原因是:调用等待回调无法解析该承诺,php,aws-sdk,guzzle,Php,Aws Sdk,Guzzle,我正试图通过Amazon SES和他们的SDK发送大量电子邮件。我可以看出他们在用GuzzleHttp来做承诺 正在发送电子邮件,过了一会儿(有时在7封电子邮件之后,有时在10000封电子邮件之后),我从$promise->wait()抛出一个异常出现以下错误: PHP致命错误:未捕获的GuzzleHttp\Promise\RejectionException: 承诺被拒绝,原因是:调用等待回调没有成功 解决…中的承诺 相关堆栈跟踪: #0 /path/to/aws/GuzzleHttp/Pro

我正试图通过Amazon SES和他们的SDK发送大量电子邮件。我可以看出他们在用
GuzzleHttp
来做承诺

正在发送电子邮件,过了一会儿(有时在7封电子邮件之后,有时在10000封电子邮件之后),我从
$promise->wait()抛出一个异常出现以下错误:

PHP致命错误:未捕获的GuzzleHttp\Promise\RejectionException: 承诺被拒绝,原因是:调用等待回调没有成功 解决…中的承诺

相关堆栈跟踪:

#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'
    });
}
知道为什么会发生这种情况吗?

方法的意思是等待请求完成,即使其中一些请求失败。在代码中,我相信您已经在
CommandPool
.

因此,您可以尝试将并发性从2增加到4,这意味着,与之前的2不同,现在4个计算同时进行,这在我们同时处理大量请求时很好。

我在下面给出了答案,请试着告诉我我的解决方案是否对您有效,我也很感兴趣。似乎在这条线路上的某个地方出现了拒绝,导致
wait()
“解体”,并且没有得到正确处理。如果替换
$promise->wait()$promise->then(…
结算($promise)->then(…
CommandPool
是PHP的AWS SDK的一部分。可在
sendMassEmails('Subject', 'Message', ['email1@example.com', 'email2@example.com', ...]);