▼ what is streamable
Only one import: stream[T], wrapping any Iterable[T]/AsyncIterable[T] (or function) with a lazy fluent interface exposing:
.map: (concurrent) transformations
.do: (concurrent) side effects
.group: batching (by key, within a time window)
.flatten: (concurrent) un-batching
.filter: element selection
.take: restrict to leading elements
.skip: ignore leading elements
.catch: error handling
.throttle: rate limiting
.buffer: background pre-fetch
.observe: iteration progress monitoring
▼ example: extract pokemonspip install streamable==2.1.0 (no dependencies)
import logging
from datetime import timedelta
from httpx import AsyncClient, HTTPStatusError, Response, get
from streamable import stream
sync: pokemons: stream[str] = (
stream(range(10))
.map(lambda i: f"https://pokeapi.co/api/v2/pokemon-species/{i}")
.throttle(5, per=timedelta(seconds=1))
.map(get, concurrency=2)
.do(Response.raise_for_status)
.observe("responses")
.catch(HTTPStatusError, do=logging.warning)
.map(lambda poke: poke.json()["name"])
.observe("pokemons")
)
assert list(pokemons) == [
'bulbasaur', 'ivysaur', 'venusaur', 'charmander', 'charmeleon', 'charizard', 'squirtle', 'wartortle', 'blastoise'
]
async, same code, just with an async HTTP client: async with AsyncClient() as async_http:
pokemons: stream[str] = (
stream(range(10))
.map(lambda i: f"https://pokeapi.co/api/v2/pokemon-species/{i}")
.throttle(5, per=timedelta(seconds=1))
.map(async_http.get, concurrency=2)
.do(Response.raise_for_status)
.observe("responses")
.catch(HTTPStatusError, do=logging.warning)
.map(lambda poke: poke.json()["name"])
.observe("pokemons")
)
assert [pokemon async for pokemon in pokemons] == [
'bulbasaur', 'ivysaur', 'venusaur', 'charmander', 'charmeleon', 'charizard', 'squirtle', 'wartortle', 'blastoise'
]
▼ use casese.g. LLM/API enrichment, web scraping, DB/file transfers, event queue consumption, batched CPU-bound processing, ETL / reverse ETL.
▼ comparison
There are many libraries that look similar (e.g. pypeln, aiostream, RxPY, PyFunctional, streamz, pipe, asyncstdlib, aioitertools, toolz, more-itertools), but to my knowledge none of them offers at the same time:
- a unified sync/async interface.
- a typed fluent interface designed for Python, not porting idioms from other languages / paradigms.
- operations focused on stream transformations, delegating opinionated source/consumption/retries to dedicated builtins/libraries.
- resilient iteration: An exception raised during a stream's iteration doesn't break it: you can keep iterating.
- robustness locked into rigorous unit tests (e.g. surviving mid-pull KeyboardInterrupt/asyncio.CancelledError, no refcycles from raised exceptions) run on a large test matrix.
- structured concurrency.
- builtins-level performance on simple sync map/filter.
streamable is not relevant when distributed processing or advanced functional programming features are needed. Also for simple use cases, you may not find it worth adding another dependency to your project if loops, comprehensions, stdlib's itertools and Executor.map (with the new buffersize param I contributed to 3.14) are enough, but that's a matter of taste.
---
Version 2.1.0 (thanks to contributors!) brings support for structured concurrency among other things: https://github.com/ebonnal/streamable/blob/main/CHANGELOG.md
Happy to discuss, thanks for your attention!
ebonnal•8m ago
- intuitive: a small surface of carefully named methods and params, so that reviewers barely need to learn anything to understand what is going on.
- composable: operations provide few atomic behaviors, and most of the complex use cases can be tackled by composing them.
- robust: a large test matrix (CPython 3.8 to 3.15 and 3.14 free-threaded, PyPy, Linux/Windows/macOS, asyncio/uvloop), a regression test for every fix, full branch coverage, thorough documentation.
Open to challenge!