Python
Asynchronous Programming
Coroutines
Concurrency
Python Tips

How to handle multiple results from a coroutine function?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

A coroutine in Python can return multiple results just like a normal function. The real design question is not whether multiple results are allowed, but how they should be represented so the calling code stays clear. In asyncio code, the most common options are returning a tuple or dataclass from one coroutine, or collecting many coroutine results with asyncio.gather or asyncio.as_completed.

Return Multiple Values from One Coroutine

A coroutine can return a tuple exactly like a synchronous function.

python
1import asyncio
2
3async def fetch_user_summary(user_id: int) -> tuple[str, int]:
4    await asyncio.sleep(0.1)
5    return "ada", 42
6
7async def main() -> None:
8    name, score = await fetch_user_summary(1)
9    print(name, score)
10
11asyncio.run(main())

This is the simplest answer when one asynchronous operation naturally produces several related outputs.

If the result has meaning beyond two or three values, a dataclass is usually clearer than a long tuple.

python
1import asyncio
2from dataclasses import dataclass
3
4@dataclass
5class UserSummary:
6    name: str
7    score: int
8    active: bool
9
10async def fetch_user_summary(user_id: int) -> UserSummary:
11    await asyncio.sleep(0.1)
12    return UserSummary(name="ada", score=42, active=True)

That reduces positional bugs and makes the call site more readable.

Collect Results from Multiple Coroutines with gather

If the real problem is that you have several coroutines and want all of their results, use asyncio.gather.

python
1import asyncio
2
3async def fetch_value(n: int) -> int:
4    await asyncio.sleep(0.1)
5    return n * 10
6
7async def main() -> None:
8    results = await asyncio.gather(
9        fetch_value(1),
10        fetch_value(2),
11        fetch_value(3),
12    )
13    print(results)
14
15asyncio.run(main())

gather preserves the order of the input coroutines, even if they finish at different times. That makes it a strong default when order matters.

Use as_completed When You Want Results Early

Sometimes the right approach is to process results as soon as each coroutine finishes instead of waiting for all of them.

python
1import asyncio
2
3async def fetch_value(n: int) -> int:
4    await asyncio.sleep(0.2 - n * 0.05)
5    return n * 10
6
7async def main() -> None:
8    tasks = [fetch_value(1), fetch_value(2), fetch_value(3)]
9
10    for future in asyncio.as_completed(tasks):
11        result = await future
12        print("finished:", result)
13
14asyncio.run(main())

This is useful when each result can be handled independently and responsiveness matters more than preserving original ordering.

Handle Exceptions Deliberately

Multiple results also mean multiple failure paths. By default, one exception in gather can fail the whole await. If partial success is acceptable, use return_exceptions=True and handle each element explicitly.

python
1results = await asyncio.gather(
2    fetch_value(1),
3    fetch_value(2),
4    return_exceptions=True,
5)

Do not treat this as a convenience flag only. It changes the shape of the result list, so the caller must inspect each item carefully.

Keep the Result Shape Stable

When a coroutine becomes part of a public API in your codebase, keep its return shape stable across success paths. Switching between tuples, dictionaries, and custom objects depending on context makes async call sites harder to reason about than the asynchronous part itself. Pick one result type and document it.

Common Pitfalls

  • Thinking coroutines need a special syntax to return more than one value.
  • Returning a long tuple when a named result object would be clearer.
  • Using gather when results should be processed as soon as they complete.
  • Forgetting that exceptions from one coroutine can affect the whole group await.
  • Writing asynchronous code that returns many loosely related values instead of defining a cleaner result type.

Summary

  • A coroutine can return multiple values the same way a normal Python function can.
  • Use tuples for small, obvious result sets and dataclasses for richer structured output.
  • Use asyncio.gather to collect results from several coroutines in input order.
  • Use asyncio.as_completed when early result handling is more important than order.
  • Design the result shape intentionally so async code stays readable and safe.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions