keyed_concurrency
Prefect global concurrency limits keyed by name, created on first use.
Limits named for one datasource, lineage or project (metadata:<task_hash>,
background_lineage:<project_id>:<task_hash>, background_publish:<project_id>)
cannot be created at bootstrap, because the names are not known then. Prefect's
concurrency() on a limit that does not exist silently admits every caller, so
keyed_concurrency creates the limit just before acquiring it and acquires with
strict=True, which raises instead.
Every slot is held on a lease that a dead process stops renewing, so a crash
frees it once the lease runs out and the server repossessor revokes it: 300 s by
default, 1200 s for a background DAG run's lineage lock.
A renewal that fails while the holder is alive does not stop the holder
(raise_on_lease_renewal_failure=False): the holder keeps running while the
server may hand its slot to the next waiter. Prefect gives no hook for that
failure, only a log line, so a handler on Prefect's loggers turns that line into
a lock_lease_lost event for the lock whose renewal thread logged it.
Module
Functions
background_lineage_limit_name
def background_lineage_limit_name(project_id: str, task_hash: str) ‑> str:Return the name of the limit admitting one run of a lineage at a time.
Arguments
project_id: The lineage's project.task_hash: The lineage's(pod, datasource)task hash.
Returns
background_lineage:<project_id>:<task_hash>.
background_publish_limit_name
def background_publish_limit_name(project_id: str) ‑> str:Return the name of the limit serialising one project's publishes.
Arguments
project_id: The project.
Returns
background_publish:<project_id>.
ensure_concurrency_limit
async def ensure_concurrency_limit(name: str, limit: int) ‑> None:Create the limit name at limit, or resize it, unless already done.
Tolerates another process creating it at the same moment.
Arguments
name: The concurrency limit.limit: Its size.
ensure_concurrency_limit_sync
def ensure_concurrency_limit_sync(name: str, limit: int) ‑> None:Synchronous ensure_concurrency_limit.
Arguments
name: The concurrency limit.limit: Its size.
keyed_concurrency
async def keyed_concurrency( name: str, limit: int, *, timeout_seconds: float | None, occupy: int = 1, holder_run_id: str | None = None, lease_duration_seconds: float = 300.0,) ‑> AsyncIterator[HeldLock]:Hold occupy slots of the limit name for the body of the block.
Creates the limit at limit if it does not exist. A limit deleted after this process ensured it (the start-up sweep deletes idle ones) is created again and the acquisition retried once.
Arguments
name: The concurrency limit.limit: Its size, applied when it is created or ensured.timeout_seconds: How long to wait for the slots;Nonewaits indefinitely.occupy: Slots to hold.holder_run_id: The run holding the slots, for telemetry; defaults to the current flow run.lease_duration_seconds: The lease the slots are held on, and so how long a dead holder keeps them.
Raises
AcquireConcurrencySlotTimeoutError: If the slots are not acquired within timeout_seconds.ConcurrencySlotAcquisitionError: If the limit cannot be acquired even after creating it again.
keyed_concurrency_sync
def keyed_concurrency_sync( name: str, limit: int, *, timeout_seconds: float | None, occupy: int = 1, holder_run_id: str | None = None, lease_duration_seconds: float = 300.0,) ‑> Iterator[HeldLock]:Synchronous keyed_concurrency, for flow bodies that are not async.
Arguments
name: The concurrency limit.limit: Its size, applied when it is created or ensured.timeout_seconds: How long to wait for the slots;Nonewaits indefinitely.occupy: Slots to hold.holder_run_id: The run holding the slots, for telemetry; defaults to the current flow run.lease_duration_seconds: The lease the slots are held on, and so how long a dead holder keeps them.
Raises
AcquireConcurrencySlotTimeoutError: If the slots are not acquired within timeout_seconds.ConcurrencySlotAcquisitionError: If the limit cannot be acquired even after creating it again.
machine_slots_in_use
async def machine_slots_in_use( name: str = 'background_dag_max_concurrent_runs',) ‑> int | None:Return how many of name's slots are taken, without taking one.
The same counter keyed_concurrency acquires from, read rather than
incremented, for a caller that wants to know whether waiting could be worth
it before it waits. A background run polls this before it takes its
lineage: on a full machine it has nothing ahead of it but the slot it cannot
get, so the waiting costs nothing there — a run that never gets a slot ends
without holding the lineage that its own replay would then have to wait for.
Read only: it holds no slot and reserves nothing, so a race for the last
slot is still decided by the acquire. A limit this pod has not created yet,
and any failure to read it, answers None — which means "ask the acquire"
rather than "free" or "full".
Arguments
name: The concurrency limit to read.
Returns
The slots currently held, or None when that cannot be read.
metadata_limit_name
def metadata_limit_name(task_hash: str) ‑> str:Return the name of the limit serialising one datasource's metadata runs.
Arguments
task_hash: The(pod, datasource)task hash.
Returns
metadata:<task_hash>.
Classes
HeldLock
class HeldLock( name: str, limit: int, holder_run_id: str | None = None, lock_wait_seconds: float = 0.0, lease_lost: bool = False, lease_duration_seconds: float = 300.0,):A slot held on a keyed limit, for the holder to read while it holds it.
Attributes
name: The concurrency limit.limit: The limit's size, as ensured before acquiring.holder_run_id: The flow run holding the slot, when known.lock_wait_seconds: How long acquiring the slot took, including creating the limit.lease_lost: Whether Prefect has reported failing to renew the lease.lease_duration_seconds: The lease the slot is held on.
Variables
-
holder_run_id : str | None
-
lease_duration_seconds : float
-
lease_lost : bool
-
limit : int
-
lock_wait_seconds : float
-
name : str