如何使用 boto 长时间轮询 amazon sqs 服务?
How to long poll amazon sqs service using boto?
我在亚马逊上有一个 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
上也找不到源代码
只需在您的 receive_message
调用中发送一个 WaitTimeSeconds
参数(最多 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
以等待消息出现。如果在时间到期之前出现消息,调用将立即 return 该消息。
有关详细信息,请参阅 Enabling Long Polling in Amazon SQS。
您仍然需要为长轮询请求付费。节省的成本在于执行一个可计费的 SQS 操作并等待长轮询超时,否则您可能不得不在同一时期内执行两个或三个或更多的可计费操作。
我在亚马逊上有一个 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
上也找不到源代码只需在您的 receive_message
调用中发送一个 WaitTimeSeconds
参数(最多 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
以等待消息出现。如果在时间到期之前出现消息,调用将立即 return 该消息。
有关详细信息,请参阅 Enabling Long Polling in Amazon SQS。
您仍然需要为长轮询请求付费。节省的成本在于执行一个可计费的 SQS 操作并等待长轮询超时,否则您可能不得不在同一时期内执行两个或三个或更多的可计费操作。