Skip to content

dispatch ¤

The Dispatch SDK for Python.

DispatchID module-attribute ¤

DispatchID: TypeAlias = str

Unique identifier in Dispatch.

It should be treated as an opaque value.

Client ¤

Client(
    api_key: Optional[str] = None,
    api_url: Optional[str] = None,
)

Client for the Dispatch API.

Parameters:

Name Type Description Default
api_key Optional[str]

Dispatch API key to use for authentication. Uses the value of the DISPATCH_API_KEY environment variable by default.

None
api_url Optional[str]

The URL of the Dispatch API to use. Uses the value of the DISPATCH_API_URL environment variable if set, otherwise defaults to the public Dispatch API (DEFAULT_API_URL).

None

Raises:

Type Description
ValueError

if the API key is missing.

batch ¤

batch() -> Batch

Returns a Batch instance that can be used to build a set of calls to dispatch.

dispatch ¤

dispatch(calls: Iterable[Call]) -> List[DispatchID]

Dispatch function calls.

Parameters:

Name Type Description Default
calls Iterable[Call]

Calls to dispatch.

required

Returns:

Type Description
List[DispatchID]

Identifiers for the function calls, in the same order as the inputs.

Registry ¤

Registry(
    endpoint: Optional[str] = None,
    api_key: Optional[str] = None,
    api_url: Optional[str] = None,
)

Registry of functions.

Parameters:

Name Type Description Default
endpoint Optional[str]

URL of the endpoint that the function is accessible from. Uses the value of the DISPATCH_ENDPOINT_URL environment variable by default.

None
api_key Optional[str]

Dispatch API key to use for authentication when dispatching calls to functions. Uses the value of the DISPATCH_API_KEY environment variable by default.

None
api_url Optional[str]

The URL of the Dispatch API to use when dispatching calls to functions. Uses the value of the DISPATCH_API_URL environment variable if set, otherwise defaults to the public Dispatch API (DEFAULT_API_URL).

None

Raises:

Type Description
ValueError

If any of the required arguments are missing.

function ¤

function(func)

Decorator that registers functions.

primitive_function ¤

primitive_function(
    primitive_func: PrimitiveFunctionType,
) -> PrimitiveFunction

Decorator that registers primitive functions.

set_client ¤

set_client(client: Client)

Set the Client instance used to dispatch calls to registered functions.

batch ¤

batch() -> Batch

Returns a Batch instance that can be used to build a set of calls to dispatch.

Reset ¤

Reset(
    func: Function[P, T], *args: args, **kwargs: kwargs
)

Bases: TailCall

The current coroutine is aborted and scheduling reset to be replaced with the call embedded in this exception.

Call dataclass ¤

Call(
    function: str,
    input: Optional[Any] = None,
    endpoint: Optional[str] = None,
    correlation_id: Optional[int] = None,
)

Instruction to call a function.

Though this class can be built manually, it is recommended to use the with_call method of a Function instead.

Error dataclass ¤

Error(
    status: Status,
    type: str,
    message: str,
    value: Optional[Exception] = None,
    traceback: Optional[bytes] = None,
)

Error when running a function.

This is not a Python exception, but potentially part of a CallResult or Output.

Parameters:

Name Type Description Default
status Status

categorization of the error.

required
type str

arbitrary string, used for humans.

required
message str

arbitrary message.

required
value Optional[Exception]

arbitrary exception from which the error is derived. Optional.

None

Raises:

Type Description
ValueError

Neither type or message was provided or status is invalid.

from_exception classmethod ¤

from_exception(
    ex: Exception, status: Optional[Status] = None
) -> Error

Create an Error from a Python exception, using its class qualified named as type.

The status tries to be inferred, but can be overridden. If it is not provided or cannot be inferred, it defaults to TEMPORARY_ERROR.

to_exception ¤

to_exception() -> Exception

Returns an equivalent exception.

Input ¤

Input(req: RunRequest)

The input to a primitive function.

Functions always take a single argument of type Input. When the function is run for the first time, it receives the input. When the function is a coroutine that's resuming after a yield point, it receives the results of the yield directive. Use the is_first_call and is_resume properties to differentiate between the two cases.

This class is intended to be used as read-only.

input_arguments ¤

input_arguments() -> Tuple[Tuple[Any, ...], Dict[str, Any]]

Returns positional and keyword arguments carried by the input.

Output dataclass ¤

Output(proto: RunResponse)

The output of a primitive function.

This class is meant to be instantiated and returned by authors of functions to indicate the follow up action they need to take. Use the various class methods create an instance of this class. For example Output.value() or Output.poll().

value classmethod ¤

value(
    value: Any, status: Optional[Status] = None
) -> Output

Terminally exit the function with the provided return value.

error classmethod ¤

error(error: Error) -> Output

Terminally exit the function with the provided error.

tail_call classmethod ¤

tail_call(
    tail_call: Call, status: Status = Status.OK
) -> Output

Terminally exit the function, and instruct the orchestrator to tail call the specified function.

exit classmethod ¤

exit(
    result: Optional[CallResult] = None,
    tail_call: Optional[Call] = None,
    status: Status = Status.OK,
) -> Output

Terminally exit the function.

poll classmethod ¤

poll(
    coroutine_state: Optional[bytes] = None,
    calls: Optional[List[Call]] = None,
    min_results: int = 1,
    max_results: int = 10,
    max_wait_seconds: Optional[int] = None,
) -> Output

Suspend the function with a set of Calls, instructing the orchestrator to resume the function with the provided state when call results are ready.

Status ¤

Bases: int, Enum

Enumeration of the possible values that can be used in the return status of functions.

all ¤

all(*awaitables: Awaitable[Any]) -> List[Any]

Concurrently run a set of coroutines, blocking until all coroutines return or any coroutine raises an error. If any coroutine fails with an uncaught exception, the exception will be re-raised here.

any ¤

any(*awaitables: Awaitable[Any]) -> List[Any]

Concurrently run a set of coroutines, blocking until any coroutine returns or all coroutines raises an error. If all coroutines fail with uncaught exceptions, the exception(s) will be re-raised here.

call ¤

call(call: Call) -> Any

Make an asynchronous function call and return its result. If the function call fails with an error, the error is raised.

gather ¤

gather(*awaitables: Awaitable[Any]) -> List[Any]

Alias for all.

race ¤

race(*awaitables: Awaitable[Any]) -> List[Any]

Concurrently run a set of coroutines, blocking until any coroutine returns or raises an error. If any coroutine fails with an uncaught exception, the exception will be re-raised here.

run ¤

run(
    init: Optional[Callable[P, None]] = None,
    *args: args,
    **kwargs: kwargs
)

Run the default dispatch server. The default server uses a function registry where functions tagged by the @dispatch.function decorator are registered.

This function is intended to be used with the dispatch CLI tool, which automatically configures environment variables to connect the local server to the Dispatch bridge API.

Parameters:

Name Type Description Default
init Optional[Callable[P, None]]

An initialization function called after binding the server address but before entering the event loop to handle requests.

None
args args

Positional arguments to pass to the entrypoint.

()
kwargs kwargs

Keyword arguments to pass to the entrypoint.

{}

Returns:

Type Description

The return value of the entrypoint function.

batch ¤

batch() -> Batch

Create a new batch object.