AsyncIO - Async/Await (Python >= 3.5 < 3.7)¶
Major change is PEP 492 – Coroutines with async and await syntax.
import asyncio
async def hello_world():
print("Hello World!")
loop = asyncio.get_event_loop()
loop.run_until_complete(hello_world())
loop.close()
import asyncio
async def compute(x, y):
print("Compute %s + %s ..." % (x, y))
await asyncio.sleep(1.0)
return x + y
async def print_sum(x, y):
result = await compute(x, y)
print("%s + %s = %s" % (x, y, result))
loop = asyncio.get_event_loop()
loop.run_until_complete(print_sum(1, 2))
loop.close()
Summary
Explicit syntax for asyncio: async and await statements
No coroutine decorator
Native coroutines