does nest-asyncio runs coroutins on separate thread?

242 views Asked by At

We're using tornadoweb which already runs an event loop single-threaded, and we want to use asyncio.run to run other coroutines, but it show error 'Event loop already running', found this library nest-asyncio that allows event loop to be nested, sorry not expert on threading or event loop, does nest-asyncio runs coroutines on separated thread?or no connection on threading?

1

There are 1 answers

2
xyres On

If the event loop is already running, you can't call asyncio.run.

You have a few options:

1. Tornado's IOLoop.add_callback:

This will run the coroutine in the background (that means you won't be able to get it's result).

from tornado import ioloop

loop = ioloop.IOLoop.current() # get the current running event loop

loop.add_callback(coro_func) # coro_func is the coroutine you want to run

2. Use asyncio.create_task:

This will also allow you to get the coroutine's result.