Aller au contenu principal
🔒 Mode aperçu. Les quinze premières leçons Foundations sont gratuites ; celle-ci est Pro. Démarrez un trial de 7 jours pour débloquer l'éditeur, les conseils AI et le reste du programme. Carte requise, annulez à tout moment dans Dashboard.Démarrer le trial de 7 jours →
← CoursSenior Deep-DivesModule 1 · Concurrency internals · RecapModèles d'annulation dans asynciopredict25 / 161
+125 XP
Tâche🌐 shown in EN
📝 **Question:** Predict the exact output. \`worker\` prints \`start\`, then awaits a long sleep. Main cancels the task. Trace: which prints fire, in what order? 📋 Pick the right answer. 💡 **Hint:** Re-read the theory above if unsure.
Prédis la sortie

Lis le code attentivement

import asyncio


async def worker():
    try:
        print("start")
        await asyncio.sleep(10)
        print("never")
    except asyncio.CancelledError:
        print("cleanup")
        raise


async def main():
    t = asyncio.create_task(worker())
    await asyncio.sleep(0)
    t.cancel()
    try:
        await t
    except asyncio.CancelledError:
        print("caught")
    print("after")


asyncio.run(main())

Que va afficher le programme ? Écris ici :