Skip to content

API

pytrio.types.APIFuture

python
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:

python
# 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 future

Methods ​

result ​

python
def result(self, timeout: float | None = None) -> T

Block until the operation finishes and return its result, or re-raise any failure exception.

ParameterTypeDefaultDescription
timeoutfloat | NoneNoneMaximum seconds to wait; None waits indefinitely. Raises TimeoutError on expiry

done ​

python
def done(self) -> bool

Return True if the operation has reached a terminal state (succeeded, failed, or cancelled).

exception ​

python
def exception(self, timeout: float | None = None) -> BaseException | None

Wait for completion and return the recorded exception, or None if the operation succeeded. Raises TimeoutError if timeout expires.

cancel ​

python
def cancel(self) -> bool

Request cancellation of an operation that has not started yet. Returns True if cancellation took effect.

map ​

python
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.

python
loss_future = training_client.forward_backward(data).map(
    lambda out: out.metrics["loss_mean"]
)
mean_loss = loss_future.result()

completed ​

python
@staticmethod
def completed(value: V) -> APIFuture[V]

Create an already-completed APIFuture holding value.

Differences from RestClient futures ​

  • TrainingClient training/optimizer/save methods, synchronous SamplingClient.sample() / compute_logprobs(), and RestClient.get_sampler() return APIFuture (supporting both await and .result()).
  • Other synchronous RestClient methods return standard concurrent.futures.Future. RestClient.*_async() and SamplingClient.*_async() return the final domain result directly when awaited. See the Async guide.