Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在AWS中从SQS队列接收多条消息?_Python_Amazon Web Services_Amazon Sqs - Fatal编程技术网

Python 如何在AWS中从SQS队列接收多条消息?

Python 如何在AWS中从SQS队列接收多条消息?,python,amazon-web-services,amazon-sqs,Python,Amazon Web Services,Amazon Sqs,在AWS SQS中,我从队列中接收超过1条消息/行时遇到一点问题 这是我的密码: import boto3 import boto AWS_ACCESS_KEY = '*****' AWS_SECRET_ACCESS_KEY = '******' sqs = boto3.resource('sqs', aws_access_key_id=AWS_ACCESS_KEY, aws_secret_access_key=AWS_SECRET_ACCESS_KEY, region_na

AWS SQS
中,我从队列中接收超过1条消息/行时遇到一点问题

这是我的密码:

import boto3
import boto

AWS_ACCESS_KEY = '*****'
AWS_SECRET_ACCESS_KEY = '******'

sqs = boto3.resource('sqs', aws_access_key_id=AWS_ACCESS_KEY,
    aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
    region_name='us-east-2'
)

queue_name = 'Messages.fifo'
queue = sqs.get_queue_by_name(QueueName=queue_name)
messages = queue.receive_messages()


for message in messages:
    print('Body: {0}'.format(message.body))
如果您能提供帮助,我们将不胜感激。 谢谢

receive_messages()函数采用默认为1条消息的
MaxNumberOfMessages
参数。因此,您需要请求更多的消息

然而,缔约国:

MaxNumberOfMessages(整数)——要返回的最大消息数。Amazon SQS不会返回超过此值的消息(但是,返回的消息可能会更少)。有效值为1到10。默认值为1

因此,您可能无法在每次通话中收到完整数量的消息


例如:

导入boto3
sqs=boto3.resource('sqs')
queue=sqs。通过名称获取队列(QueueName='my-queue-name')
消息=队列。接收消息(MaxNumberOfMessages=10)
对于消息中的消息:
打印(message.body)
message.delete()
函数的
接收消息()
采用默认为1条消息的
MaxNumberOfMessages
参数。因此,您需要请求更多的消息

然而,缔约国:

MaxNumberOfMessages(整数)——要返回的最大消息数。Amazon SQS不会返回超过此值的消息(但是,返回的消息可能会更少)。有效值为1到10。默认值为1

因此,您可能无法在每次通话中收到完整数量的消息


例如:

导入boto3
sqs=boto3.resource('sqs')
queue=sqs。通过名称获取队列(QueueName='my-queue-name')
消息=队列。接收消息(MaxNumberOfMessages=10)
对于消息中的消息:
打印(message.body)
message.delete()
以下是一些代码(来自上一个较大的示例),显示了如何接收多条消息:

import logging

import boto3
from botocore.exceptions import ClientError

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

def receive_messages(queue, max_number, wait_time):
    """
    Receive a batch of messages in a single request from an SQS queue.
    :param queue: The queue from which to receive messages.
    :param max_number: The maximum number of messages to receive. The actual number
                       of messages received may be less.
    :param wait_time: The maximum time to wait (in seconds) before returning. When
                      this number is greater than zero, long polling is used. This
                      can result in reduced costs and fewer false empty responses.
    :return: The list of Message objects received. These each contain the body
             of the message and metadata and custom attributes.
    """
    try:
        messages = queue.receive_messages(
            MessageAttributeNames=['All'],
            MaxNumberOfMessages=max_number,
            WaitTimeSeconds=wait_time
        )
        for msg in messages:
            logger.info("Received message: %s: %s", msg.message_id, msg.body)
    except ClientError as error:
        logger.exception("Couldn't receive messages from queue: %s", queue)
        raise error
    else:
        return messages

下面是一些代码(来自上一个较大的示例),显示了如何接收多条消息:

import logging

import boto3
from botocore.exceptions import ClientError

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

def receive_messages(queue, max_number, wait_time):
    """
    Receive a batch of messages in a single request from an SQS queue.
    :param queue: The queue from which to receive messages.
    :param max_number: The maximum number of messages to receive. The actual number
                       of messages received may be less.
    :param wait_time: The maximum time to wait (in seconds) before returning. When
                      this number is greater than zero, long polling is used. This
                      can result in reduced costs and fewer false empty responses.
    :return: The list of Message objects received. These each contain the body
             of the message and metadata and custom attributes.
    """
    try:
        messages = queue.receive_messages(
            MessageAttributeNames=['All'],
            MaxNumberOfMessages=max_number,
            WaitTimeSeconds=wait_time
        )
        for msg in messages:
            logger.info("Received message: %s: %s", msg.message_id, msg.body)
    except ClientError as error:
        logger.exception("Couldn't receive messages from queue: %s", queue)
        raise error
    else:
        return messages


不要在STACKOVERFLOW上发布凭据!请立即删除该用户并检查您的帐户是否已被泄露。好的,即使我只是在玩这个系统,我也会记住这一点:PIt不在乎您是否“只是在玩”。有人可以拿着这些证书,在你的帐户上创建很多服务器,你会得到一大笔AWS账单。好的,谢谢你的回答!我现在要更改:)不要在STACKOVERFLOW上发布凭据!请立即删除该用户并检查您的帐户是否已被泄露。好的,即使我只是在玩这个系统,我也会记住这一点:PIt不在乎您是否“只是在玩”。有人可以拿着这些证书,在你的帐户上创建很多服务器,你会得到一大笔AWS账单。好的,谢谢你的回答!我现在将更改此选项:)