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 json
from dataclasses import dataclass
import comio as io

@dataclass
class Page:
    items: list
    next_cursor: object

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

    async def read(self, *, cursor=None, n=None) -> 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 Listener with as_listener

listener = io.as_listener(reader)

async for item in listener.listen():
    process(item)

Buffered copy: Listener → Batcher

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

Streaming pipe with backpressure

import comio as io
from comio import pipe, PipeConfig

listener = io.as_listener(reader)

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

await pipe(listener, writer, transform, cfg=PipeConfig(buffer=10))

Glue pipes with asyncio queues

import asyncio
from comio import pipe
from comio.adapters.asyncio_queue import QueueListener, QueueWriter

q: asyncio.Queue = asyncio.Queue()
glue_src = QueueListener(q)
glue_dst = QueueWriter(q)

async def transform(item):
    return {**item, "step": 1}

async def enrich(item):
    return {**item, "step": 2}

# pipe1: listener -> transform -> queue
await pipe(listener, glue_dst, transform)
glue_src.close()

# pipe2: queue -> enrich -> final destination
await pipe(glue_src, writer, enrich)

Sync

import json
from dataclasses import dataclass
import comio.sync as io

@dataclass
class Page:
    items: list
    next_cursor: object

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

    def read(self, *, cursor=None, n=None) -> 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())

    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)

listener = io.as_listener(reader)

for item in listener.listen():
    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
Handler[I,O] __call__ Transform a single item

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

Adapters

Adapter Module Implements
QueueListener comio.adapters.asyncio_queue Listener
QueueWriter comio.adapters.asyncio_queue Writer, Batcher
Stdin comio.adapters.std Listener
Stdout comio.adapters.std Writer, Batcher
TextFileReader comio.adapters.text_file Reader
TextFileWriter comio.adapters.text_file Writer, Batcher

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.4.0.tar.gz (8.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.4.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: comio-0.4.0.tar.gz
  • Upload date:
  • Size: 8.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.4.0.tar.gz
Algorithm Hash digest
SHA256 1b758dbe77f12a99b01549feac3788be5e37453a7e5cf070ec157500cd97aaba
MD5 b9ac26ab478d752150074dd464a8eede
BLAKE2b-256 97b8e59c12fd0c6b94e1cb55a5684e240e097261822dd9ccb3b820f7b48bd321

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: comio-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 11.8 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.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 499c4992043aa2e7ced7e917afdc0df7ff6aa1fbf62c2e42bbcfa17c3e024182
MD5 6687dc6766ae0ab4fa6969a35335ae06
BLAKE2b-256 55f7d74bb0e352733a80875e18f13d2b8740903fad102e45da88fd4fd2d4dee3

See more details on using hashes here.

Provenance

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