问题描述
我有一个Lambda函数,该lambda函数应该将消息发送到SQS中的多个队列,然后退出,
I have a Lambda function and that lambda function is supposed to send messages to multiple queues in SQS and then exit,
如果我添加一个等待,则消息将发送到所有队列,
If I add await, then messages are sent to all queues,
var sqsClient = ServerlessHelper.GetAmazonSqsClient();
foreach (var item in items)
{
await sqsClient.SendMessageAsync(item.QueueUrl, item.Message);
}
但是,如果我从代码中删除了await,则不会有任何消息发送到队列.我想并行发送消息.但是由于等待,我有功能无法并行发送消息.我正在尝试做这样的事情,
But if I remove await from the code, then none of messages are sent to queues. I want to send messages in parallel. But due to await I have function cannot send messages in parallel. I am trying to do something like this,
var sqsClient = ServerlessHelper.GetAmazonSqsClient();
foreach (var item in items)
{
sqsClient.SendMessageAsync(item.QueueUrl, item.Message);
}
// wait until are messages are sent to queues.
有可能这样做吗?
推荐答案
我正在猜测 sqsClient.SendMessageAsync(item.QueueUrl,item.Message);
返回一个Task,因为它是一个异步函数?
I'm guessing sqsClient.SendMessageAsync(item.QueueUrl, item.Message);
returns a Task since it is an async function?
您需要保存任务实例,然后使用 Task.WhenAll
,如该示例
You need to save the task instances then use Task.WhenAll
as in this example.
这篇关于没有等待的QqsClient.SendMessageAsync不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!