Skip to main content

step_resources

Locks a background DAG step holds on the contended resource it uses.

A step declares at most one background_resources name (bitfount.steps.registry.STEP_RESOURCES), and in a background run the executor holds the matching Prefect concurrency limit around each invocation:

  • gpu_inference: background_gpu_inference, sized by background_gpu_inference_concurrency.
  • ehr_connection: background_ehr_connections, sized by background_ehr_connection_concurrency.
  • project_publish: background_publish:<project_id>, of size 1.

Only background runs are gated: the flow body binds a StepResourceScope and the executor gates nothing without one, so an interactive run is never held behind a background one.

The wait for a resource is bounded by background_step_resource_wait, and when that runs out the step raises StepResourceBusyError, which stops the run at that wave without counting the wave as failed. The wait is not counted against the wave timeout or the hard cap while no other step of the run is running; beside a running step it is counted (bitfount.flows.dag.time_bounds.excluded_from_bounds).

A bound that expires during the wait, or any other cancellation of the run, ends the wait within about a second without taking the lock: the step raises RunCancelledError, as raise_if_cancelled does. Prefect releases a slot it grants to a waiter that has already left.

Module​

Functions​

hold_step_resource​

async def hold_step_resource(    lock: StepResourceLock, step_name: str,) ‑> AsyncIterator[HeldLock]:

Hold lock for the body, giving up the wait if the run is cancelled.

The wait is not counted against the run's time bounds while no other step of the run is running; beside a running step it is counted.

Arguments

  • lock: The lock to hold.
  • step_name: The step holding it, for the error and the log.

Raises

  • StepResourceBusyError: If the lock was not acquired within its wait.
  • RunCancelledError: If the run was cancelled before the lock was acquired.

step_resource_lock​

def step_resource_lock(    resources: frozenset[str],) ‑> StepResourceLock | None:

Return the lock a step declaring resources holds here, if any.

Arguments

  • resources: The step's background_resources.

Returns None outside a step_resource_scope (an interactive run) or for a step that declares none.

step_resource_scope​

def step_resource_scope(    scope: StepResourceScope,) ‑> Iterator[StepResourceScope]:

Gate the steps run in this block on their declared resources.

Arguments

  • scope: The run's project and wait.

Global variables​

  • RESOURCE_BUSY - stop_reason of a run that stopped because a step's resource stayed busy.

Classes​

StepResourceBusyError​

class StepResourceBusyError(step_name: str, lock_name: str, waited_seconds: float):

A step's resource stayed held by others for the whole allowed wait.

Arguments

  • step_name: The step that waited.
  • lock_name: The concurrency limit it waited for.
  • waited_seconds: How long it waited.

Attributes

  • step_name: The step that waited.
  • lock_name: The concurrency limit it waited for.
  • waited_seconds: How long it waited.

Build the error.

StepResourceLock​

class StepResourceLock(resource: str, name: str, limit: int, wait_seconds: float | None):

The concurrency limit one step invocation holds.

Attributes

  • resource: The declared resource name.
  • name: The Prefect concurrency limit.
  • limit: Its size.
  • wait_seconds: How long to wait for it; None for no bound.

Variables​

  • limit : int
  • name : str
  • resource : str
  • wait_seconds : float | None

StepResourceScope​

class StepResourceScope(project_id: str, wait_seconds: float | None):

What a background run's steps need to know to take their resource locks.

Attributes

  • project_id: The run's project, naming its publish lock.
  • wait_seconds: How long a step waits for its resource; None for no bound.

Variables​

  • project_id : str
  • wait_seconds : float | None