我能找到的关于此方法的所有文档都说 Queue.write 返回 True 或 False,具体取决于写入是否成功,但这与现实不符。
文档说:
The write method returns a True if everything went well. If the write didn't succeed it will either return a False (meaning SQS simply chose not to write the message for some reason) or an exception if there was some sort of problem with the request.
但实际上该方法只是返回传入的消息。这是来自https://github.com/boto/boto/blob/develop/boto/sqs/queue.py 的相关源代码。 :
def write(self, message, delay_seconds=None):
"""
Add a single message to the queue.
:type message: Message
:param message: The message to be written to the queue
:rtype: :class:`boto.sqs.message.Message`
:return: The :class:`boto.sqs.message.Message` object that was written.
"""
new_msg = self.connection.send_message(self,
message.get_body_encoded(),
delay_seconds)
message.id = new_msg.id
message.md5 = new_msg.md5
return message
那么我的问题是:如何真正判断写入是否成功?
最佳答案
您提供的文档引用来自 SQS 教程。 SQS API docs正确描述当前的返回值。 SQS 教程已经过时,需要更正。我创建了一个 issue跟踪这个。
如果写入由于任何原因失败,服务将返回一个 HTTP 错误代码,这反过来又会导致 boto 引发 SQSError 异常。如果没有抛出异常,则写入成功。
关于python - 如何判断 boto.sqs.Queue.write() 是否成功?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21083772/