异步发布到Azure队列 [英] Asynchronously posting to Azure Queues

查看:86
本文介绍了异步发布到Azure队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试像这样异步在Azure队列中排队消息:

I try to enqueue messages in Azure Queues asynchronously like this:

private async Task EnqueueItemsAsync(IEnumerable<string> messages) {
            var tasks = messages.Select(msg => _queue.AddMessageAsync(new CloudQueueMessage(msg),
                null, null, null, null));

            await Task.WhenAll(tasks);
        }

如果我说对了,它会说开始将一个项目排在另一个项目后面,等待它们发布,为每个任务保留参考,然后等待所有任务发布。

If I get it right this says "start enqueuing one item after the other without waiting them to get posted, keep a reference for each task and then wait until all get posted".

此代码在大多数情况下可以正常工作,但对于项(5000),它开始入队,然后抛出超时异常(入队〜3500项后)。

This code works fine in most cases, but for a large number of items (5000), it starts enqueuing and then throws a timeout exception (after having enqueued ~3500 items).

我通过等待解决了它每个任务都要先完成,然后再继续下一个任务

I solved it by waiting each one to finish before continuing with the next one

private async Task EnqueueItemsAsync(IEnumerable<string> messages) {
            foreach (var message in messages) {
                await _queue.AddMessageAsync(new CloudQueueMessage(message), null, null, null, null);
            }
        }

有人可以解释为什么会这样吗?

Can anyone explain why this happened?

例外:


System.AggregateException ,其中包含许多此类异常:
Microsoft.WindowsAzure.Storage.Core.Util.AsyncExtensions。<> c__DisplayClass4。< CreateCallbackVoid> b__3(IAsyncResult
ar)
请求信息RequestID:RequestDate:StatusMessage:< ---
--->(内部异常#1)Microsoft.WindowsAzure.Storage.StorageException:客户端无法
完成在指定的超时时间内运行。 --->
System.TimeoutException:客户端无法在指定的超时时间内完成操作
。 ---内部异常堆栈跟踪的结尾---
Microsoft.WindowsAzure.Storage.Core.Executor.Executor.EndExecuteAsync [T](IAsyncResult
result)`。

System.AggregateException which wraps many such exceptions: Microsoft.WindowsAzure.Storage.Core.Util.AsyncExtensions.<>c__DisplayClass4.<CreateCallbackVoid>b__3(IAsyncResult ar) Request Information RequestID: RequestDate: StatusMessage: <--- ---> (Inner Exception #1) Microsoft.WindowsAzure.Storage.StorageException: The client could not finish the operation within specified timeout. ---> System.TimeoutException: The client could not finish the operation within specified timeout. --- End of inner exception stack trace --- Microsoft.WindowsAzure.Storage.Core.Executor.Executor.EndExecuteAsync[T](IAsyncResult result)`.


推荐答案

Azure中的队列被设计为具有每秒2000条消息的吞吐量。

A queue in Azure is designed have a throughput of 2000 messages per second.

请参阅: Azure存储可伸缩性和性能目标


当应用程序达到分区可以为您的工作负荷处理的限制时,Azure存储将开始返回错误代码503(服务器繁忙)或错误代码500(操作超时)响应。发生这种情况时,应用程序应使用指数退避策略进行重试。指数补偿可以减少分区上的负载,并减轻到达该分区的流量峰值。

When your application reaches the limit of what a partition can handle for your workload, Azure Storage will begin to return error code 503 (Server Busy) or error code 500 (Operation Timeout) responses. When this occurs, the application should use an exponential backoff policy for retries. The exponential backoff allows the load on the partition to decrease, and to ease out spikes in traffic to that partition.

这篇关于异步发布到Azure队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆