dispatchrun / dispatch-py

Python package to develop applications with Dispatch.
https://pypi.org/project/dispatch-py/
Apache License 2.0
54 stars 3 forks source link

Batch submit #127

Closed chriso closed 6 months ago

chriso commented 6 months ago

Users can dispatch a set of calls using the low-level Client:

class Client:
  def dispatch(calls: Iterable[Call]) -> list[DispatchID])

We construct a Client instance when users use the FastAPI integration, but we don't currently export the client instance.

We provide sugar for dispatching a single call like so:

@dispatch.function
def foo(bar: int): ...

foo.dispatch(1)  # sugar for: client.dispatch([foo.build_call(1)])

We don't provide any sugar for dispatching more than one call to a single function, or to many functions in the same batch.

This PR fixes the issue by providing a way to build and dispatch a batch of calls:

batch = dispatch.batch()

batch.add(func1, *args, **kwargs)
batch.add(func2, *args, **kwargs)
batch.add(func3, *args, **kwargs)

batch.dispatch()

Care has been taken to ensure that mypy will check that the types of args/kwargs match the specified function.