Skip to main content

prefect_redis.locking

Classes

RedisLockManager

A lock manager that uses Redis as a backend. Attributes:
  • host: The host of the Redis server
  • port: The port the Redis server is running on
  • db: The database to write to and read from
  • username: The username to use when connecting to the Redis server
  • password: The password to use when connecting to the Redis server
  • ssl: Whether to use SSL when connecting to the Redis server
  • client: The Redis client used to communicate with the Redis server
  • async_client: The asynchronous Redis client used to communicate with the Redis server
Methods:

aacquire_lock

aacquire_lock(self, key: str, holder: str, acquire_timeout: Optional[float] = None, hold_timeout: Optional[float] = None) -> bool
Acquires a lock asynchronously. Args:
  • key: Unique identifier for the transaction record.
  • holder: Unique identifier for the holder of the lock. Must match the holder provided when acquiring the lock.
  • acquire_timeout: Maximum time to wait for the lock to be acquired.
  • hold_timeout: Maximum time to hold the lock.
Returns:
  • True if the lock was acquired, False otherwise.

acquire_lock

acquire_lock(self, key: str, holder: str, acquire_timeout: Optional[float] = None, hold_timeout: Optional[float] = None) -> bool
Acquires a lock synchronously. Args:
  • key: Unique identifier for the transaction record.
  • holder: Unique identifier for the holder of the lock.
  • acquire_timeout: Maximum time to wait for the lock to be acquired.
  • hold_timeout: Maximum time to hold the lock.
Returns:
  • True if the lock was acquired, False otherwise.

await_for_lock

await_for_lock(self, key: str, timeout: Optional[float] = None) -> bool

is_lock_holder

is_lock_holder(self, key: str, holder: str) -> bool

is_locked

is_locked(self, key: str) -> bool

release_lock

release_lock(self, key: str, holder: str) -> None
Releases the lock on the corresponding transaction record. Handles the case where a lock might have been released during a task retry If the lock doesn’t exist in Redis at all, this method will succeed even if the holder ID doesn’t match the original holder. Args:
  • key: Unique identifier for the transaction record.
  • holder: Unique identifier for the holder of the lock. Must match the holder provided when acquiring the lock.
Raises:
  • ValueError: If the lock is held by a different holder.

wait_for_lock

wait_for_lock(self, key: str, timeout: Optional[float] = None) -> bool