SQS 消息未被删除

SQS Messages not being removed

我正在使用 python,当我从 SQS 读取内容时,它们没有被正确删除,几秒钟后又被读取。

我使用此代码片段 post 一份工作

#!/usr/bin/python 

#simple code snippet that posts a message to SQS
import boto
from boto.sqs.message import Message
conn = boto.connect_sqs()
q = conn.create_queue('myqueue')
newJob.set_body("Message")
q.write(newJob)

这是消化消息的代码

#!/usr/bin/python 

#simple code snippet that reads a message from SQS
import boto
from boto.sqs.message import Message
conn = boto.connect_sqs()
q = conn.get_queue('myqueue')

while True:
    try:
        print q.read(10).get_body()
    except:
        pass

在 运行 两个脚本之后,第二个脚本将每 15 秒左右打印一次 'Message'。

我真的很困惑为什么它没有被删除,为什么它只在 15 秒后才回来。 (它也没有出现在 AWS 控制台中,我从那里发送的消息也从未被处理过)

免责声明:我以前从未使用过boto.sqs,这个答案只是基于阅读its doc

根据下面的描述,它看起来像一条消息在阅读后 1) 不会自动删除,并且 2) 在特定时间段内变得不可见。

There is an optional parameter to create_queue called visibility_timeout. This basically controls how long a message will remain invisible to other queue readers once it has been read (see SQS documentation for more detailed explanation). If this is not explicitly specified the queue will be created with whatever default value SQS provides (currently 30 seconds).

更新Amazon SQS Doc证实了我的上述理解。

...Therefore, Amazon SQS does not delete the message, and instead, your consuming component must delete the message from the queue after receiving and processing it.

...Therefore, Amazon SQS blocks them with a visibility timeout, which is a period of time during which Amazon SQS prevents other consuming components from receiving and processing that message...

所以我想您需要在成功从队列中读取消息后显式 delete 消息(如果您不希望其他客户端使用它)。