Debian Codegraph @

docs/source/guide/sqs-example-long-polling.rst

raw
1 .. Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
3 This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0
4 International License (the "License"). You may not use this file except in compliance with the
5 License. A copy of the License is located at http://creativecommons.org/licenses/by-nc-sa/4.0/.
6
7 This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
8 either express or implied. See the License for the specific language governing permissions and
9 limitations under the License.
10
11 .. _aws-boto3-sqs-long-polling:
12
13 ###################################
14 Enabling long polling in Amazon SQS
15 ###################################
16
17 This Python example shows you how to enable long polling in Amazon SQS in one of these ways:
18
19 * For a newly created queue
20
21 * For an existing queue
22
23 * Upon receipt of a message
24
25 The scenario
26 ============
27
28 Long polling reduces the number of empty responses by allowing Amazon SQS to wait a specified time
29 for a message to become available in the queue before sending a response. Also, long polling eliminates
30 false empty responses by querying all of the servers instead of a sampling of servers. To enable long
31 polling, you must specify a non-zero wait time for received messages. You can do this by setting the
32 :code:`ReceiveMessageWaitTimeSeconds` parameter of a queue or by setting the :code:`WaitTimeSeconds`
33 parameter on a message when it is received.
34
35 In these examples, the AWS SDK for Python is used to enable long polling
36 using the following Amazon SQS methods.
37
38 * `create_queue <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.create_queue>`_
39
40 * `set_queue_attributes <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.set_queue_attributes>`_.
41
42 * `receive_message <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.receive_message>`_.
43
44 For more information, see
45 `Amazon SQS Long Polling <http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html>`_
46 in the *Amazon Simple Queue Service Developer Guide*.
47
48 Enable long polling when creating a queue
49 =========================================
50
51 The example below shows how to:
52
53 * Create a queue and enable long polling using
54 `create_queue <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.create_queue>`_.
55
56 Example
57 -------
58
59 .. code-block:: python
60
61 import boto3
62
63 # Create SQS client
64 sqs = boto3.client('sqs')
65
66 # Create a SQS queue with long polling enabled
67 response = sqs.create_queue(
68 QueueName='SQS_QUEUE_NAME',
69 Attributes={'ReceiveMessageWaitTimeSeconds': '20'}
70 )
71
72 print(response['QueueUrl'])
73
74 Enable long polling on an existing queue
75 ========================================
76
77 The example below shows how to:
78
79 * Enable long polling on an existing queue using
80 `set_queue_attributes <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.set_queue_attributes>`_.
81
82 Example
83 -------
84
85 .. code-block:: python
86
87 import boto3
88
89 # Create SQS client
90 sqs = boto3.client('sqs')
91
92 queue_url = 'SQS_QUEUE_URL'
93
94 # Enable long polling on an existing SQS queue
95 sqs.set_queue_attributes(
96 QueueUrl=queue_url,
97 Attributes={'ReceiveMessageWaitTimeSeconds': '20'}
98 )
99
100 Enable long polling on message receipt
101 ======================================
102
103 The example below shows how to:
104
105 * Enable long polling for a message on an SQS queue using
106 `receive_message <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.receive_message>`_.
107
108 Example
109 -------
110
111 .. code-block:: python
112
113 import boto3
114
115 # Create SQS client
116 sqs = boto3.client('sqs')
117
118 queue_url = 'SQS_QUEUE_URL'
119
120 # Long poll for message on provided SQS queue
121 response = sqs.receive_message(
122 QueueUrl=queue_url,
123 AttributeNames=[
124 'SentTimestamp'
125 ],
126 MaxNumberOfMessages=1,
127 MessageAttributeNames=[
128 'All'
129 ],
130 WaitTimeSeconds=20
131 )
132
133 print(response)