Sobes.tech
Back to tasks
Junior — Senior
60

Параллельный запуск задач с разной задержкой

Get help with live coding in real time with Sobes Copilot
Task condition

Ситуация: одна coroutine задерживается, другая выводит результат сразу. Требуется указать, в каком порядке появятся сообщения при одновременном запуске:

import asyncio

async def delayed_task():
    await asyncio.sleep(10)
    print("foo")

async def instant_task():
    print("bar")

async def run_all():
    await asyncio.gather(delayed_task(), instant_task())

asyncio.run(run_all())