我编写了以下代码,将几条消息写入SQS队列并立即删除它们。但是我在输出中看到每条消息都被即时删除。然而,我看到消息计数非常随机出现。我的意思是它最初显示1,然后它再次显示2,尽管删除了一个。我已经将可见性超时设置为50秒,就像这个post中提到的那样。我尝试了同样的程序。
所以我怀疑删除消息是否立即发生。如果是这样,那将如何影响线程数。请参阅下面的代码和输出。
__author__ = 'Dhanapathi.Marepalli'
import boto.sqs
conn = boto.sqs.connect_to_region("us-east-1", aws_access_key_id="********",
aws_secret_access_key="************")
my_queue = conn.get_queue('SMSQueue')
def write_messages():
print("Writing messages.")
for i in range(1, 5):
new_message = Message()
message_body = str(random.randint(1111111111, 9999999999))
print("Message body is ", message_body)
new_message.set_body(message_body)
my_queue.write(new_message)
print("Message with message body {0} is written.".format(message_body))
print("Messages completed. Totally {0} messages written to Queue.".format(my_queue.count()))
def retrieve_messages():
while True:
counter = 0
rs = my_queue.get_messages()
for message in rs:
counter = 1
body = message.get_body()
print(current_thread().getName(), " Message is ", body)
print(current_thread().getName(), " Assume something happened in between.")
is_message_deleted = my_queue.delete_message(message)
if is_message_deleted:
print("{0}: Message with body {1} is deleted.".format(current_thread().getName(), body))
else:
print("{0}: Message with body {1} failed to delete.".format(current_thread().getName(), body))
print(current_thread().getName(), " thread count is ", my_queue.count())
if counter == 0:
print("Thread1:No messages inside the Queue.")
break
if __name__ == "__main__":
write_messages()
retrieve_messages()
生成的输出如下:
Writing messages.
Message body is 8111076750
Message with message body 8111076750 is written.
Message body is 4927934264
Message with message body 4927934264 is written.
Message body is 8511692382
Message with message body 8511692382 is written.
Message body is 9718698315
Message with message body 9718698315 is written.
Messages completed. Totally 4 messages written to Queue.
MainThread Message is 8111076750
MainThread Assume something happened in between.
MainThread: Message with body 8111076750 is deleted.
MainThread thread count is 3
MainThread Message is 4927934264
MainThread Assume something happened in between.
MainThread: Message with body 4927934264 is deleted.
MainThread thread count is 2
MainThread Message is 9718698315
MainThread Assume something happened in between.
MainThread: Message with body 9718698315 is deleted.
MainThread thread count is 1
MainThread Message is 8511692382
MainThread Assume something happened in between.
MainThread: Message with body 8511692382 is deleted.
MainThread thread count is 2
Thread1:No messages inside the Queue.
提前致谢。
答案 0 :(得分:4)
SQS是一个分布式系统。你想要它的规模和可靠性。但是,由于系统的分布式特性,它不能保证100%的一致性。为了获得队列中项目的计数,boto使用队列属性ApproximateNumberOfMessages
,顾名思义,它只能提供队列中可用消息的近似计数。
如果您的应用程序需要队列中消息的准确计数,则SQS可能不是您问题的正确答案。但是,它是一种非常可靠和可扩展的排队服务,适用于了解其一致性限制的应用程序。