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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
87c5a4f018d2292e02ec3829eb2124805aa6b2aa84a43c7643675d340afb2dcd
|
|
| MD5 |
4bd9a38b97dec86027ca0bc29ba3e2ff
|
|
| BLAKE2b-256 |
b66ab8bd872ac94eaf017d2c6d858f4dba4a91ba352046b08046970207caf0b1
|
Provenance
The following attestation bundles were made for comio-0.2.1.tar.gz:
Publisher:
publish.yml on sdkim96/comio
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
comio-0.2.1.tar.gz -
Subject digest:
87c5a4f018d2292e02ec3829eb2124805aa6b2aa84a43c7643675d340afb2dcd - Sigstore transparency entry: 1767452132
- Sigstore integration time:
-
Permalink:
sdkim96/comio@83a4b5019aaaa2bd1ecc1b61393beb1036e4e6b1 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/sdkim96
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@83a4b5019aaaa2bd1ecc1b61393beb1036e4e6b1 -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c931cebadae26b728fa02ac7ad0d7511d69fda1814460752cd94983044a82db
|
|
| MD5 |
fbcffc023d9ed66962b5d2984683adff
|
|
| BLAKE2b-256 |
de21604880355ca8f672f9dd97105a9f3c8b19a846c9564a62e1692a27a9bd70
|
Provenance
The following attestation bundles were made for comio-0.2.1-py3-none-any.whl:
Publisher:
publish.yml on sdkim96/comio
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
comio-0.2.1-py3-none-any.whl -
Subject digest:
9c931cebadae26b728fa02ac7ad0d7511d69fda1814460752cd94983044a82db - Sigstore transparency entry: 1767452245
- Sigstore integration time:
-
Permalink:
sdkim96/comio@83a4b5019aaaa2bd1ecc1b61393beb1036e4e6b1 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/sdkim96
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@83a4b5019aaaa2bd1ecc1b61393beb1036e4e6b1 -
Trigger Event:
release
-
Statement type: