pytrio.types.APIFuture
class APIFuture(Generic[T]):
def result(self, timeout: float | None = None) -> T
def done(self) -> bool
def exception(self, timeout: float | None = None) -> BaseException | None
def cancel(self) -> bool
def map(self, transform: Callable[[T], U]) -> APIFuture[U]
def __await__(self) -> Generator[object, None, T]APIFuture represents a thread-safe pending result submitted to the PyTRIO runtime. It is returned by TrainingClient sync/async operations, synchronous SamplingClient.sample() / compute_logprobs(), and RestClient.get_sampler().
It supports both blocking .result() calls and await:
# Synchronous code
future = training_client.forward_backward(data=data)
output = future.result(timeout=120)
# Asynchronous code
future = await training_client.forward_backward_async(data=data)
output = await futureMethods
result
def result(self, timeout: float | None = None) -> TBlock until the operation finishes and return its result, or re-raise any failure exception.
| Parameter | Type | Default | Description |
|---|---|---|---|
timeout | float | None | None | Maximum seconds to wait; None waits indefinitely. Raises TimeoutError on expiry |
done
def done(self) -> boolReturn True if the operation has reached a terminal state (succeeded, failed, or cancelled).
exception
def exception(self, timeout: float | None = None) -> BaseException | NoneWait for completion and return the recorded exception, or None if the operation succeeded. Raises TimeoutError if timeout expires.
cancel
def cancel(self) -> boolRequest cancellation of an operation that has not started yet. Returns True if cancellation took effect.
map
def map(self, transform: Callable[[T], U]) -> APIFuture[U]Create a derived APIFuture that applies synchronous transform to the successful result while propagating failures and cancellation.
loss_future = training_client.forward_backward(data).map(
lambda out: out.metrics["loss_mean"]
)
mean_loss = loss_future.result()completed
@staticmethod
def completed(value: V) -> APIFuture[V]Create an already-completed APIFuture holding value.
Differences from RestClient futures
TrainingClienttraining/optimizer/save methods, synchronousSamplingClient.sample()/compute_logprobs(), andRestClient.get_sampler()returnAPIFuture(supporting bothawaitand.result()).- Other synchronous
RestClientmethods return standardconcurrent.futures.Future.RestClient.*_async()andSamplingClient.*_async()return the final domain result directly when awaited. See the Async guide.