Comments (2)
Hey! Thanks for the report :)
I am trying to understand the meaning of 'a stream can be streamed multiple times'.
Oh yea that can be confusing. That simply means that a given stream can be used multiple times, as in:
xs = stream.range(3) | pipe.list()
assert await xs == [0, 1, 2]
assert await xs == [0, 1, 2]
And I can confirm it is fine to run them concurrently, as they correspond to two different streamer
instantiation:
assert await asyncio.gather(xs, xs) == [[0, 1, 2], [0, 1, 2]]
Your example is a bit trickier though, as the stream you built depends on an external source, which means the two streamers iterate the same async generator concurrently. Here's how I would re-write it:
import asyncio
from functools import partial
from aiostream import stream, pipe, async_
async def produce():
i = 1
while True:
yield i
i += 1
await asyncio.sleep(1)
async def consume(cid, item):
print(f'Consumer {cid} got: {item}')
await asyncio.sleep(.1)
return item
async def main():
producer_stream = stream.iterate(produce())
async with producer_stream.stream() as producer:
consumer_stream_1 = (
stream.preserve(producer)
| pipe.take(2)
| pipe.map(async_(partial(consume, 1)))
)
consumer_stream_2 = (
stream.preserve(producer)
| pipe.take(4)
| pipe.map(async_(partial(consume, 2)))
)
await asyncio.gather(consumer_stream_1, consumer_stream_2)
if __name__ == "__main__":
asyncio.run(main())
Notice how stream.preserve is used to prevent consumer_stream_1
from closing the producer once it's finished so consumer_stream_2
can keep working on it.
I noticed 2 bugs while playing with this example plus the one you already noticed on the anyio
branch, I'll try to do some proper reporting tomorrow.
Let me know if you have any questions :)
from aiostream.
After consumer 1 finishes, this crashes for me with:
AttributeError: 'generator' object has no attribute 'cr_await'
But if I understand you correctly, there shouldn't be anything inherently wrong with trying to iterate the same external generator in parallel?
from aiostream.
Related Issues (20)
- Incompatibility with mypy 1.7 and later
- Docstring and prototype not properly shown with pylance HOT 1
- New operators
- Rate limit / throttle operator HOT 19
- Task exception was never retrieved warning HOT 3
- Cancelling pending tasks in same TaskGroup causes RuntimeError HOT 3
- UnboundLocalError when trying to reuse a stream HOT 1
- 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
- How to handle stream splitting HOT 2
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.