Why await does not work?

There is such code:

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

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

caller()


The caller is await, which suspends its execution until execute the calee function. But after running this script, the console displays the following error message:

(asyncio) md@md ~/.MINT18/code/python/asyncio $ python index.py
index.py:8: RuntimeWarning: coroutine 'caller' was never awaited
caller()
RuntimeWarning: Enable tracemalloc to get the traceback object allocation


Which stated that the caller did not wait for the execution of the caller. Please help to understand the problem and fix the script
April 4th 20 at 00:59
4 answers
April 4th 20 at 01:01
Solution
asyncio.run(caller())
April 4th 20 at 01:03
All coroutine (functions using the async/await) must be executed in the eventloop. This is what is specified in warning that the eventloop has not been started and coroutine has not been called.

Start coroutine you can do this:
asyncio.run(caller())

This call should be done to the main function where all the asynchronous calls.
April 4th 20 at 01:05
Add the await keyword to print
so?

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

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

caller()
- hollie commented on April 4th 20 at 01:08
@hollie, Yes, did it work? - mac82 commented on April 4th 20 at 01:11
@mac82, no :) - hollie commented on April 4th 20 at 01:14
@hollie, Google then, I don't know Python so js XD) - mac82 commented on April 4th 20 at 01:17
April 4th 20 at 01:07
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

Find more questions by tags PythonAsynchronous programming