Saltar para o conteúdo principal
🔒 Modo de pré-visualização. As primeiras quinze aulas de Foundations são grátis; esta é Pro. Inicie um trial de 7 dias para desbloquear o editor, as dicas AI e o resto do programa. Cartão necessário, cancele a qualquer momento no Dashboard.Iniciar trial de 7 dias →
← CursosSenior Deep-DivesMódulo 1 · Simultaneidade e aspectos internos assíncronosasyncio: aprofundamento do loop de eventospredict3 / 161
+100 XP
Tarefa🌐 shown in EN
📝 **Task:** Predict the 5-line interleave from `asyncio.gather(fetch('A', 1), fetch('B', 2))`. Both fetches print 'start NAME' first, then 'done NAME' after their await — and gather prints the result list last. 📋 Implement the function above. Tests run automatically. 💡 **Hint:** Re-read the theory if you get stuck.
Prevê a saída

Lê o código com atenção

import asyncio

async def fetch(name, delay):
    print(f"start {name}")
    await asyncio.sleep(delay)
    print(f"done {name}")
    return name

async def main():
    results = await asyncio.gather(
        fetch("A", 1),   # sleeps 1 s
        fetch("B", 2),   # sleeps 2 s
    )
    print(results)

asyncio.run(main())

# What does this print? Type your prediction.
# Hint: both fetches enter immediately (gather schedules them
# together). A finishes first (shorter sleep). The list at the end
# matches the ORDER the tasks were passed to gather, NOT the
# completion order.

O que o programa vai imprimir? Escreve aqui: