Comments (2)
Hmm interesting problem!
It turns out that aiostream can handle streams of streams using the advanced operators, so that's probably what you're after here. The missing part is the ability to split a stream into a steam of streams where items are forwarded depending on a given predicate.
Here's an example of a split
operator that would do just that:
from typing import AsyncIterable, TypeVar, Callable, AsyncIterator
from aiostream import pipable_operator, stream, pipe
from aiostream.core import streamcontext, Streamer
from aiostream.aiter_utils import AsyncExitStack
from anyio import create_memory_object_stream, BrokenResourceError
from anyio.abc import ObjectSendStream
T = TypeVar("T")
K = TypeVar("K")
@pipable_operator
async def split(
source: AsyncIterable[T], key_function: Callable[[T], K], max_buffer_size: float = 0
) -> AsyncIterator[tuple[K, Streamer[T]]]:
mapping: dict[K, ObjectSendStream[T]] = {}
async with AsyncExitStack() as stack:
async with streamcontext(source) as source:
async for chunk in source:
key = key_function(chunk)
if key not in mapping:
sender, receiver = create_memory_object_stream[T](
max_buffer_size=max_buffer_size
)
mapping[key] = await stack.enter_async_context(sender)
yield key, streamcontext(receiver)
try:
await mapping[key].send(chunk)
except BrokenResourceError:
pass
Note how it uses a key function to tell where each produced item belongs. Here's an example of this operator being used:
@pytest.mark.asyncio
async def test_split():
def is_even(x: int) -> bool:
return x % 2 == 0
def split_stream(
key: bool, stream: Streamer[int], *_
) -> AsyncIterable[int | list[int]]:
match key:
case True:
return stream | pipe.accumulate(initializer=0) | pipe.takelast(1)
case False:
return stream[:3] | pipe.list() | pipe.takelast(1)
xs = (
stream.range(10, interval=0.1)
| split.pipe(is_even)
| pipe.starmap(split_stream)
| pipe.flatten()
| pipe.list()
)
assert await xs == [[1, 3, 5], 20]
Here the key function is simply whether the item is even or odd. Then starmap
can be used to apply specific stream operations depending on this predicate. For the sake of this example, the even numbers will summed together while the first 3 odd numbers are gathered as a list. Then both results are produced using the advanced flatten
operator.
Here's a diagram of the corresponding pipeline:
graph TD;
A(range) --> B(split);
B --> C(starmap);
C --> D(accumulate);
D --> E(takelast);
C --> F(take);
F --> G(list);
G --> H(takelast);
E --> I(flatten);
H --> I;
I --> J(list);
Does that correspond to your use case?
from aiostream.
Thanks! ya this is awesome.
from aiostream.
Related Issues (20)
- Incompatibility with mypy 1.7 and later
- Docstring and prototype not properly shown with pylance HOT 1
- New operators
- No easy to see installation instructions in the docs or README HOT 1
- merge causes anext(): asynchronous generator is already running Exception HOT 2
- question: how to use chain HOT 8
- Feature suggestion: `strict` parameter for `zip` HOT 3
- iterating from a synchronous iterator blocks the event loop HOT 6
- add support python3.10 HOT 1
- Please upload a wheel release to pypi HOT 2
- `stream.list` implementation is x10 slower than using plain Python built-in functionality (list comprehension) HOT 2
- Idea: Timeouts on chunks HOT 4
- action: support task_limit HOT 5
- Regarding `update_pipe_module` HOT 3
- Aiostream fails to import with a TypeError HOT 1
- CI test pipeline doesn't run in Python 3.12: ModuleNotFoundError: No module named 'setuptools' HOT 6
- License change HOT 3
- 0.5.0 (#84) made backwards incompatible changes HOT 6
- asyncio.Event for graceful/early termination HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aiostream.