Composable I/O primitives for async Python
Project description
cio – Composable I/O
I/O primitives for async Python. Designed to be used as:
import comio as io
Giving you io.Reader, io.Writer, io.Listener, etc. — a Go-like DX for Python.
Install
pip install comio
Usage
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 JsonL:
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 = JsonL(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 cio import pipe
async def transform(item):
return {**item, "processed": True}
await pipe(listener, writer, transform)
Protocols
| 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 |
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.1.0.tar.gz
(5.8 kB
view details)
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
comio-0.1.0-py3-none-any.whl
(6.5 kB
view details)
File details
Details for the file comio-0.1.0.tar.gz.
File metadata
- Download URL: comio-0.1.0.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ecf04b0667b7ca462ef410346d9d21d73574454a40ddbdf44cd7c4d6ba08ea6
|
|
| MD5 |
0a412af3ee14c761aec2bff4274ae90a
|
|
| BLAKE2b-256 |
ceab553416d0040081686d2ebf09744d45d6cf29febeb5b0c8d73a5aac9e00d4
|
File details
Details for the file comio-0.1.0-py3-none-any.whl.
File metadata
- Download URL: comio-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
70f60ddfbd0ff8fe12f79a3758046997260286142c0531aab53edbeb9d5298bd
|
|
| MD5 |
e4f384c542b8f4c6f729d72dec1bf697
|
|
| BLAKE2b-256 |
9749f6a1e073b46b90ef163f59d68e96e9f3c4828b90627b27f90ab1ba717bba
|