Skip to content

Styx Futures

StyxResponse(request_id, in_timestamp=-1, out_timestamp=-1, response=None) dataclass

Bases: object

Encapsulates a response from a Styx function invocation.

Attributes:

Name Type Description
request_id bytes

The unique identifier of the request.

in_timestamp int

The time the request was sent, in ms.

out_timestamp int

The time the response was received, in ms.

response any

The actual response value returned.

styx_latency_ms property

float: The measured latency of the function in milliseconds.

BaseFuture(request_id, timeout_sec, is_async=False)

Bases: ABC

Abstract base class for representing asynchronous or blocking Styx results.

Tracks a future-style response using either threading.Event or asyncio.Event based on the execution context.

Attributes:

Name Type Description
_timeout int

Timeout duration in seconds.

_val StyxResponse

Encapsulated response and timestamps.

_condition Event | Event

Event object (threading or asyncio) to signal completion.

Initializes a future for a given request.

Parameters:

Name Type Description Default
request_id bytes

Unique ID for the request.

required
timeout_sec int

Timeout in seconds.

required
is_async bool

Whether to use asyncio. Defaults to False.

False

request_id property

bytes: The request ID associated with this future.

done()

Checks if the future has been fulfilled.

Returns:

Name Type Description
bool bool

True if the result is available, False otherwise.

set(response_val, out_timestamp)

Fulfills the future with a response.

Parameters:

Name Type Description Default
response_val any

The value to store as the response.

required
out_timestamp int

The time the response was received.

required

Raises:

Type Description
FutureAlreadySet

If the future was already fulfilled.

set_in_timestamp(in_timestamp)

Sets the time the request was issued.

Parameters:

Name Type Description Default
in_timestamp int

Timestamp in milliseconds.

required

StyxFuture(request_id, timeout_sec=30)

Bases: BaseFuture

Blocking future for retrieving Styx function results synchronously.

Initializes a synchronous future.

Parameters:

Name Type Description Default
request_id bytes

Unique request ID.

required
timeout_sec int

Timeout in seconds. Defaults to 30.

30

get()

Blocks until the result is available or times out.

Returns:

Type Description
StyxResponse | None

StyxResponse | None: The received response.

Raises:

Type Description
FutureTimedOut

If the future is not fulfilled within the timeout.

StyxAsyncFuture(request_id, timeout_sec=30)

Bases: BaseFuture

Async future for retrieving Styx function results with asyncio.

Initializes an asynchronous future.

Parameters:

Name Type Description Default
request_id bytes

Unique request ID.

required
timeout_sec int

Timeout in seconds. Defaults to 30.

30

get() async

Awaits the result with a timeout.

Returns:

Type Description
StyxResponse | None

StyxResponse | None: The received response.

Raises:

Type Description
FutureTimedOut

If the future is not fulfilled within the timeout.