I think I'm getting this error because my code calls asyncio.get_event_loop().run_until_complete(foo())
twice. Once from foo()
and second time from function called by foo()
. My question is then: why should this be a problem? Why should I even care that this loop is running?
There was an edit made to this question which, I think, obscured it (some people prefer to follow rules without understanding them, thus an "illegal" word was removed from the title). Unfortunately, this creates confusion.
I'm not surprised by the fact that the error is raised. I can trace it back to the asyncio
source and see that the authors of this library wanted to do it this way, there's no mystery there. The puzzling part is in the reason the authors of the library decided it's illegal to ask from event loop to run some function to completion when the loop is already running.
We can reduce the problem to just two such calls, and through case analysis we will see that these are the three possibilities:
Now, is there any sane behavior which would address all three cases? To me, it is obvious that there is, or, perhaps are multiple sane behaviors possible here. For example:
run_until_complete()
until second function completes (thus no code after run_until_complete()
will be executed.run_until_complete
ignoring all other invocation sites.Now, I can understand that this behavior may not be something that everyone would want. But, since this library decided to give programmers control over starting / stopping the event loop, it should also meet the consequences of such decisions. Making it an error to start the same loop multiple times precludes library code from ever doing this, which reduces the quality and usefulness of libraries utilizing asyncio
(which is indeed the case with, for example, aiohttp
).
The event loop is the core of every asyncio application. Event loops run asynchronous tasks and callbacks, perform network IO operations, and run subprocesses. Application developers should typically use the high-level asyncio functions, such as asyncio.
An async function uses the await keyword to denote a coroutine. When using the await keyword, coroutines release the flow of control back to the event loop. To run a coroutine, we need to schedule it on the event loop. After scheduling, coroutines are wrapped in Tasks as a Future object.
nest-asyncio is a tool in the PyPI Packages category of a tech stack. nest-asyncio is an open source tool with 424 GitHub stars and 34 GitHub forks.
ensure_future is a method to create Task from coroutine . It creates tasks in different ways based on argument (including using of create_task for coroutines and future-like objects). create_task is an abstract method of AbstractEventLoop .
I got the issue resolved by using the nest_async
pip install nest-asyncio
and adding below lines in my file.
import nest_asyncio nest_asyncio.apply() __import__('IPython').embed()
Event loop running - is an entry point of your async program. It manages running of all coroutines, tasks, callbacks. Running loop while it's running makes no sense: in some sort it's like trying to run job executor from same already running job executor.
Since you have this question, I guess you may misunderstand a way how asyncio works. Please, read this article - it's not big and gives a good introduction.
Upd:
There's absolutely no problem in adding multiple things to be ran by event loop while this loop is already running. You can do it just by awaiting for it:
await coro() # add coro() to be run by event loop blocking flow here until coro() is finished
or creating a task:
# python 3.7+ asyncio.create_task(coro()) # add coro() to be run by event loop without blocking flow here # This works in all Python versions but is less readable asyncio.ensure_future(coro())
As you can see you don't need call event loop's methods to make something being ran by it.
Event loop's method such as run_forever
or run_until_complete
— are just a ways to start event loop in general.
run_until_complete(foo())
means: "add foo()
to be ran by event loop and run event loop itself until foo()
isn't done".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With