Skip to main content

Simple zero-dependency asynchronous IO

Project description

Downloads License Python Versions Build status

simio

Python simple zero-dependency asynchronous IO.

Motivation

Python 3.4 introduced native support for asynchronous code and announced asyncio standard library. async/await syntax provided a very convenient way to write single-threaded concurrent code but asyncio library itself caused a lot of pain to developers since it was ugly designed and provides very inconvenient api and asynchronous primitives. To fix that problem developers implemented some third-party libraries to replace the standard one like trio.

You may use trio or anyio, but trio implements its own runtime, anyio although work on top of asyncio but provides too high level interface which may be undesirable for small projects.

This library is intended to solve some of that tensions.

Features

  • Buffered streams
  • TCP stream
  • TCP server

Installation

You can install simio with pip:

pip install simio

Quickstart

Buffered stream:

import asyncio as aio

from simio import net, stream


async def main() -> None:
    async with await net.open_tcp_stream("httpforever.com", 80) as http_stream:
        buffered_stream = stream.BufferedStream(http_stream)
        await buffered_stream.write_all(
            b'GET / HTTP/1.1\r\n'
            b'Host: httpforever.com\r\n'
            b'\r\n',
        )
        status = await buffered_stream.read_until(b'\r\n')
        print(status.decode())

        headers = await buffered_stream.read_until(b'\r\n\r\n')
        headers = dict(line.split(b':', maxsplit=1) for line in headers.removesuffix(b'\r\n\r\n').split(b'\r\n'))

        body = await buffered_stream.read_exactly(size=int(headers[b'Content-Length']))
        print(body.decode())


aio.run(main())

Echo TCP server:

import asyncio as aio
import logging

from simio import net


async def echo(socket: net.TcpSocket) -> None:
    logging.info("client %s connected", socket.getpeername())

    data = await socket.recv(1024)
    await socket.sendall(data)


async def main() -> None:
    logging.basicConfig(level=logging.INFO)

    await net.start_tcp_server("127.0.0.1", 8080, echo, graceful_shutdown=10.0)


aio.run(main())

TCP client stream:

import asyncio as aio

from simio import net


async def main() -> None:
    async with await net.open_tcp_stream("127.0.0.1", 8080) as tcp_stream:
        await tcp_stream.write(b"Hello World!!!")

        while data := await tcp_stream.read(1024):
            print(data)


aio.run(main())

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

simio-1.1.0.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

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

simio-1.1.0-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file simio-1.1.0.tar.gz.

File metadata

  • Download URL: simio-1.1.0.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.4 CPython/3.14.4 Linux/6.17.0-1010-azure

File hashes

Hashes for simio-1.1.0.tar.gz
Algorithm Hash digest
SHA256 991a12eecd0b823a6571006e1178d9956567470fa1cdcd200c8a9f4f7c4a82c2
MD5 08c3a3a5477bc30c53e2c863f648dc7b
BLAKE2b-256 bd24b638839909621d0825cc784addbd1842805cd5bfe9d295c6d58448e88ed8

See more details on using hashes here.

File details

Details for the file simio-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: simio-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.4 CPython/3.14.4 Linux/6.17.0-1010-azure

File hashes

Hashes for simio-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 69495189121a2ba81c219c165044dfca5016f9841078a574ba45f5d3504dc536
MD5 38a21de67dd3c932fc3f7a9ca25bdb45
BLAKE2b-256 61e42c6005c8cf569b515f5765b7e7066121fd4683a7200c3948dbda722560b8

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