Resources & Locking
Worker resource system, type registration, and distributed locking.
Worker resource system, type registration, and distributed locking.
Methods for the worker resource system and distributed locking.
@queue.worker_resource()@queue.worker_resource(
name: str,
depends_on: list[str] | None = None,
teardown: Callable | None = None,
health_check: Callable | None = None,
health_check_interval: float = 0.0,
max_recreation_attempts: int = 3,
scope: ResourceScope | str = ResourceScope.WORKER,
pool_size: int | None = None,
pool_min: int = 0,
acquire_timeout: float = 10.0,
max_lifetime: float = 3600.0,
idle_timeout: float = 300.0,
reloadable: bool = False,
frozen: bool = False,
) -> CallableDecorator to register a resource factory initialized at worker startup.
| Parameter | Type | Default | Description |
|---|---|---|---|
name | str | — | Resource name used in inject=["name"] or Inject["name"]. |
depends_on | list[str] | None | None | Names of resources this one depends on. |
teardown | Callable | None | None | Called with the resource instance on shutdown. |
health_check | Callable | None | None | Called periodically; returns truthy if healthy. |
health_check_interval | float | 0.0 | Seconds between health checks (0 = disabled). |
max_recreation_attempts | int | 3 | Max times to recreate on health failure. |
scope | ResourceScope | str | WORKER | Lifetime: WORKER, THREAD, TASK (fresh per task), or POOLED (pool checkout). |
pool_size | int | None | None | Pool capacity; POOLED scope only (defaults to 4). |
pool_min | int | 0 | Pre-warmed instances; POOLED scope only. |
acquire_timeout | float | 10.0 | Max seconds to wait for a pool instance. |
max_lifetime | float | 3600.0 | Max seconds a pooled instance can live. |
idle_timeout | float | 300.0 | Max idle seconds before pool eviction. |
reloadable | bool | False | Allow hot-reload via SIGHUP. |
frozen | bool | False | Wrap in a read-only proxy that blocks attribute writes. |
queue.register_resource()queue.register_resource(definition: ResourceDefinition) -> NoneProgrammatically register a ResourceDefinition. Equivalent to
@queue.worker_resource() but accepts a pre-built definition object.
queue.load_resources()queue.load_resources(toml_path: str) -> NoneLoad resource definitions from a TOML file. Must be called before run_worker().
See TOML configuration.
queue.health_check()queue.health_check(name: str) -> boolRun a resource's health check immediately. Returns True if healthy, False
otherwise or if the runtime is not initialized.
queue.reload_resources()queue.reload_resources(names: list[str] | None = None) -> dict[str, bool]Hot-reload reloadable worker resources — the programmatic equivalent of
SIGHUP. Tears down and re-creates each target in dependency order. None
reloads every resource registered with reloadable=True. Returns a
name-to-success mapping; empty when no worker resource runtime is active in
this process. Async twin: areload_resources().
queue.resource_status()queue.resource_status() -> list[dict]Return per-resource status. Each entry contains: name, scope, health,
init_duration_ms, recreations, depends_on. Pooled entries also
include pool stats.
queue.register_type()queue.register_type(
python_type: type,
strategy: str,
*,
resource: str | None = None,
message: str | None = None,
converter: Callable | None = None,
type_key: str | None = None,
proxy_handler: str | None = None,
) -> NoneRegister a custom type with the interception system. Requires
interception="strict" or "lenient".
| Parameter | Type | Description |
|---|---|---|
python_type | type | The type to register. |
strategy | str | "pass", "convert", "redirect", "reject", or "proxy". |
resource | str | None | Resource name for "redirect" strategy. |
message | str | None | Rejection reason for "reject" strategy. |
converter | Callable | None | Converter callable for "convert" strategy. |
type_key | str | None | Dispatch key for the converter reconstructor. |
proxy_handler | str | None | Handler name for "proxy" strategy. |
queue.analyze_arguments()queue.analyze_arguments(args: tuple = (), kwargs: dict | None = None) -> InterceptionReportDry-run the argument interceptor without enqueuing — the report describes the
strategy chosen for each argument. Empty when the queue was created with
interception="off". Per-task variant: my_task.analyze(...).
queue.interception_stats()queue.interception_stats() -> dictReturn interception metrics: total call count, per-strategy counts, average duration in ms, max depth reached. Returns an empty dict if interception is disabled.
queue.proxy_stats()queue.proxy_stats() -> list[dict]Return per-handler proxy metrics: handler name, deconstruction count, reconstruction count, error count, average reconstruction time in ms.
queue.lock()queue.lock(
name: str,
ttl: int = 30,
auto_extend: bool = True,
owner_id: str | None = None,
timeout: float | None = None,
retry_interval: float = 0.1,
) -> contextlib.AbstractContextManagerAcquire a distributed lock. Use as a context manager:
with queue.lock("my-resource", ttl=60):
# exclusive section
...Raises LockNotAcquired if acquisition fails (when timeout is None or expires).
queue.alock()queue.alock(
name: str,
ttl: float = 30.0,
auto_extend: bool = True,
owner_id: str | None = None,
timeout: float | None = None,
retry_interval: float = 0.1,
) -> AsyncDistributedLockAsync context manager version of lock(). Returns an AsyncDistributedLock
directly — use async with, not await:
async with queue.alock("my-resource"):
...