gpt4 book ai didi

amazon-web-services - 如何使用 boto 长时间轮询亚马逊 sqs 服务?

转载 作者:行者123 更新时间:2023-12-03 21:23:33 29 4
gpt4 key购买 nike

我在亚马逊上有一个 sqs 队列,有几个消费者在轮询它。

最近我发现numberofEmptyReceives是 1000 万,这意味着我正在根据这些请求收费。

以下是我消费消息的方式

while True:
for message in queue.receive_messages(AttributeNames=['All'], MaxNumberOfMessages=10):

我知道有 WaitTimeSeconds选项,但文档似乎表明它不进行长轮询

The duration (in seconds) for which the call waits for a message to arrive in the queue before returning. If a message is available, the call returns sooner than WaitTimeSeconds . If no messages are available and the wait time expires, the call returns successfully with an empty list of messages.



具体来说

If a message is available, the call returns sooner than WaitTimeSeconds .



从上面的句子似乎暗示 boto3仍然打电话 sqs检查是否有消息。

使用 boto3 进行长时间轮询以避免收取请求费用的正确方法是什么?

是不是像设置一样简单 thread.sleep ?

我也无法在 github 上找到源代码

最佳答案

只需发送一个 WaitTimeSeconds receive_message 中的参数(最多 20 秒)称呼。从 AWS 文档:

# Long poll for message on provided SQS queue
response = sqs.receive_message(
QueueUrl=queue_url,
AttributeNames=[
'SentTimestamp'
],
MaxNumberOfMessages=1,
MessageAttributeNames=[
'All'
],
WaitTimeSeconds=20
)

如果队列中没有消息,则调用将等待至 WaitTimeSeconds显示一条消息。如果在时间到期之前出现消息,调用将立即返回该消息。

Enabling Long Polling in Amazon SQS更多细节。

您仍需为长轮询请求付费。成本节约在于执行一个可计费的 SQS 操作并等待长轮询超时,否则您可能不得不在同一时间段内执行两个或三个或更多的计费操作。

关于amazon-web-services - 如何使用 boto 长时间轮询亚马逊 sqs 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50558084/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com