Full Byte Developer.

Python create_task vs await for concurrent work

Await runs one coroutine at a time. asyncio.create_task schedules work so several coroutines can progress together.

Published here · 2 sources

I choose between await and asyncio.create_task() based on whether the next waits can overlap. Per asyncio docs, awaiting coroutines one after another finishes in the sum of their delays; creating Tasks lets those waits run concurrently. That is still one thread and one event loop, not OS-thread parallelism. [1]

Related reading: Python async def is lazy until you await, Python GIL: why threads are not truly parallel and JavaScript promises event loop quick start. Browse the dev-explainers topic.

Await in series when order is the point

The asyncio docs walk a timed example. Sequential awaits print hello after one second, then world after another two seconds, for about three seconds total: [1]

bash
python3 - <<'PY'
import asyncio, time

async def say_after(delay, what):
    await asyncio.sleep(delay)
    print(what)

async def main():
    print(f"started at {time.strftime('%X')}")
    await say_after(1, "hello")
    await say_after(2, "world")
    print(f"finished at {time.strftime('%X')}")

asyncio.run(main())
PY

asyncio.run() is the top-level entry point that runs main() and manages the loop. [1]

Create tasks when waits can overlap

The same docs show the concurrent form. Both Tasks are scheduled immediately, and waiting for both should take about two seconds instead of three: [1]

bash
python3 - <<'PY'
import asyncio, time

async def say_after(delay, what):
    await asyncio.sleep(delay)
    print(what)

async def main():
    task1 = asyncio.create_task(say_after(1, "hello"))
    task2 = asyncio.create_task(say_after(2, "world"))
    print(f"started at {time.strftime('%X')}")
    await task1
    await task2
    print(f"finished at {time.strftime('%X')}")

asyncio.run(main())
PY

create_task() wraps a coroutine in a Task so the event loop can run it concurrently with other Tasks. You still await the Task when you need the result or to surface exceptions. [1]

Prefer TaskGroup for structured sets

The docs present asyncio.TaskGroup as a more modern alternative. The await is implicit when the context manager exits: [1]

python
async def main():
    async with asyncio.TaskGroup() as tg:
        task1 = tg.create_task(say_after(1, "hello"))
        task2 = tg.create_task(say_after(2, "world"))
        print(f"started at {time.strftime('%X')}")
    print(f"finished at {time.strftime('%X')}")

Use TaskGroup when the set of child tasks should succeed or fail together. Keep bare create_task when you need a Task that outlives a narrow block and you will await it later yourself. [1]

Pick the shape that matches the wait graph

PatternUse when
await coro()The next step needs that result first
create_task then await taskIndependent I/O waits can overlap
async with TaskGroup()A set of child tasks should complete as a unit
asyncio.run(main())You need a single script entry point [1]

Calling an async def without await or create_task still only builds a coroutine object. See Python async def is lazy. For why overlapping Tasks are not OS-thread parallelism, see Python GIL threads are not parallel. [1]

Failure checklist

  1. Sequential awaits on independent I/O: wall time is the sum; switch to Tasks if overlap is safe
  2. create_task without a later await: errors can be deferred or lost; await or use TaskGroup
  3. Expecting CPU speedup from Tasks alone: still one event loop thread; use processes or native extensions for CPU [1]

Your next slow async script should get a wall-clock measurement of the sequential form, then the Task form, before anyone rewrites the architecture.

Send this to the teammate who awaits every call in a chain and then asks why the script feels slow.

Keep this guide

By Full Byte Developer

Filed under Dev explainers

Questions about this guide

Does await start a new thread?

No. Per asyncio docs, await suspends the current coroutine until the awaitable completes on the event loop.

When should I use create_task?

Per asyncio docs, use create_task to run coroutines concurrently as Tasks when you need overlapping waits.

Must I await the Task later?

Yes if you need its result or to observe errors. Per asyncio docs, awaiting the Task waits until that Task finishes.

What is TaskGroup?

Per asyncio docs, asyncio.TaskGroup is a more modern alternative to create_task for structured concurrency; awaits are implicit when the context manager exits.

Sources

Next in this stack