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.
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.
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.
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.
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.
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.
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
gatherwhen 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.gatherto collect results from several coroutines in input order. - Use
asyncio.as_completedwhen early result handling is more important than order. - Design the result shape intentionally so async code stays readable and safe.
Related reading
- How to handle promise when calling async JS method from C using Emscripten
- How to handle race conditions in distributed programming?
- How to handle results for each Future of a List and when all futures are completed in vert.x?
- How to handle Task.Run Exception
- How to handle SQLAlchemy Connections in ProcessPool?
- How to handle unseen categorical values in test data set using python?
- How to handle timeout in Async Socket?
- How to illustrate multiple threads in sequence diagram?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseInterview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.