Skip to main content

Asyncio TCP Multiplexer

Project description

async_multiplexer

PyPI version Python 3.8 Build Status codecov

PEP8 black Checked with mypy

async_multiplexer is a TCP Multiplexer based on Mplex protocol, but simplified. It is intended for creating mutiple streams in parallel on top of a same TCP connection.

Installation

pip install async-multiplexer

Usage

Client

import asyncio
from async_multiplexer import open_multiplexer_context

async def echo_client():
    async with open_multiplexer_context("127.0.0.1", 7777) as multiplexer:
        stream_echo_1 = await multiplexer.multiplex("echo.1")
        stream_echo_2 = await multiplexer.multiplex("echo.2")
        await stream_echo_1.write(b"echo.1")
        await stream_echo_2.write(b"echo.2")


if __name__ == "__main__":
    asyncio.run(echo_client())

Server

import asyncio
from async_multiplexer import bind_multiplex_listener_context

async def handler(stream):
    data = await stream.read()
    print(data)

async def echo_server():
    async with bind_multiplex_listener_context("127.0.0.1", 7777) as listener:
        listener.set_handler("echo.1", handler)
        listener.set_handler("echo.2", handler)
        await asyncio.sleep(10)

if __name__ == "__main__":
    asyncio.run(echo_server())

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

async_multiplexer-0.1.0.tar.gz (9.1 kB view hashes)

Uploaded Source

Built Distribution

async_multiplexer-0.1.0-py3-none-any.whl (13.1 kB view hashes)

Uploaded Python 3

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