dispatchrun/dispatch-py

Add additional concurrency primitives

Closed this issue · 0 comments

The SDK provides a gather function that concurrently runs zero or more awaitables, and blocks until all awaitables return successfully or any awaitable raises.

async def gather(*awaitables: Awaitable[Any]) -> list[Any]: ...

The name was chosen to match the asyncio function that has the same behavior. See https://docs.python.org/3/library/asyncio-task.html#running-tasks-concurrently.

Other languages provide a more complete set of concurrency/composition primitives. For example, JavaScript provides Promise.all, Promise.any and Promise.race:

  • all: returns when all awaitables return successfully, or any awaitable raises (like gather)
  • any: returns when any awaitable returns successfully, or all awaitables raise
  • race: returns when any awaitable returns or raises

We should consider adding any and race, and aliasing gather as all.