我作为工人的角色需要睡眠时间吗?

发布于 2024-12-28 07:19:42 字数 382 浏览 5 评论 0原文

我有一个监视队列的辅助角色。尽管我希望它被猛击,但大多数时候队列都是空的。每隔几分钟我就会在队列中收到一个新项目。

我有:

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

漫漫岁月 2025-01-04 07:19:42

帮助最小化交易费用(我认为这是您最关心的问题)的一个好方法是采用退避轮询方法。意思是,每次找不到队列消息时,慢慢增加睡眠时间。这样做直到睡眠时间达到某个最大值。一旦找到消息,就开始更快地轮询消息。

另一种帮助方法是一次从队列中获取多条消息(而不是一次获取一条)。这也将有助于减少交易费用。

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.

尘曦 2025-01-04 07:19:42

我还建议您检查消息上的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文