Skip to content

function ¤

PrimitiveFunctionType module-attribute ¤

PrimitiveFunctionType: TypeAlias = Callable[
    [Input], Awaitable[Output]
]

A primitive function is a function that accepts a dispatch.proto.Input and unconditionally returns a dispatch.proto.Output. It must not raise exceptions.

DEFAULT_REGISTRY module-attribute ¤

DEFAULT_REGISTRY: Optional[Registry] = None

The default registry for dispatch functions, used by dispatch applications when no custom registry is provided.

In most cases, applications do not need to create a custom registry, so this one would be used by default.

The default registry use DISPATCH_* environment variables for configuration, or is uninitialized if they are not set.

AsyncFunction ¤

AsyncFunction(
    registry: Registry,
    name: str,
    primitive_func: PrimitiveFunctionType,
)

Bases: PrimitiveFunction, Generic[P, T]

Callable wrapper around a function meant to be used throughout the Dispatch Python SDK.

__call__ ¤

__call__(
    *args: args, **kwargs: kwargs
) -> Coroutine[Any, Any, T]

Call the function asynchronously (through Dispatch), and return a coroutine that can be awaited to retrieve the call result.

dispatch async ¤

dispatch(*args: args, **kwargs: kwargs) -> DispatchID

Dispatch an asynchronous call to the function without waiting for a result.

The Registry this function was registered with must be initialized with a Client / api_key for this call facility to be available.

Parameters:

Name Type Description Default
*args args

Positional arguments for the function.

()
**kwargs kwargs

Keyword arguments for the function.

{}

Returns:

Name Type Description
DispatchID DispatchID

ID of the dispatched call.

build_call ¤

build_call(*args: args, **kwargs: kwargs) -> Call

Create a Call for this function with the provided input. Useful to generate calls when using the Client.

Parameters:

Name Type Description Default
*args args

Positional arguments for the function.

()
**kwargs kwargs

Keyword arguments for the function.

{}

Returns:

Name Type Description
Call Call

can be passed to Client.dispatch.

BlockingFunction ¤

BlockingFunction(func: AsyncFunction[P, T])

Bases: Generic[P, T]

BlockingFunction is like Function but exposes a blocking API instead of functions that use asyncio.

Applications typically don't create instances of BlockingFunction directly, and instead use decorators from packages that provide integrations with Python frameworks.

Reset ¤

Reset(
    func: Union[
        AsyncFunction[P, T], BlockingFunction[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.

Registry ¤

Registry(
    name: str,
    client: Optional[Client] = None,
    endpoint: Optional[str] = None,
)

Registry of functions.

Parameters:

Name Type Description Default
name str

A unique name for the registry.

required
endpoint Optional[str]

URL of the endpoint that the function is accessible from.

None
client Optional[Client]

Client instance to use for dispatching calls to registered functions. Defaults to creating a new client instance.

None

Raises:

Type Description
ValueError

If any of the required arguments are missing.

close ¤

close()

Closes the registry, removing it and all its functions from the dispatch application.

function ¤

function(func)

Decorator that registers functions.

primitive_function ¤

primitive_function(
    primitive_func: PrimitiveFunctionType,
) -> PrimitiveFunction

Decorator that registers primitive functions.

batch ¤

batch() -> Batch

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

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.

dispatch async ¤

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.

Batch ¤

Batch(client: Client)

A batch of calls to dispatch.

add ¤

add(
    func: AsyncFunction[P, T],
    *args: args,
    **kwargs: kwargs
) -> Batch

Add a call to the specified function to the batch.

add_call ¤

add_call(call: Call) -> Batch

Add a Call to the batch.

clear ¤

clear()

Reset the batch.

dispatch async ¤

dispatch() -> List[DispatchID]

Dispatch dispatches the calls asynchronously.

The batch is reset when the calls are dispatched successfully.

Returns:

Type Description
List[DispatchID]

Identifiers for the function calls, in the same order they

List[DispatchID]

were added.

default_registry ¤

default_registry() -> Registry

Returns the default registry for dispatch functions.

The function initializes the default registry if it has not been initialized yet, using the DISPATCH_* environment variables for configuration.

Returns:

Name Type Description
Registry Registry

The default registry.

Raises:

Type Description
ValueError

If the DISPATCH_API_KEY or DISPATCH_ENDPOINT_URL environment variables are missing.