python - SQS 消息未被删除

标签 python amazon-sqs

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

我使用此代码片段来发布职位

#!/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 秒左右打印出“消息”。

我真的很困惑为什么它没有被删除,为什么它只在 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成功从队列中读取消息后的消息(如果您不希望其他客户端使用它)。

关于python - SQS 消息未被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37369795/

相关文章:

python - 实用 Django 项目 - 第 71 和 80 页

python - 如何在 python 中做一个简单的 Pika SelectConnection 来发送消息?

java - 使用 AWS Java SDK 为基于角色的联合用户创建 SQS 而无需凭据

amazon-web-services - 无法在 S3 中配置 SQS 队列通知

amazon-web-services - AWS Worker Tier - 任务完成但进入死信队列 - 任务完成有时间限制吗?

amazon-web-services - aws cloudformation s3 向 SQS 发出的事件通知不起作用

python - 如何使用 Selenium 在非安全页面上绕过消息 -"your connection is not private"?

python - 关于Python中的杀死开关

python - 十进制转二进制的python算法

amazon-web-services - Amazon SQS - 使消息不可见 x 秒