Jordan Brown Jordan Brown - 2 years ago 127
Python Question

SQS Messages not being removed

I'm using python and when I read things from SQS they don't get removed properly and they get read again a few seconds later.

I use this code snippet to post a job


#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')

Here is the code that digests messages


#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:

After running both scripts the second one will print out 'Message' every 15 seconds or so.

I'm really confused as to why it's not being removed AND why it comes back only after 15 seconds.
(It also doesn't show up in the AWS console, and messages I send from there are never processed)

Answer Source

Disclaimer: I never used boto.sqs before and this answer is just based on reading its doc.

According to below description, it looks like a message after being read 1) is NOT automatically deleted , and 2) becomes invisible for a specific time period.

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).

update: Amazon SQS Doc confirms my above understanding.

...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...

So I guess you need to explicitly delete the message after you have successfully read it from the queue (if you don't want another client consume it).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download