Я хотел бы создать две функции receive_message и delete_message из одной очереди sqs. Должен ли я использовать тот же клиент sqs для получения и удаления, или я могу использовать разные? Есть ли какие-либо накладные расходы на создание нового клиента sqs, который имеет значение с точки зрения скорости и производительности?
Я провел два теста.
Тест 1 - использование разных клиентов sqs в функции приема и удаления сообщений. и я записал прошедшее время.
Тест 2 - это использование тех же sqs-клиентов для получения и удаления, а также я записал прошедшее время
test1:
def receive_delete_message():
"""Receive message from SQS"""
sqs = boto3.client("sqs")
queue_url = "my sqs url"
response = sqs.receive_message(
QueueUrl=queue_url,
AttributeNames=[
'SentTimestamp'
],
MaxNumberOfMessages=10, # [1, 10] default is 1, 10 is desired
MessageAttributeNames=[
'All'
],
VisibilityTimeout=1, # default is 30sec
WaitTimeSeconds=20 # [0, 20]. short poll if 0, otherwise long poll
)
if "Messages" not in response: # empty sqs queue
print("empty queue")
return
messages = response["Messages"]
for i, message in enumerate(messages):
receipt_handle = message['ReceiptHandle']
print("this is message {}, {}".format(i+1, message["Body"]))
delete_message(receipt_handle)
def delete_message(receipt_handle):
sqs2 = boto3.client("sqs")
queue_url = "my sqs url"
sqs2.delete_message(
QueueUrl=queue_url,
ReceiptHandle=receipt_handle
)
print("message deleted")
start = time.time()
receive_delete_message()
print(time.time() - start)
test2:
sqs = boto3.client("sqs")
queue_url = "my sqs url"
def receive_delete_message():
"""Receive message from SQS"""
response = sqs.receive_message(
QueueUrl=queue_url,
AttributeNames=[
'SentTimestamp'
],
MaxNumberOfMessages=10, # [1, 10] default is 1, 10 is desired
MessageAttributeNames=[
'All'
],
VisibilityTimeout=1, # default is 30sec
WaitTimeSeconds=20 # [0, 20]. short poll if 0, otherwise long poll
)
if "Messages" not in response: # empty sqs queue
print("empty queue")
return
messages = response["Messages"]
for i, message in enumerate(messages):
receipt_handle = message['ReceiptHandle']
print("this is message {}, {}".format(i+1, message["Body"]))
delete_message(receipt_handle)
def delete_message(receipt_handle):
sqs.delete_message(
QueueUrl=queue_url,
ReceiptHandle=receipt_handle
)
print("message deleted")
start = time.time()
receive_delete_message()
print(time.time() - start)
результат теста 1:
this is message 1, message1000
message deleted
this is message 2, message1002
message deleted
this is message 3, message1003
message deleted
this is message 4, message1004
message deleted
this is message 5, message1007
message deleted
this is message 6, message1008
message deleted
this is message 7, message1018
message deleted
this is message 8, message1025
message deleted
this is message 9, message1034
message deleted
this is message 10, message1036
message deleted
1.526839017868042
результат теста 2
this is message 1, message1002
message deleted
this is message 2, message1013
message deleted
this is message 3, message1017
message deleted
this is message 4, message1028
message deleted
this is message 5, message1029
message deleted
this is message 6, message1044
message deleted
this is message 7, message1047
message deleted
this is message 8, message1005
message deleted
this is message 9, message1006
message deleted
this is message 10, message1015
message deleted
0.5138881206512451