Lesson 11.13
Asynchrony helps you not wait for nothing while one task is waiting (for a network response, for example):
import asyncio
async def fetch(name):
return "done " + name
async def main():
one, two = await asyncio.gather(fetch("A"), fetch("B"))
print(one)
print(two) # example: the two names from the brackets above
asyncio.run(main())
async def creates a coroutine, and it has to be started;await waits for a result without blocking the other tasks;asyncio.gather starts several coroutines together;asyncio.run(main()) is the entry point.The check sets two names first and second. Write fetch(name), which returns
done <name>, and main, where gather gets both and prints their results one by one.
asyncio.sleep timing is not needed — the check only cares about the order of work.
An async function is declared with async def, and its result is taken
with await.
asyncio.gather(...) starts both tasks and gives the results in the same order
in which they were passed. The launch is asyncio.run(main()).