在boto3

时间:2017-02-10 17:29:14

标签: amazon-sqs boto3

我正在批量使用boto3到receive_message()。这将返回boto3 documentation中指定的dict项。

收到批量邮件后,我逐个处理它们,并希望在它们全部处理后批量删除它们。我不想在处理每个项目后逐个删除它们。

delete_message_batch()函数的语法与来自receive_message()函数的响应略有不同,因此我需要将来自receive_message()的响应dict重新格式化为合适的格式(仅使用Id和ReceiptHandle)。

有没有更简单的方法直接使用receive_message()的响应进行批量删除?

3 个答案:

答案 0 :(得分:4)

以下是我处理邮件的方式。我创建了一个包含id和收据句柄的列表。 id不是必需的,您可以插入范围(10)的输出。此外,没有错误检查,所以你需要照顾它。

response = SQS.receive_message(QueueUrl=queue_url,
                               AttributeNames=[],
                               MaxNumberOfMessages=10,
                               MessageAttributeNames=['All'])
delete_batch = []
for message in response.get('Messages') or []:
    # do stuff with each message
    delete_batch.append({'Id': message['MessageId'],
                         'ReceiptHandle': message['ReceiptHandle']})
if delete_batch:
    SQS.delete_message_batch(QueueUrl=queue_url, Entries=delete_batch)

答案 1 :(得分:0)

在Boto3中,您可以使用队列资源在一个呼叫中删除成批的消息。这是一些代码(来自GitHub中的一个较大示例),显示了如何:

import logging

import boto3
from botocore.exceptions import ClientError

logger = logging.getLogger(__name__)
sqs = boto3.resource('sqs')

def delete_messages(queue, messages):
    """
    Delete a batch of messages from a queue in a single request.
    :param queue: The queue from which to delete the messages.
    :param messages: The list of messages to delete.
    :return: The response from SQS that contains the list of successful and failed
             deletions.
    """
    try:
        entries = [{
            'Id': str(ind),
            'ReceiptHandle': msg.receipt_handle
        } for ind, msg in enumerate(messages)]
        response = queue.delete_messages(Entries=entries)
        if 'Successful' in response:
            for msg_meta in response['Successful']:
                logger.info("Deleted %s", messages[int(msg_meta['Id'])].receipt_handle)
        if 'Failed' in response:
            for msg_meta in response['Failed']:
                logger.warning(
                    "Could not delete %s",
                    messages[int(msg_meta['Id'])].receipt_handle
                )
    except ClientError:
        logger.exception("Couldn't delete messages from queue %s", queue)
    else:
        return response

queue = sqs.get_queue_by_name(QueueName=name)
messages = queue.receive_messages(
            MessageAttributeNames=['All'],
            MaxNumberOfMessages=max_number,
            WaitTimeSeconds=wait_time
        )
delete_messages(queue, messages)

答案 2 :(得分:0)

还有一个选项可以清除队列。不确定它是否适合您的用例,但这里是参考:

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Queue.purge

和代码示例:

import boto3

REGION_NAME = 'us-east-01'
QUEUE_NAME = 'my-queue01-example'

sqs = boto3.resource('sqs', REGION_NAME)
queue = sqs.get_queue_by_name(QueueName=QUEUE_NAME)
response = queue.purge()