For those that come stumbling through AWS and SES trying to solve this problem of parallel requests that are done on a persistent connection, AWS SDK 2 and up has support for this in php using command objects.
The SesClient and other clients can execute commands in parallel. This is the normal method for firing a single connection and email through SES:
$result = $client->sendEmail(array(
//email data
));
The client objects are very powerful and inherit many methods to perform and manipulate the requests such as getCommand()
and execute()
. It took hours and hours of digging before I found the simple solution! You just have to know the right thing to search for. Here is an example:
$commands = array();
$commands[] = $sesClient->getCommand('SendEmail', array(
//email data
));
$commands[] = $sesClient->getCommand('SendEmail', array(
//email data
));
// Execute an array of command objects to do them in parallel
$sesClient->execute($commands);
// Loop over the commands, which have now all been executed
foreach ($commands as $command) {
$result = $command->getResult();
// Do something with result
}
Error handling can be achieved by executing the following code:
use Guzzle\Service\Exception\CommandTransferException;
try {
$succeeded = $client->execute($commands);
} catch (CommandTransferException $e) {
$succeeded = $e->getSuccessfulCommands();
echo "Failed Commands:\n";
foreach ($e->getFailedCommands() as $failedCommand) {
echo $e->getExceptionForFailedCommand($failedCommand)->getMessage() . "\n";
}
}
Amazon documents these examples in their developer guide under the commands feature.