Meaning
Retrieves the return value of a completed Future or Task, re-raising any exception that occurred during execution. It addresses the need to explicitly extract an outcome when you cannot or choose not to use await, such as in callback-based or testing code. You reach for it when a Future is confirmed done and you need its value or want to surface its exception.
Primary Function
Concurrency result retrieval
Communicative Purpose
Obtain the outcome of an asynchronous operation.
Pattern
task.result(timeout=timeout)
Core Structure
...result(...)
Função primária
Concurrency result retrieval
Propósito comunicativo
Obtain the outcome of an asynchronous operation.
Situações de gatilho
Concurrent execution: retrieving a value from a completed Future after checking done(). Async testing: asserting the result of a finished Task in unit tests. Callback-based code: extracting the outcome inside an add_done_callback handler.
Contextos
concurrent.futures, asyncio, testing frameworks.
Padrão
task.result(timeout=timeout)
Estrutura central
...result(...)
Slots de substituição
task: Future or Task instance
Colocados típicos
- await
- add_done_callback
- done()
Substituições comuns
- future.result(timeout=seconds) for timed blocking waits on concurrent.futures.Future
- await task for automatic exception propagation in async code
Erros comuns
Calling .result() before the Future is done — misconception that it blocks like Java's Future.get() — raises InvalidStateError on asyncio.Task or blocks indefinitely on concurrent.futures.Future. Forgetting to catch exceptions stored in the Future — assuming .result() always returns a value — causes unhandled exceptions to propagate unexpectedly. Using .result() instead of await in async code — misunderstanding that .result() participates in the event loop — can break cooperative scheduling. Passing timeout=0 expecting an instant check — misconception that zero means non-blocking poll — may still raise TimeoutError if the result is not immediately available.
Similar / contraste
await task (returns result or raises exception) vs .result() which raises InvalidStateError if not done.
Interferências
Coming from Java: confusion with Future.get() which blocks; .result() raises if not done rather than blocking.
Família do chunk
- Future.result
- Task.result
- asyncio.wait_for
Nuance
Never call .result() on an asyncio.Task before confirming it is done — it raises InvalidStateError rather than blocking. On concurrent.futures.Future, .result() blocks the calling thread until the computation finishes, potentially stalling indefinitely without a timeout. The timeout parameter exists only on concurrent.futures.Future.result(), not on asyncio.Task.result().
Efeito pragmático
Allows explicit retrieval of result with precise error handling.
Dica de memória
Get the fruit of the future.
Nota
Use .result() only after confirming the Future/Task is done; prefer await for automatic exception propagation; handle InvalidStateError and use the timeout argument for timed waits.
Upgrade path
Use await task for automatic exception propagation.
Log in to save chunks.