CKTIS / Python
Mot-clé await
Dans une coroutine ou un générateur asynchrone, reçoit le résultat ou l’exception d’un objet attendable.
Version : Python 3.14; repository image, production runtime not attested
Syntaxe
result = await awaitableDans une coroutine ou un générateur asynchrone, reçoit le résultat ou l’exception d’un objet attendable.
Un résultat prêt peut éviter la suspension, tandis qu’une opération inachevée peut rendre la main à la boucle. Une coroutine achevée ne se réutilise pas.
Un nouvel appel à une fonction coroutine crée un nouvel objet coroutine.
Exemples
import asyncio
async def job():
await asyncio.sleep(0)
return 7
async def run():
obj = job()
print(await obj)
try:
await obj
except RuntimeError as error:
print(type(error).__name__)
asyncio.run(run())
async def stream():
yield await job()
async def check():
assert [n async for n in stream()] == [7]
asyncio.run(check())
Résultat
7 RuntimeError
Un nouvel appel à une fonction coroutine crée un nouvel objet coroutine.