Prečítaj kód pozorne
import asyncio
results: list[str] = []
async def critical(name: str) -> str:
await asyncio.sleep(0.01)
results.append(name)
return name
async def main() -> None:
t = asyncio.create_task(critical("commit"))
try:
out = await asyncio.wait_for(asyncio.shield(t), timeout=0.001)
print(f"got {out}")
except asyncio.TimeoutError:
print("timeout")
# Give the shielded task time to finish in the background.
await asyncio.sleep(0.05)
print(f"results: {results}")
print(f"task done: {t.done()}")
asyncio.run(main())
Čo program vypíše? Napíš sem: