-
-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
[Asyncio] Support for async → sync → async call chains #93462
Comments
It looks like I found a similar solution in fsspec, thanks to @isidentical! Would be great to have something similar in asyncio as well! |
Could you change the title to something like |
Was able to create a solution to this in this PR, using fsspec. |
Is that a typo? It just links back to this same issue. Separately, I note that the solution in fsspec (linked from the previous comment) just runs the whole thing in a separate thread, using a separate event loop. That's pretty heavy machinery, not something I'd like to enable with a quick call or decorator -- it'd just encourage a bad architecture. |
Yeah it seems like a typo, removed it. I definitely agree that fsspec's solution is a hacky solution, there were a lot of occassions I needed this feature in the past, and think this is a critical feature. |
I have needed something like this myself when wrapping an async library with a sync interface, the solution I came up with is very complicated for an average user so would be nice to have support for this given that https://github.com/erdewit/nest_asyncio library which allows something like this is downloaded 17 million per month so in high demand. |
This would require an event loop which supports reentrancy which does has it downsides so even if this were to be supported it should be behind a user configurable flag and should definitely be not the default behavior. |
Millions of downloads may be the result of being the dependency of one other very popular package; nest_asyncio only has modest numbers of starts/forks/issues. Nevertheless, it seems to just patch a bunch of asyncio functions to allow re-entrancy, which looks much lighter weight than running in a thread (pool). What would it take to just incorporate those patches in the stdlib asyncio? I guess we'd have to reimplement them in C for the C accelerator to keep working (nest_asyncio disables that), but that's a matter of work. If those patches don't introduce other bugs. My post from 7 years ago makes it sound rather dubious though. But I'd be happy to see someone try this experiment and report back how it went. (Try hard to come up with scenarios that break it, that's the interesting part of the experiment.) |
WoW what an ancient issue :D |
Please continue the discussion on #66435 |
Feature or enhancement
Support for running async functions in sync functions
Pitch
Currently running async functions inside sync functions is only available with
asyncio.run(func(x))
orloop.run_until_complete()
; however when there is a running loop, these calls cannot be used.The only way is running the function in background, and using callback functions(which is very limiting)
See example PR
I think there should be function to handle all cases of running async funcs in sync funcs
Suggested syntax:
synchronize_async(func(*args))
The text was updated successfully, but these errors were encountered: