Skip to main content

Generic asynchronous message channel with routing by predicators

Project description

message-channel

PyPI PyPI - License PyPI - Python Version Test

This library provides a message channel object which subtract particular messages from mass of messages. It's like group by of SQL or ReactiveX but for asynchronous reader.

Installation

pip install python-message-channel

Usage

For example, assume that you have a string stream which messages are prefixed by a, b, ... e and you'd like to split subchannels for messages prefixed by b or d like below.

=============================================
---------------------------------> a:foo
--------------------+
--------------------|------------> c:foo
--------------------|------------> d:foo
--------------------|------------> e:foo
====================|========================
channel             |
                   =|========================
                    +------------> b:foo
                   ==========================
                   subchannel `m.startswith('b')`

This library is a tool for handling such situation. First, create a Channel instance from a steram reader and you can receive messages by channel.recv() method. In this example, we use asyncio.Queue as a stream.

import asyncio

from message_channel import Channel

async def main():
    # Create original stream
    stream = asyncio.Queue()

    # Create stream reader
    async def reader():
        return await stream.get()

    # Create stream channel
    async with Channel(reader) as channel:
        stream.put_nowait('a:foo')
        stream.put_nowait('b:foo')
        stream.put_nowait('c:foo')
        stream.put_nowait('d:foo')
        stream.put_nowait('e:foo')
        assert (await channel.recv()) == 'a:foo'
        assert (await channel.recv()) == 'b:foo'
        assert (await channel.recv()) == 'c:foo'
        assert (await channel.recv()) == 'd:foo'
        assert (await channel.recv()) == 'e:foo'


if __name__ == '__main__':
    asyncio.run(main())

And you can split the channel by channel.split() method by a predicator like

    async with Channel(reader) as channel:
        def predicator(m):
            return m.startswith('b:')

        async with channel.split(predicator) as sub:
            stream.put_nowait('a:foo')
            stream.put_nowait('b:foo')
            stream.put_nowait('c:foo')
            stream.put_nowait('d:foo')
            stream.put_nowait('e:foo')
            # sub receive messages starts from 'b:'
            assert (await sub.recv()) == 'b:foo'
            # channel (original) receive messages other than above
            assert (await channel.recv()) == 'a:foo'
            assert (await channel.recv()) == 'c:foo'
            assert (await channel.recv()) == 'd:foo'
            assert (await channel.recv()) == 'e:foo'

API documentation

https://fixpoint.github.io/python-message-channel/

powered by pdoc.

License

Distributed under the terms of the MIT License

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

python-message-channel-0.2.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

python_message_channel-0.2.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file python-message-channel-0.2.0.tar.gz.

File metadata

  • Download URL: python-message-channel-0.2.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.1 Linux/5.4.0-1032-azure

File hashes

Hashes for python-message-channel-0.2.0.tar.gz
Algorithm Hash digest
SHA256 5f644abc3f8ffb11797af64080f5a98a13b1ae41b00153ec2437f999e689566f
MD5 cb85020250f0480a76253d1541ca18f7
BLAKE2b-256 c9f9f00213815cd09028c4353d6c148de434dd751ef0f76607110e2d0801d892

See more details on using hashes here.

File details

Details for the file python_message_channel-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_message_channel-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 898308dae3e0b7438d2a48eb339e30cffd0ac46d25c78f1b792df885bf9209a3
MD5 05d42e75159b9f47b1548275b08ce9e2
BLAKE2b-256 2a4ab69687c2a93fe0a6593cae0b82042adb1bece6bdbdedbd9635e031dbcd8d

See more details on using hashes here.

Supported by

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