Run until complete in thread / nested event loops #341
-
It would be nice to have the "opposite" of from anyio import to_thread
async def foo():
print('Hello, world!')
def main():
to_thread.run_async(foo)
main() It would help to solve the problem where you want to run an async function in a blocking way but you don't know if you are being called from an already running event loop, so you cannot run until complete. An alternative is to use nest-asyncio, but it has issues too. Maybe it could also be made transparent, such as the following code works: from anyio import run
async def foo():
print('Hello, world!')
async def main():
run(foo)
run(main) This currently gives a |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
there's https://anyio.readthedocs.io/en/stable/api.html#anyio.from_thread.start_blocking_portal |
Beta Was this translation helpful? Give feedback.
-
Just in case someone reads this issue, this is the way. Thanks! from anyio import run, from_thread
async def foo():
print('Hello, world!')
async def main():
with from_thread.start_blocking_portal() as portal:
portal.call(foo)
run(main) |
Beta Was this translation helpful? Give feedback.
Just in case someone reads this issue, this is the way. Thanks!