Hello Guys, How are you all? Hope You all Are Fine. Today I am trying to use asyncio But I am facing following error RuntimeError: asyncio.run() cannot be called from a running event loop in Python. So Here I am Explain to you all the possible solutions here.
Without wasting your time, Let’s start This Article to Solve This Error.
How this Error Occurs ?
I am trying to use asyncio But I am facing following error.
RuntimeError: asyncio.run() cannot be called from a running event loop
How To Solve RuntimeError: asyncio.run() cannot be called from a running event loop Error ?
How To Solve RuntimeError: asyncio.run() cannot be called from a running event loop Error ?
To Solve RuntimeError: asyncio.run() cannot be called from a running event loop Error You need to Use asyncio like this. import asyncio async def main(): print(1) asyncio.run(main()) This will run without error. Second solution Just use nest_asyncio.
RuntimeError: asyncio.run() cannot be called from a running event loop
To Solve RuntimeError: asyncio.run() cannot be called from a running event loop Error You need to Use asyncio like this. import asyncio async def main(): print(1) asyncio.run(main()) This will run without error. Second solution Just use nest_asyncio.
Solution 1: Use asyncio like this
You need to Use asyncio like this.
import asyncio
async def main():
print(1)
asyncio.run(main())
This will run without error.
Solution 2: Use nest_asyncio
Just use nest_asyncio Here is example.
import nest_asyncio
nest_asyncio.apply()
Solution 3: Use this Example
Explore This Example.
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!
Summary
It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?
Also, Read