处理SQS队列与博托 [英] Processing SQS queues with boto

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

问题描述

我在使用的EC2实例的博托库,它是一个自动缩放组的一部分,Python脚本。该脚本从SQS队列处理消息:

I have a python script using the boto library on ec2 instance which is part of an autoscaling group. The script processes messages from a SQS queue:

import boto
from boto.sqs.message import Message

conn = boto.connect_sqs()
q = conn.create_queue('queue-name')

while (qin.count() > 0):
    m = q.get_messages()
    #do something with the message

是否使用whil​​e语句有意义吗?是否实时计数()更新内容:

Does using the while statement make sense? Does count() update in real time as:

  1. 在其他情况下起飞队列中的消息(还是我要加倍)
  2. 在新消息被添加到队列中(或我会想念他们吗?)

如何让我这个剧本经常听取新增加的队列中,即使在队列为空?

在这个问题<一href="http://stackoverflow.com/questions/8203531/processing-items-in-sqs-queue-with-a-php-script">Processing在SQS队列PHP脚本该项目提到,SQS红宝石客户端库有一个方法民意调查,它不断轮询队列,并在队列中接收一个消息将它传递给块。有没有在Python等效?

In this question Processing items in SQS queue with a php script it was mentioned that 'sqs ruby client library has a method "poll" which continuously polls the queue and on receiving a message in the queue passes it on to a block'. Is there an equivalent in Python?

也有人提出,SNS可以用来通知的消息队列状态的脚本,但我看不出你如何能与SNS配置响应系统作为度量标准的报警都没有足够细度的。

It has also been suggested that the SNS could be used to notify the scripts of the message queue status but I do not see how you could configure a responsive system with SNS as the metric alarms are not fine grained enough.

推荐答案

您不应该依赖于一个队列中的计数,因为它是只是为了提供近似计数,并且不保证是准确的。

You shouldn't rely on the count for a queue as it's only meant to provide an approximate count and is not guaranteed to be accurate.

如果你想只保留查询永远,只是这样做:

If you want to just keep polling forever, just do this:

while 1:
    messages = q.get_messages()
    # do something with messages
    time.sleep(N)

我添加了调用time.sleep介绍,在循环延迟。 N的值应至少一秒钟,可能是相当多的,这取决于您希望如何迅速的新邮件出现在您的队列。如果你不把某种延迟的循环,你可能会开始得到节流的服务。

I have added the call to time.sleep to introduce a delay in the loop. The value of N should be at least one second and could be considerably more, depending on how quickly you expect new messages to appear in your queue. If you don't put some sort of delay in the loop you will probably start getting throttled by the service.

要避免消息得到多​​次读取,你应该尝试调整队列的可见性超时是一个比它需要你来处理邮件,然后确保你删除了消息的时间长时,处理完成

To avoid a message getting read multiple times, you should try to adjust the visibility timeout of the queue to be a value greater than the time it takes you to process a message and then make sure you delete the message when processing has completed.

这篇关于处理SQS队列与博托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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