Z
Z
zlodiak2020-01-07 01:16:22
Python
zlodiak, 2020-01-07 01:16:22

Why isn't await firing?

There is this code:

async def callee():
    print('Hello')

async def caller():
    await callee()
    print('World')

caller()

The caller function has an await that suspends its execution until the calee function is executed. But after running this script, the console displays the following error message:
(asyncio) [email protected] ~/.MINT18/code/python/asyncio $ python index.py
index.py:8: RuntimeWarning: coroutine 'caller' was never awaited
caller()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

Which says that the caller function did not wait for caller to execute. Please help me understand the problem and fix the script

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
Ivan Yakushenko, 2020-01-07
@zlodiak

asyncio.run(caller())

A
Alexander, 2020-01-07
@monester

All coroutines (functions using async/await) must be executed in the eventloop. This is exactly what is indicated in the warning that the eventloop was not launched and the coroutine was not called.
The launch of the coroutine can be done like this:
This call must be made for the main function where all asynchronous calls are located.

V
Vladimir, 2020-01-07
@HistoryART

Add await to print

M
MasterCard000, 2020-01-08
@MasterCard000

I understand you forgot to import the library

import asyncio

async def callee():
    print('Hello')

async def caller():
    await callee()
    print('World')

if __name__ == '__main__':
    asyncio.run(caller())

5e1521adbe673666227516.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question