Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python create_task vs await: Key Differences

Understand when to use asyncio.create_task versus await in Python, including scheduling behavior, error handling, and common async I/O patterns.

~5 min readPython
#asyncio#concurrency#coroutines#event loop
A visual comparison of Python asyncio's create_task and await showing a task being scheduled on an event loop versus a coroutine waiting for a result.

Python's asyncio library offers two common ways to run a coroutine: await and asyncio.create_task. Both are essential in async code, but they serve different purposes. This article explains the difference and gives concrete guidance on when to use each.

What await and create_task Actually Do

await suspends the current coroutine until the awaited object (usually another coroutine or a Future) produces a result. It is a sequential operation: the current coroutine yields control to the event loop and resumes when the awaited operation completes. The result is directly available to the calling code.

asyncio.create_task wraps a coroutine in a Task and schedules it to run on the event loop. The call returns immediately with a Task object. The coroutine starts executing on a later event-loop cycle, and the caller does not wait for its completion unless it explicitly awaits the task later.

The Core Difference: Scheduling vs Waiting

The key distinction is that await is about waiting for a result, while create_task is about scheduling work to run in the background. When you await a coroutine, you are saying: "I need this result before I can continue." When you call create_task, you are saying: "Start this coroutine now, and I'll deal with it later."

This difference affects how the event loop interleaves execution. await suspends only the current coroutine, not the event loop; other tasks can run while the awaited operation is pending. create_task does not suspend the caller; it adds the coroutine to the event loop's queue and returns immediately.

Minimal Example: Sequential vs Concurrent Execution

Consider two coroutines that each sleep for one second:

python
import asyncio async def wait_one(): await asyncio.sleep(1) return "done" async def sequential(): result1 = await wait_one() result2 = await wait_one() return [result1, result2] async def concurrent(): task1 = asyncio.create_task(wait_one()) task2 = asyncio.create_task(wait_one()) return [await task1, await task2]

In sequential, the two wait_one calls run one after another, taking two seconds total. In concurrent, both tasks are scheduled immediately, so they run concurrently and complete in roughly one second. This is the most direct illustration of why create_task is used for concurrency.

When to Use await

Use await when you need the result of a coroutine before you can proceed. This is typical when the next step depends on the returned value. For example, fetching a user ID and then fetching that user's profile:

python
user_id = await get_user_id() profile = await get_profile(user_id)

Here, you cannot fetch the profile until you have the user ID. await enforces the dependency and keeps the code readable and linear.

await is also the correct way to consume a coroutine that you have already scheduled as a task. You still need to await the task to get its result or to ensure it completes before the program exits.

When to Use create_task

Use create_task when you want to start a coroutine without blocking the current flow. This is useful for:

  • Running multiple independent operations concurrently.
  • Starting a background job that should continue while the main coroutine does other work.
  • Implementing producer/consumer patterns where you need to kick off several workers.

A common pattern is to create tasks for independent network calls and then await them all together:

python
async def fetch_all(urls): tasks = [asyncio.create_task(fetch(url)) for url in urls] return await asyncio.gather(*tasks)

Here, all fetches are scheduled before any of them is awaited. If you instead wrote [await fetch(url) for url in urls], the fetches would run one at a time. You can also pass coroutines directly to asyncio.gather; it wraps them in tasks and schedules them for you. Using create_task explicitly is useful when you need task handles for cancellation, callbacks, or awaiting individual tasks after doing other work.

Error Handling and Propagation

Exceptions in a coroutine that is directly awaited are raised at the await point. You can catch them with a normal try/except block.

With create_task, exceptions are stored in the Task object. They are not raised unless you await the task. If you never await the task or otherwise retrieve its exception, Python can report an unhandled task exception. To handle errors, await the task or use a done callback and call task.exception().

python
task = asyncio.create_task(coro()) try: result = await task except SomeError: # handle error

If you create a task and never retrieve its exception, that error is easy to lose. Keep a reference to the task and either await it or inspect task.exception() in a callback.

Performance and Concurrency Implications

The main benefit of create_task is overlapping I/O waits. When a coroutine performs I/O (network, disk, sleep), it releases control to the event loop, allowing other tasks to run. Scheduling multiple tasks lets those I/O waits overlap, reducing total wall-clock time.

create_task does have overhead, but for ordinary numbers of tasks it is negligible. If you are creating many tasks and do not need each task handle, asyncio.gather can accept coroutines directly and schedule them for you; that is often the less verbose pattern.

create_task does not make CPU-bound code concurrent. If a coroutine performs CPU-bound work without yielding, it blocks the event loop and prevents other tasks from running. In that case, offload the work to a thread or process regardless of whether you use await or create_task.

Common Pitfalls and Misconceptions

One common mistake is creating a task in a loop and then immediately awaiting it, which defeats the purpose. For example:

python
# Wrong: this runs sequentially tasks = [] for url in urls: task = asyncio.create_task(fetch(url)) tasks.append(await task)

This awaits each task right after creation, so the next task is not scheduled until the previous one finishes. Instead, create all tasks first and then await them, as shown in the earlier example.

Another pitfall is forgetting to keep a reference to a task. A task that is not referenced elsewhere can be garbage-collected before it finishes, so store tasks in a list or variable if you intend to await them later.

Also, create_task requires a running event loop. Normally you call it from inside a coroutine or from code running on the loop. From another thread, use asyncio.run_coroutine_threadsafe; from ordinary synchronous startup code, put the task creation inside an async function and run it with asyncio.run.

Decision Criteria: Choosing Between create_task and await

SituationRecommended Approach
Need the result before continuingawait
Independent operations that can run concurrentlycreate_task + await later
Fire-and-forget background workcreate_task (but handle exceptions)
Sequential dependency between coroutinesawait
Starting many I/O-bound tasksasyncio.gather directly, or create_task in a list then gather
CPU-bound workNeither; use run_in_executor

In short, await is for when you need to wait, and create_task is for when you want to start work that will be waited on later. The choice depends on whether you need the result immediately and whether the operations are independent. For most I/O-bound concurrency, gather with coroutines or tasks is the standard pattern.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.