. Advertisement .
..3..
. Advertisement .
..4..
The error: “RuntimeError: asyncio.run() cannot be called from a running event loop” is a common error that can show up in many ways. In this blog, we will go through some of the ways you can fix this issue. Read on.
When Does The Error: “RuntimeError: asyncio.run() cannot be called from a running event loop” Happen?
When attempting to utilize asyncio, you might encounter the following issue.
ModuleNotFoundError: No module named 'wtforms.compat'
The asyncio.run() document indicates that: You can not call this function when there is another asyncio event loop running in the same thread. In your situation, the issue is that jupyter (IPython) is already operating in an event loop (for IPython ≥ 7.0).
This is the reason why you have got the error ”RuntimeError: asyncio.run() cannot be called from a running event loop”.
How To Solve The Error: “RuntimeError: asyncio.run() cannot be called from a running event loop”
Approach 1: Utilize asyncio like this
You have to utilize asyncio like this to solve your error ”RuntimeError: asyncio.run() cannot be called from a running event loop”.
import asyncio
async def main():
print(1)
asyncio.run(main())
This will work without a hitch.
Approach 2: Utilize nest_asyncio
Another solution for your error is that you simply need to utilize nest_asyncio. This is an instance:
import nest_asyncio
nest_asyncio.apply()
Approach 3: Utilize the following example
Consider the following instance:
import asyncio
from unsync import unsync
@unsync
async def example_async_function():
await asyncio.sleep(0.1)
return "Run Successfully!"
print(example_async_function().result())
Output:
Run Successfully!
Approach 4: Upgrade IPython to version 7+ and IPykernel to version 5+
Besides the above solutions, there is another method for you to fix ”RuntimeError: asyncio.run() cannot be called from a running event loop” erro. In the notebook and in the IPython terminal, you can use async/await at the top level; it should for the most part, “just work”. Let’s upgrade IPython to version 7+ and IPykernel to version 5+, and you’re ready to the races.
For this reason, you may directly run await main(url) even outside of asynchronous functions in Jupyter without having to initiate the event loop yourself.
In Jupyter:
async def main():
print(1)
await main()
In plain Python (≥3.7):
import asyncio
async def main():
print(1)
asyncio.run(main())
In your code that would look like:
url = ['url1', 'url2']
result = await main(url)
for text in result:
pass # text contains your html (text) response
Conclusion
We hope you enjoyed our article about the error. With this knowledge, we know that you can fix your error: “RuntimeError: asyncio.run() cannot be called from a running event loop” quickly by following these steps! If you still have any other questions about fixing this syntax error, please leave a comment below. Thank you for reading!
Read more
→ Tips On Solving The Error: “runtimeError: package fails to pass a sanity check for numpy and pandas”
Leave a comment