我作为工人的角色需要睡眠时间吗?
我有一个监视队列的辅助角色。尽管我希望它被猛击,但大多数时候队列都是空的。每隔几分钟我就会在队列中收到一个新项目。
我有:
public override void Run()
{
while (true)
{
//Check the queue for new messages
//if there's a new message, do some stuff
}
}
我的问题是,如果队列中没有项目,我是否需要执行 Thread.Sleep(x) ?或者我可以一遍又一遍地检查它吗?如果我确实需要睡觉,我应该睡多长时间? (毫秒/秒?)
我主要关心的是 Azure 费用。
I have a worker role monitoring a queue. As much as I would like it to be slammed, most of the time that queue will be empty. I get a new item in the queue every couple of minutes.
I have:
public override void Run()
{
while (true)
{
//Check the queue for new messages
//if there's a new message, do some stuff
}
}
My question is, do I need to do a Thread.Sleep(x) if there isn't an item in the queue? Or can I just keep checking it over and over? If I do need to sleep, how long should I sleep for? (milliseconds/seconds?)
My main concern is Azure charges.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
帮助最小化交易费用(我认为这是您最关心的问题)的一个好方法是采用退避轮询方法。意思是,每次找不到队列消息时,慢慢增加睡眠时间。这样做直到睡眠时间达到某个最大值。一旦找到消息,就开始更快地轮询消息。
另一种帮助方法是一次从队列中获取多条消息(而不是一次获取一条)。这也将有助于减少交易费用。
A good approach to help minimize transaction charges (which is I think your primary concern) would be to go with a backoff polling approach. Meaning, slowly increase the sleep time each time there isn't a queue message to be found. Doing so until the sleep time reaches some maximum value. Once a message is found, then start to poll faster for messages.
Another way to help would be to grab multiple messages from the queue at once (instead of getting one at a time). That will help with the transaction charges as well.
看看:
http://windowsazurecat.com/2010/12/best-practices-for-maximizing-scalability-and-cost-efficientness-of-queue-based-messaging-solutions-on-windows-azure/
Take a look at:
http://windowsazurecat.com/2010/12/best-practices-for-maximizing-scalability-and-cost-effectiveness-of-queue-based-messaging-solutions-on-windows-azure/
我还建议您检查消息上的 DequeueCount,以避免对有毒消息进行不必要的处理/费用。
例如,记录和删除超过设定的出列计数的消息,而不是在消息循环中的任何睡眠周期上旋转。
I would also recommend that you check the DequeueCount on the message to avoid unnecessary processing / charges on poisoned messages.
For example logging and deleting messages that exceeed a set dequeue count rahter than spinning on whatever sleepperiod you hav ein your message loop.