

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# Amazon SQS 短轮询和长轮询
<a name="sqs-short-and-long-polling"></a>

Amazon SQS 提供短轮询和长轮询选项，用于接收来自队列的消息。选择使用哪种轮询方式时，您需要考虑应用程序对响应能力和成本效益的要求：
+ **短轮询**（默认）：[https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html) 请求查询服务器子集（基于加权随机分布）以查找可用消息并立即发送响应，即使未找到任何消息也是如此。
+ **长轮询**：[https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html) 查询所有服务器的消息，在至少有一条消息可用时发送响应，不超过指定的最大值。只有在轮询等待时间到期时，系统才会发送空响应。这一选项可以减少空响应的数量，并有可能降低成本。

以下部分解释短轮询和长轮询的详细信息。

## 通过短轮询来使用消息
<a name="sqs-short-polling"></a>

当通过短轮询从队列（FIFO 队列或标准队列）中使用消息时，Amazon SQS 会对服务器的一个子集进行采样（基于加权随机分布），并且仅返回来自这些服务器的消息。因此，特定 [https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html) 请求可能不会返回您的所有消息。但是，如果您的队列中的消息少于 1000 条，一个后续请求将返回您的消息。如果继续从您的队列中使用消息，则 Amazon SQS 会对其所有服务器进行采样，然后您将收到您的所有消息。

下图显示了系统组件之一发出接收请求后从标准队列返回的消息的短轮询行为。Amazon SQS 对其若干服务器（显示为灰色）进行采样并从这些服务器返回消息 A、C、D 和 B。系统不会为此请求返回消息 E，但将为后续请求返回该消息。

![\[使用短（标准）轮询进行消息采样\]](http://docs.aws.amazon.com/zh_cn/AWSSimpleQueueService/latest/SQSDeveloperGuide/images/ArchOverview_Receive.png)


## 通过长轮询来使用消息
<a name="sqs-long-polling"></a>

当 `[ReceiveMessage](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html)` API 操作的等待时间大于 0 时，*长轮询*生效。最长长轮询等待时间为 20 秒。长轮询通过减少空响应（当`ReceiveMessage`请求没有消息可用时）和虚假空响应（当消息可用但未包含在响应中时）的数量来帮助降低使用 Amazon SQS 的成本。有关使用 Amazon SQS 控制台为新队列或现有队列启用长轮询的信息，请参阅[使用 Amazon SQS 控制台配置队列参数](sqs-configure-queue-parameters.md)。有关最佳实践，请参阅[在 Amazon SQS 中设置长轮询](best-practices-setting-up-long-polling.md)。

长轮询具有以下优势：
+ 在发送响应之前，允许 Amazon SQS 等到队列中的消息可用为止，从而消除空响应。除非连接超时，否则对 `ReceiveMessage` 请求的响应将至少包含一条可用的消息，并且最多包含 `ReceiveMessage` 操作中指定的最大数量的消息。在极少数情况下，即使队列中仍有消息，您也可能会收到空响应，尤其是在您为 [https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html#SQS-ReceiveMessage-request-WaitTimeSeconds](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html#SQS-ReceiveMessage-request-WaitTimeSeconds) 参数指定的值较低的情况下。
+ 通过查询所有（而不是其子集）Amazon SQS 服务器来减少错误空响应。
+ 在消息可用时立即返回消息。

有关如何确认队列为空的信息，请参阅[确认 Amazon SQS 队列为空](confirm-queue-is-empty.md)。

## 长轮询和短轮询之间的区别
<a name="sqs-short-long-polling-differences"></a>

如果以下列两种方式之一将 `[ReceiveMessage](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html)` 请求的 [https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html#SQS-ReceiveMessage-request-WaitTimeSeconds](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html#SQS-ReceiveMessage-request-WaitTimeSeconds) 参数设置为 `0`，则会发生短轮询：
+ `ReceiveMessage` 调用将 `WaitTimeSeconds` 设置为 `0`。
+ `ReceiveMessage` 调用不会设置 `WaitTimeSeconds`，但队列属性 [https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SetQueueAttributes.html](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SetQueueAttributes.html) 将设置为 `0`。