CKTIS / Python
Parola chiave await
In una coroutine o in un generatore asincrono riceve il risultato o l’eccezione di un oggetto awaitable.
Versione: Python 3.14; repository image, production runtime not attested
Sintassi
result = await awaitableIn una coroutine o in un generatore asincrono riceve il risultato o l’eccezione di un oggetto awaitable.
Un risultato pronto può evitare la sospensione e un’operazione pendente può cedere il controllo al ciclo. Una coroutine completata non si riusa.
Una nuova chiamata a una funzione coroutine crea una nuova coroutine.
Esempi
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())
Risultato
7 RuntimeError
Una nuova chiamata a una funzione coroutine crea una nuova coroutine.