Skip to main content

Composable I/O primitives for async Python

Project description

comio – Composable I/O

I/O primitives for async & sync Python. Designed to be used as:

import comio as io          # async
import comio.sync as io     # sync

Giving you io.Reader, io.Writer, io.Listener, etc. — a Go-like DX for Python.

Install

pip install comio

Usage

Async

Implement a Reader / Writer

Any object with the right method is a valid io.Reader or io.Writer — no base class needed.

import comio as io

class AsyncJsonL:
    def __init__(self, f):
        self.f = f

    async def read(self, *, cursor=None, n=None) -> io.Page:
        self.f.seek(cursor or 0)
        line = self.f.readline()
        if line == "":
            return Page([], io.EOF)
        return Page(items=[json.loads(line)], next_cursor=self.f.tell())

    async def write(self, item: dict) -> None:
        self.f.write(json.dumps(item) + "\n")

Read pages with scroll

reader = AsyncJsonL(open("data.jsonl"))

async for page in io.scroll(reader):
    print(page.items)

Drain everything with read_all

items = await io.read_all(reader)

Resume from a cursor

items = await io.read_all(reader, cursor=saved_cursor)

Normalize a Reader into a stream with as_listener

async for item in io.as_listener(reader):
    process(item)

Buffered copy: Listener → Batcher

await io.copy(listener, batcher, n=100)  # flush every 100 items

Streaming pipe with backpressure

from comio import pipe

async def transform(item):
    return {**item, "processed": True}

await pipe(listener, writer, transform)

Sync

import comio.sync as io

class JsonL:
    def __init__(self, f):
        self.f = f

    def read(self, *, cursor=None, n=None):
        self.f.seek(cursor or 0)
        line = self.f.readline()
        if line == "":
            return Page([], io.EOF)
        return Page(items=[json.loads(line)], next_cursor=self.f.tell())

    def write(self, item: dict) -> None:
        self.f.write(json.dumps(item) + "\n")
reader = JsonL(open("data.jsonl"))

for page in io.scroll(reader):
    print(page.items)

items = io.read_all(reader)

for item in io.as_listener(reader):
    process(item)

Protocols

Async (comio)

Protocol Method Description
Reader[T] read Pull a page of items
Listener[T] listen Push items as an async iterator
Writer[T] write Accept a single item
Batcher[T] batch Accept a sequence of items

Sync (comio.sync)

Protocol Method Description
Reader[T] read Pull a page of items
Listener[T] listen Push items as an iterator
Writer[T] write Accept a single item
Batcher[T] batch Accept a sequence of items

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

comio-0.2.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

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

comio-0.2.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file comio-0.2.0.tar.gz.

File metadata

  • Download URL: comio-0.2.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for comio-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8147d0b5d3b2724518905b3dd78476a2bdab526dc710505ad022ca39d73f2ec3
MD5 f8d7a3d611d2e55a49e2c8d2421f3aa8
BLAKE2b-256 16bae6695f469daa5c20003455082f39c8ff6d4df74b7e0837a0b246df530208

See more details on using hashes here.

Provenance

The following attestation bundles were made for comio-0.2.0.tar.gz:

Publisher: publish.yml on sdkim96/comio

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: comio-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for comio-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6b1377eeb576d3f9f72054f45e2df63f5e74dd58c86fe7a53266bfd665741caa
MD5 1a7201af8b678e2a2e89aed395f3ed21
BLAKE2b-256 4551dffd50b9475beec9783e40561834ccd7de77ec1b41904a37760942d62624

See more details on using hashes here.

Provenance

The following attestation bundles were made for comio-0.2.0-py3-none-any.whl:

Publisher: publish.yml on sdkim96/comio

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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