【发布时间】:2014-01-07 01:02:10
【问题描述】:
所以,我正在尝试使用 SQS 在两个 EC2 实例之间传递 Python 对象。这是我失败的尝试:
import boto.sqs
from boto.sqs.message import Message
class UserInput(Message):
def set_email(self, email):
self.email = email
def set_data(self, data):
self.data = data
def get_email(self):
return self.email
def get_data(self):
return self.data
conn = boto.sqs.connect_to_region('us-west-2')
q = conn.create_queue('user_input_queue')
q.set_message_class(UserInput)
m = UserInput()
m.set_email('something@something.com')
m.set_data({'foo': 'bar'})
q.write(m)
它返回一条错误消息,指出The request must contain the parameter MessageBody。事实上,tutorial 告诉我们在将消息写入队列之前执行m.set_body('something')。但是在这里我没有传递字符串,我想传递我的 UserInput 类的实例。那么,MessageBody 应该是什么?我读过docs,他们这么说
The constructor for the Message class must accept a keyword parameter “body” which represents the content or body of the message. The format of this parameter will depend on the behavior of the particular Message subclass. For example, if the Message subclass provides dictionary-like behavior to the user the body passed to the constructor should be a dict-like object that can be used to populate the initial state of the message.
我想我的问题的答案可能在那段中,但我无法理解它。谁能提供一个具体的代码示例来说明他们在说什么?
【问题讨论】:
标签: python amazon-web-services amazon-ec2 boto amazon-sqs