Fixing Python asyncio Error – RuntimeWarning coroutine was never awaited

Python asyncio Error – RuntimeWarning coroutine was never awaited is an error which occurs sometimes when you use asyncio from inside a Coroutine and sometimes for other reasons.

In today’s article I am going to deal with a confusing error and explain why it takes place and how to fix it, with a set of possible solutions.

Exploring the Python asyncio Error – RuntimeWarning coroutine was never awaited

This is an error which occurs sometimes when you use asyncio from inside a Coroutine and sometimes for other reasons.

Beware of mixing between different errors. Please make sure you are dealing with the same error.

                                                                       #
RuntimeWarning: coroutine '...' was never awaited
                                                                       #

Bellow we will take care of the error using multiple possible solutions according to your needs.

The Method that solved my issue : Do not use asyncio from inside a Coroutine

It is hard to avoid this error when you use asyncio from inside a Coroutine

A Coroutine is a computer program component to generalize subroutines for non-preemptive multitasking, by allowing execution to be suspended and resumed.

The coroutine is created when calling an awaitable function using async def

A loop cannot run in this case, since the loop wont even start in the first place if it is inside an asynchronus function.

So, the solution is to make the function synchronus. Instead of using the code bellow.

                                                                       #
async def myfunction():
    loop = asyncio.get_event_loop()
    # <your code>
    loop.close()
    print("...")
                                                                       #

You should remove async def and replace it with only def, the code should become like this

                                                                       #
def myfunction():
    loop = asyncio.get_event_loop()
    # <your code>
    loop.close()
    print("...")
                                                                       #

The code also wont work if you do not import asyncio at the beginning of your code

                                                                       #
import asyncio

async def main()
.....
                                                                       #

I hope the fixes above helped you get rid of the issue.

I hope this guide solved your problem, thank you for reading this blog post to the end.

Summing-up : 

We arrived at the end of this quest to solve this annoying error, I hope me sharing my experience with you helped, I hope the other solutions helped, If you like this website support us on Kofi and keep browsing, thank you.

Thank you for reading, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/