Skip to main content

Send and receive messages without thinking about it

Project description

Morp

Simple message processing without really thinking about it. Morp can use dropfiles (simple text files), Postgres, and Amazon SQS.

Installation

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"

1 Minute Getting Started

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 our Message 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):
    some_field: int
    some_other_field: str
    
    def handle(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 asyncio

# import our Foo message class from our tasks.py file
from tasks import Foo

# Foo's `process` method will call `Foo.handle` for each Foo instance received
asyncio.run(Foo.process())

And start it up:

$ python recv.py

Finally, let's send some messages by fleshing out send.py:

# send.py

import asyncio

from tasks import Foo

async def send_messages():
    # create a message and send it manually
    f = Foo()
    f.some_field = 1
    f.some_other_field = "one"
    f.ignored_field = True
    await f.send()

    # quickly send a message
    await Foo.create(
        some_field=2,
        some_other_field="two",
    )
    
asyncio.run(send_messages())

And running it in a separate shell from the shell running our recv.py script (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.

DSN

You configure your connection using a dsn in the form:

InterfaceName://username:password@host:port/path?param1=value1&param2=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

Serializers

  • pickle (default)
  • json
MORP_DSN="sqs://x:x@?serializer=json"

Encryption

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.

Environment configuration

MORP_DISABLED

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

MORP_PREFIX

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.

MORP_DSN

Set this environment variable with your connection DSN so Morp can automatically configure itself when the interface is first requested.

FAQ

I would like to have multiple queues

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 property:

from morp import Message

class childMsg(Message):
    _name = "custom-queue-name"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

morp-7.0.0.tar.gz (23.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

morp-7.0.0-py3-none-any.whl (24.7 kB view details)

Uploaded Python 3

File details

Details for the file morp-7.0.0.tar.gz.

File metadata

  • Download URL: morp-7.0.0.tar.gz
  • Upload date:
  • Size: 23.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for morp-7.0.0.tar.gz
Algorithm Hash digest
SHA256 d4d2069243cde5b1de9ebee9fda5aba0bec0a99af988da35774e2d8391f8328a
MD5 7bc9718d93dc5cc727e87dad89915398
BLAKE2b-256 b0383624e593d5b709289f4120b0fcd41ad3c8d2f8acc2dfc5eca9709056d635

See more details on using hashes here.

File details

Details for the file morp-7.0.0-py3-none-any.whl.

File metadata

  • Download URL: morp-7.0.0-py3-none-any.whl
  • Upload date:
  • Size: 24.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for morp-7.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a6a2ebb4eaa21682799815281202cf832db900ba0dac811613a2583ab3b4bece
MD5 3836b57ab7baa22d21df8a67a1d5af04
BLAKE2b-256 27cffc40dff347d046a4cba9fe45b5727c77e87edd5a0b0ce11f8ac9d1647014

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page