Home > Back-end >  SQS does not delete the message and runs infinite loop
SQS does not delete the message and runs infinite loop

Time:07-11

I am new to AWS SQS. I am trying to implement a simple server to node SQS service where multiple nodes send some information (Some identification about itself) to the server, which the server needs to put in a list and delete the messages iteratively. I have the following code:

sqs_client = boto3.client("sqs",aws_access_key_id=akey,aws_secret_access_key=skey,region_name=region)

#Queue URL
queue=r'https://ap-south-1.queue.amazonaws.com/blahblah/SQS_client_list'

response = sqs_client.receive_message(
QueueUrl=queue,
AttributeNames=['SentTimestamp'],
MaxNumberOfMessages=10,
MessageAttributeNames=['All'],
VisibilityTimeout=2,
WaitTimeSeconds=2
)
message=response.get("Messages")
#Information should go into this list
node_list=[]

#Function to delete messages
#I need to delete the message after passing into the list "node_list"
def get_node_list():
    message_body = message[0]["Body"]
    node_list.append(message_body)
    sqs_client.delete_message(QueueUrl=queue, ReceiptHandle=response['Messages'][0]["ReceiptHandle"]) 
    time.sleep(3)
    return node_list

# Loop to delete all messages
while(True):
    if len(response.get('Messages', [])) !=0:
        time.sleep(3)
        get_node_list()
    if len(response.get('Messages', [])) ==0:
        break
print(node_list)

The issue is that when multiple nodes push their information into the same queue, this code runs an infinite loop and node_list gets append infinite number of times whereas the while loop does not delete the SQS message. What can possibly be wrong in the code?

CodePudding user response:

Your code should look like:

while(True):

  # Get messages
  response = sqs_client.receive_message(WaitTimeSeconds=20,...)

  # Loop through messages
  for message in response['Messages']:

    # process message here

    # Delete the message
    sqs_client.delete_message(QueueUrl=queue, ReceiptHandle=message['ReceiptHandle']) 

I am assuming that you want it to be able to continually process new messages, hence the outer loop that loops forever.

When calling receive_messages(), I recommend that you set WaitTimeSeconds=20. This means that if there are no messages in the queue, it will wait up to 20 seconds before returning. However, as soon as a message appears, it will return without waiting 20 seconds. This reduces the number of API calls to AWS.

I notice that you have specified MaxNumberOfMessages=10, which means that multiple messages might be received. Therefore the code needs to loop through each of the messages provided.

After processing the message, it can call delete_message(). Alternatively, you could add all message handles to a list and call delete_message_batch() after the inner loop.

  • Related