如何使用Python中的boto库获取Amazon SQS中的消息接收计数? [英] How to get messages receive count in Amazon SQS using boto library in Python?

查看:66
本文介绍了如何使用Python中的boto库获取Amazon SQS中的消息接收计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Python中使用boto库获取Amazon SQS消息.在特殊情况下,我不会从队列中删除消息,以便进行更多更改以恢复临时故障.但是我不想一直不断收到失败的消息.我想做的是在接收3次以上后删除消息,或者在接收计数大于3次时不接收消息.

I am using boto library in Python to get Amazon SQS messages. In exceptional cases I don't delete messages from queue in order to give a couple of more changes to recover temporary failures. But I don't want to keep receiving failed messages constantly. What I would like to do is either delete messages after receiving more than 3 times or not get message if receive count is more than 3.

最优雅的方法是什么?

推荐答案

至少有两种方法可以做到这一点.

There are at least a couple of ways of doing this.

当您在boto中读取消息时,您会收到一个Message对象或其某些子类.消息对象具有一个属性"字段,该字段是一个包含SQS已知的所有消息属性的字典.SQS跟踪的事情之一是大约已读取消息的次数.因此,您可以使用此值来确定是否应删除邮件,但是您必须对值的近似"性质感到满意.

When you read a message in boto, you receive a Message object or some subclass thereof. The Message object has an "attributes" field that is a dict containing all message attributes known by SQS. One of the things SQS tracks is the approximate # of times the message has been read. So, you could use this value to determine whether the message should be deleted or not but you would have to be comfortable with the "approximate" nature of the value.

或者,您可以在某种数据库中记录消息ID,并在每次阅读消息时在数据库中增加一个count字段.如果始终在单个进程中读取消息,则可以通过简单的Python字典来完成;如果需要记录跨进程的读数,则可以使用SimpleDB之类的方法来完成.

Alternatively, you could record message ID's in some sort of database and increment a count field in the database each time you read the message. This could be done in a simple Python dict if the messages are always being read within a single process or it could be done in something like SimpleDB if you need to record readings across processes.

希望有帮助.

下面是一些示例代码:

>>> import boto.sqs
>>> c = boto.sqs.connect_to_region()
>>> q = c.lookup('myqueue')
>>> messages = c.receive_message(q, num_messages=1, attributes='All')
>>> messages[0].attributes
{u'ApproximateFirstReceiveTimestamp': u'1365474374620',
 u'ApproximateReceiveCount': u'2',
 u'SenderId': u'419278470775',
 u'SentTimestamp': u'1365474360357'}
>>>

这篇关于如何使用Python中的boto库获取Amazon SQS中的消息接收计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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