Comments (2)
Thanks for creating this issue, maybe it's something that should be better documented.
Consider the following asynchronous generator:
async def count_to_ten():
try:
for i in range(10):
yield i
await asyncio.sleep(1)
finally:
print('cleaning up')
Now, say you're only interested in the first three values:
async def main():
xs = stream.enumerate(agen())
async for i, x in xs:
if i == 3:
break
print(x)
print('done!')
And you run the main coroutine using the standard boiler-plate code:
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
Let's check the output:
[...]: UserWarning: AsyncIteratorContext is iterated outside of its context
"AsyncIteratorContext is iterated outside of its context")
0
1
2
done!
And that's it, no clean up! According to PEP 525, that's because you're actually supposed to add the following line before closing the loop:
loop.run_until_complete(loop.shutdown_asyncgens())
Notice however that 'cleaning up'
will appear after 'done!'
which is usually not what you want. By using the stream method, the clean up gets to run before leaving the corresponding context:
async def main():
xs = stream.enumerate(agen())
async with xs.stream() as streamer:
async for i, x in streamer:
if i == 3:
break
print(x)
print('done!')
Note that if you use await
instead of the asynchronous iteration, the streaming context is not necessary:
async def main():
xs = stream.iterate(agen())
print(await xs[:3])
print('done!')
Now if you can think of a more intuitive interface, I'd be happy to look into it!
from aiostream.
Looks like I stepped into a minefield here.
https://vorpus.org/blog/some-thoughts-on-asynchronous-api-design-in-a-post-asyncawait-world/
This is actually broken in python IMHO.. If DB can't come up with any good solution, I am not even going to try. Closing.
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.