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.1.tar.gz (6.6 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.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: comio-0.2.1.tar.gz
  • Upload date:
  • Size: 6.6 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.1.tar.gz
Algorithm Hash digest
SHA256 87c5a4f018d2292e02ec3829eb2124805aa6b2aa84a43c7643675d340afb2dcd
MD5 4bd9a38b97dec86027ca0bc29ba3e2ff
BLAKE2b-256 b66ab8bd872ac94eaf017d2c6d858f4dba4a91ba352046b08046970207caf0b1

See more details on using hashes here.

Provenance

The following attestation bundles were made for comio-0.2.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: comio-0.2.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9c931cebadae26b728fa02ac7ad0d7511d69fda1814460752cd94983044a82db
MD5 fbcffc023d9ed66962b5d2984683adff
BLAKE2b-256 de21604880355ca8f672f9dd97105a9f3c8b19a846c9564a62e1692a27a9bd70

See more details on using hashes here.

Provenance

The following attestation bundles were made for comio-0.2.1-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