无法使用boto3将消息写入SQS

问题描述 投票:2回答:2

我正在使用boto3,AWS库连接到他们的SQS服务。我正在尝试从队列连接,读取和写入消息,但这似乎不起作用,文档也无济于事

这是我的代码,任何人都可以发现我在做什么错?

#Connect to a session
session = Session(aws_access_key_id=SQSAccessKey, aws_secret_access_key=SQSSecretKey,region_name=sqsRegion)

#Connect to a resource
sqs= session.resource('sqs')

queue = sqs.get_queue_by_name(QueueName=transactionQueue)
print(queue.url)

# Create a new message
print 'creating new message'
toWrite = 'hello world'
response = queue.send_message(MessageBody=toWrite)
print(response.get('MessageId'))

#Reading messages in queue
messages = queue.receive_messages()
print 'there are %s messages in the queue' % len(messages)
for message in messages:
    # SQS Message
    message.body
    message.delete()

在将新消息发送到队列(并打印出消息ID)后,我尝试读取消息队列,但是它没有返回新消息,就好像没有写任何消息。

我做错什么了吗?

谢谢!

python amazon-web-services amazon-sqs boto3
2个回答
2
投票

当队列较小时,您不能保证会收到任何消息作为响应。这是分布式队列的属性。有关更多信息,请参见this page


2
投票

这里有两件事在起作用。一方面,我不小心使用了另一个组件正在读取的队列(并删除了其消息)。我没有意识到这一点。另外,乔丹的评论也是一个问题,因为我一次只能进行一次阅读测试。

© www.soinside.com 2019 - 2024. All rights reserved.