Reference / Locks API for Python
Locks API for Python
Every public class, function and type of the Locks engine in tinystore, generated from its source. The Locks guide explains how to use them, and the Bun and Node, Go and Rust pages list the same API.
Lease
@dataclass(frozen=True, slots=True)
class Lease:
fence: int # grows with every lease the store gives, 128 bits: hand it to whatever must refuse a late holder
waited: timedelta # how long the hold waited for the key, from its call to the grant, the round trip includedA holding of a lock's key.
Permit
@dataclass(frozen=True, slots=True)
class Permit:
waited: timedelta # how long the hold waited for its place, from its call to the grant, the round trip includedA holding of a semaphore's key: one of its limit.
Permit.healthy
def healthy() -> None: ...Says the guarded service is healthy, so that the breaker counts no failure though the block then raises.
Breaker
@dataclass(frozen=True, slots=True)
class Breaker:
failures: int
pause: DurationA semaphore's breaker: failures failed holds of a key in a row put it down for pause.
A hold failed when its block raised, unless its permit said
healthy(), or when it ran past its timeout or max_hold; a success
resets the count. While the key is down every hold is UnavailableError at
once. Once the pause is over, one hold goes through: its success puts the
key up, its failure pauses it again. The store's process keeps it in
memory, so a restart of that process puts every key up.
KeyState
@dataclass(frozen=True, slots=True)
class KeyState:
up: bool # false from the failure that put the key down until a hold after the pause succeeds; true without a breaker
down_since: datetime | None
last_failure: datetime | None # the last failed hold's end, kept until a success after it
waiting: int
holding: int
failures: int # the failed holds in a row the breaker counts now, toward its ``failures``: reset by a success, and by a pause with no failure; while the key is down, those that put it down; 0 without a breakerHow a semaphore's key stands now: await semaphore.state(key).
Holding
class Holding[V]:
...A hold of a key for the block of an async with, which gives what the block holds it by.
A lease lost while the block runs cancels it at its next await, as
asyncio.timeout does, and the block ends LostError; one lost after its
last await ends it LostError too, since another holder may have run beside it.
Lock
class Lock:
name: strKeys held one holder at a time: store.lock(name).
Lock.hold
def hold(key: Key, *, wait: Duration = _WAIT) -> Holding[Lease]: ...Waits for key in line and holds it through the block: async with invoices.hold(id) as lease:.
A key still held after wait is InUseError; a lease lost while the block ran is LostError, though the block succeeded.
Lock.try_hold
def try_hold(key: Key) -> Holding[Lease | None]: ...Holds key through the block if nobody holds it; the lease is None when somebody does.
Semaphore
class Semaphore:
name: str
limit: intKeys held by up to their limit at a time: store.semaphore(name, limit=3).
Semaphore.state
async def state(key: Key) -> KeyState: ...How key stands now: up or down and since when, its last failed hold, how many wait and hold it:
state = await cabinet.state("requests") if not state.up: print(f"the cabinet is down since {state.down_since}")
Semaphore.hold
def hold(
key: Key,
*,
wait: Duration = _WAIT,
priority: Priority = 'urgent',
timeout: Duration | None = None,
) -> Holding[Permit]: ...Waits for one of key's places in line and holds it through the block.
In the background it waits behind every urgent hold and takes no more
than the semaphore's background places; past timeout from the
grant the place goes to the next in line and the block ends LostError:
async with cabinet.hold("requests", priority="background", timeout="15s"):
await sync()Semaphore.try_hold
def try_hold(
key: Key,
*,
priority: Priority = 'urgent',
timeout: Duration | None = None,
) -> Holding[Permit | None]: ...Holds one of key's places through the block if one is free; None when none is.