-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
asyncio.Runner allows async generators to skip gracefull finalization #95584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I think this is a fundamental problem with allowing an event loop to be paused and resumed. currently pausing and resuming a loop can impact other threads relying on the main thread loop to handle processes
it makes sense for low-level hooks like loop.run_until_complete and loop.close to permit this, but high-level APIs shouldn't expose users to these sorts of edge cases cc @njsmith |
Is this an issue in practice though? I don't see any point in using a async generator across multiple runs. It should be iterated over once in the cycle. |
@graingert Do you have a fix in mind? |
FWIW I consider this to be a misuse of the API here not as a bug, you are not supposed to create async generator without a running event loop otherwise you end up not triggering async hooks. @graingert Do you have any use case for this? otherwise I'll close this a won't fix. See my comment #95584 (comment) |
But that feels like an arbitrary restriction. I could imagine doing exactly that to control some test case, for example. (The advantage of doing it this way rather than the other way would be that the test itself can remain synchronous.) |
I guess you are not aware of how async gen initialization and finalization works. See https://docs.python.org/3/library/sys.html#sys.set_asyncgen_hooks, using it without a running loop is inviting issues. |
That sounds a little condescending. :-) I sometimes try to see things from the POV of an unsophisticated user (which honestly I still am, most of the time :-). The docs there don't actually explain a thing, the semantics are in PEP 525. I could look that up, but basically my point is that nobody who isn't an asyncio implementation expert understands how async generators really work, so the restrictions feel arbitrary (even though I remember that when I approved the PEP I understood why they existed and couldn't think of a better way either). At the very least the docs should explain the restrictions of async generators. Not sure where that should go though -- the |
I imagine that this isn't something an unsophisticated user could ever hit. You can only ever hit this if you are trying hard to do it. The general case is that an
+1 for better documentation. |
I created #100108 to add better documentation for async generators. |
Closing as #100108 covers this. |
reproducer:
output:
The text was updated successfully, but these errors were encountered: