So, I'm trying to use SQS to pass a Python object between two EC2 instances. Here is my unsuccessful attempt:
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)
It returns an error message The request must contain the parameter MessageBody. In fact, the tutorial tells us to do m.set_body('something')before writing the message to the queue. But here I am not passing a string, I want to pass an instance of the UserInput class. So what should be MessageBody? I read the docs and they say that
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.
I think the answer to my question may be in this paragraph, but I cannot understand. Can someone provide a specific code example to illustrate what they are talking about?