Simple message processing without really thinking about it. Morp can use dropfiles (simple text files), Postgres, and Amazon SQS.
Use pip to install the latest stable version:
pip install morp
Morp only supports the dropfiles interface out of the box, you'll need to install certain dependencies depending on what interface you want to use:
pip install morp[sqs]
pip install morp[postgres]
To install the development version:
pip install -U "git+https://github.com/Jaymon/morp#egg=morp"
Send and receive a Foo
message.
First, let's set our environment variable to use dropfiles (local files suitable for development and prototyping) interface:
export MORP_DSN=dropfile:${TMPDIR}
Then, let's create three files in our working directory:
tasks.py
- We'll define ourMessage
classes here.send.py
- We'll send messages from this script.recv.py
- We'll receive messages from this script.
Let's create our Message
class in tasks.py
:
# tasks.py
from morp import Message
class Foo(Message):
def target(self):
# this will be run when a Foo message is consumed
print(self.fields)
Now, let's flesh out our recv.py
file:
# recv.py
# import our Foo message class from our tasks.py file
from tasks import Foo
# Foo's handle method will call Foo.target
Foo.handle()
And start it up:
$ python recv.py
Finally, let's send some messages by fleshing out send.py
:
# send.py
from tasks import Foo
# create a message and send it manually
f = Foo()
f.some_field = 1
f.some_other_field = "one"
f.send()
# quickly send a message
Foo.create(
some_field=2,
some_other_field = "two"
)
And running it (it should send two messages):
$ python send.py
That's it! Our running recv.py
script should've received the messages we sent when we ran our send.py
script.
You configure your connection using a dsn in the form:
InterfaceName://username:password@host:port/path?param1=value1¶m2=value2
So, to connect to Amazon SQS, you would do:
sqs://${AWS_ACCESS_KEY_ID}:${AWS_SECRET_ACCESS_KEY}@
You can also override some default values like region
and read_lock
:
sqs://${AWS_ACCESS_KEY_ID}:${AWS_SECRET_ACCESS_KEY}@?region=${AWS_DEFAULT_REGION}&read_lock=120
pickle
(default)json
MORP_DSN="sqs://x:x@?serializer=json"
You might need to install some dependencies:
pip install morp[encryption]
If you would like to encrypt all your messages, you can pass in a key
argument to your DSN and Morp will take care of encrypting and decrypting the messages for you transparently.
Let's just modify our DSN to pass in our key:
sqs://${AWS_ACCESS_KEY_ID}:${AWS_SECRET_ACCESS_KEY}@?key=jy4XWRuEsrH98RD2VeLG62uVLCPWpdUh
That's it, every message will now be encrypted on send and decrypted on receive. If you're using SQS you can also use Amazon's key management service to handle the encryption for you.
By default every message will be sent, if you just want to test functionality without actually sending the message you can set this environment variable to turn off all the queues.
MORP_DISABLED = 1 # queue is off
MORP_DISABLED = 0 # queue is on
If you would like to have your queue names prefixed with something (eg, prod
or dev
) then you can set this environment variable and it will be prefixed to the queue name.
Set this environment variable with your connection DSN so Morp can automatically configure itself when the interface is first requested.
By default, Morp will send any message from any morp.Message
derived class to Message.get_name()
, you can override this behavior by giving your child class a .name
attribute:
from morp import Message
class childMsg(Message):
name = "custom-queue-name"