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
import socket as sc

from simio import net


async def echo(socket: sc.socket) -> None:
    loop = aio.get_running_loop()

    peer_address, peer_port = net.get_socket_peer_address(socket)
    logging.info("client %s:%d connected", peer_address, peer_port)

    data = await loop.sock_recv(socket, 1024)
    await loop.sock_sendall(socket, 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.0.0.tar.gz (9.2 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.0.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: simio-1.0.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.14.2 Linux/6.11.0-1018-azure

File hashes

Hashes for simio-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c534bfa8557970fd509a6b8de7ad534967ee09dfb2779f05b6a4f21668aff45f
MD5 e6b281afc772a2f88fdabc49fbfbdfe6
BLAKE2b-256 f33708972c3aa92046943855ce8c62f7392970949b525e81907e88e82f157578

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simio-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.14.2 Linux/6.11.0-1018-azure

File hashes

Hashes for simio-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0c65ea4c3380225c4987b7d5f61005579c900a419f2e749c4f693a2e69e7648f
MD5 a074c283dfeb55d43ff5ba49145da00a
BLAKE2b-256 4718f4188df5c8451df7e822d4534ef983d60b0f2459a7596bf84a0774262f69

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