| 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-visibility-timeout: |
| 12 |
|
| 13 |
######################################### |
| 14 |
Managing visibility timeout in Amazon SQS |
| 15 |
######################################### |
| 16 |
|
| 17 |
This Python example shows you how to specify the time interval during which messages received by a |
| 18 |
queue are not visible. |
| 19 |
|
| 20 |
The scenario |
| 21 |
============ |
| 22 |
|
| 23 |
In this example, Python code is used to manage visibility timeout. The code uses the SDK for Python |
| 24 |
to manage visibility timeout by using this method of the AWS.SQS client class: |
| 25 |
|
| 26 |
* `set_queue_attributes <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.set_queue_attributes>`_. |
| 27 |
|
| 28 |
For more information about Amazon SQS visibility timeout, see |
| 29 |
`Visibility Timeout <http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-visibility-timeout.html>`_ |
| 30 |
in the *Amazon Simple Queue Service Developer Guide*. |
| 31 |
|
| 32 |
Prerequisite tasks |
| 33 |
================== |
| 34 |
|
| 35 |
To set up and run this example, you must first complete these tasks: |
| 36 |
|
| 37 |
* Create an Amazon SQS queue. For an example of creating an Amazon SQS |
| 38 |
queue, see :ref:`aws-boto3-sqs-create-queue`. |
| 39 |
|
| 40 |
* Send a message to the queue. For an example of sending a message to a |
| 41 |
queue, see :ref:`aws-boto3-sqs-send-message`. |
| 42 |
|
| 43 |
Change the visibility timeout |
| 44 |
============================= |
| 45 |
|
| 46 |
The example below shows how to: |
| 47 |
|
| 48 |
* Change the visibility timeout using |
| 49 |
`set_queue_attributes <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.set_queue_attributes>`_. |
| 50 |
|
| 51 |
Example |
| 52 |
------- |
| 53 |
|
| 54 |
.. code-block:: python |
| 55 |
|
| 56 |
import boto3 |
| 57 |
|
| 58 |
# Create SQS client |
| 59 |
sqs = boto3.client('sqs') |
| 60 |
|
| 61 |
queue_url = 'SQS_QUEUE_URL' |
| 62 |
|
| 63 |
# Receive message from SQS queue |
| 64 |
response = sqs.receive_message( |
| 65 |
QueueUrl=queue_url, |
| 66 |
AttributeNames=[ |
| 67 |
'SentTimestamp' |
| 68 |
], |
| 69 |
MaxNumberOfMessages=1, |
| 70 |
MessageAttributeNames=[ |
| 71 |
'All' |
| 72 |
], |
| 73 |
) |
| 74 |
|
| 75 |
message = response['Messages'][0] |
| 76 |
receipt_handle = message['ReceiptHandle'] |
| 77 |
|
| 78 |
# Change visibility timeout of message from queue |
| 79 |
sqs.change_message_visibility( |
| 80 |
QueueUrl=queue_url, |
| 81 |
ReceiptHandle=receipt_handle, |
| 82 |
VisibilityTimeout=20 |
| 83 |
) |
| 84 |
print('Received and changed visibility timeout of message: %s' % message) |