Skip to content

prefect.locking.protocol

LockManager

Bases: Protocol

Source code in src/prefect/locking/protocol.py
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
@runtime_checkable
class LockManager(Protocol):
    def acquire_lock(
        self,
        key: str,
        holder: str,
        acquire_timeout: Optional[float] = None,
        hold_timeout: Optional[float] = None,
    ) -> bool:
        """
        Acquire a lock for a transaction record with the given key. Will block other
        actors from updating this transaction record until the lock is
        released.

        Args:
            key: Unique identifier for the transaction record.
            holder: Unique identifier for the holder of the lock.
            acquire_timeout: Max number of seconds to wait for the record to become
                available if it is locked while attempting to acquire a lock. Pass 0
                to attempt to acquire a lock without waiting. Blocks indefinitely by
                default.
            hold_timeout: Max number of seconds to hold the lock for. Holds the lock
                indefinitely by default.

        Returns:
            bool: True if the lock was successfully acquired; False otherwise.
        """
        ...

    async def aacquire_lock(
        self,
        key: str,
        holder: str,
        acquire_timeout: Optional[float] = None,
        hold_timeout: Optional[float] = None,
    ) -> bool:
        """
        Acquire a lock for a transaction record with the given key. Will block other
        actors from updating this transaction record until the lock is
        released.

        Args:
            key: Unique identifier for the transaction record.
            holder: Unique identifier for the holder of the lock.
            acquire_timeout: Max number of seconds to wait for the record to become
                available if it is locked while attempting to acquire a lock. Pass 0
                to attempt to acquire a lock without waiting. Blocks indefinitely by
                default.
            hold_timeout: Max number of seconds to hold the lock for. Holds the lock
                indefinitely by default.

        Returns:
            bool: True if the lock was successfully acquired; False otherwise.
        """
        ...

    def release_lock(self, key: str, holder: str):
        """
        Releases the lock on the corresponding transaction record.

        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.
        """
        ...

    def is_locked(self, key: str) -> bool:
        """
        Simple check to see if the corresponding record is currently locked.

        Args:
            key: Unique identifier for the transaction record.

        Returns:
            True is the record is locked; False otherwise.
        """
        ...

    def is_lock_holder(self, key: str, holder: str) -> bool:
        """
        Check if the current holder is the lock holder for the transaction record.

        Args:
            key: Unique identifier for the transaction record.
            holder: Unique identifier for the holder of the lock.

        Returns:
            bool: True if the current holder is the lock holder; False otherwise.
        """
        ...

    def wait_for_lock(self, key: str, timeout: Optional[float] = None) -> bool:
        """
        Wait for the corresponding transaction record to become free.

        Args:
            key: Unique identifier for the transaction record.
            timeout: Maximum time to wait. None means to wait indefinitely.

        Returns:
            bool: True if the lock becomes free within the timeout; False
                otherwise.
        """
        ...

    async def await_for_lock(self, key: str, timeout: Optional[float] = None) -> bool:
        """
        Wait for the corresponding transaction record to become free.

        Args:
            key: Unique identifier for the transaction record.
            timeout: Maximum time to wait. None means to wait indefinitely.

        Returns:
            bool: True if the lock becomes free within the timeout; False
                otherwise.
        """
        ...

aacquire_lock(key, holder, acquire_timeout=None, hold_timeout=None) async

Acquire a lock for a transaction record with the given key. Will block other actors from updating this transaction record until the lock is released.

Parameters:

Name Type Description Default
key str

Unique identifier for the transaction record.

required
holder str

Unique identifier for the holder of the lock.

required
acquire_timeout Optional[float]

Max number of seconds to wait for the record to become available if it is locked while attempting to acquire a lock. Pass 0 to attempt to acquire a lock without waiting. Blocks indefinitely by default.

None
hold_timeout Optional[float]

Max number of seconds to hold the lock for. Holds the lock indefinitely by default.

None

Returns:

Name Type Description
bool bool

True if the lock was successfully acquired; False otherwise.

Source code in src/prefect/locking/protocol.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
async def aacquire_lock(
    self,
    key: str,
    holder: str,
    acquire_timeout: Optional[float] = None,
    hold_timeout: Optional[float] = None,
) -> bool:
    """
    Acquire a lock for a transaction record with the given key. Will block other
    actors from updating this transaction record until the lock is
    released.

    Args:
        key: Unique identifier for the transaction record.
        holder: Unique identifier for the holder of the lock.
        acquire_timeout: Max number of seconds to wait for the record to become
            available if it is locked while attempting to acquire a lock. Pass 0
            to attempt to acquire a lock without waiting. Blocks indefinitely by
            default.
        hold_timeout: Max number of seconds to hold the lock for. Holds the lock
            indefinitely by default.

    Returns:
        bool: True if the lock was successfully acquired; False otherwise.
    """
    ...

acquire_lock(key, holder, acquire_timeout=None, hold_timeout=None)

Acquire a lock for a transaction record with the given key. Will block other actors from updating this transaction record until the lock is released.

Parameters:

Name Type Description Default
key str

Unique identifier for the transaction record.

required
holder str

Unique identifier for the holder of the lock.

required
acquire_timeout Optional[float]

Max number of seconds to wait for the record to become available if it is locked while attempting to acquire a lock. Pass 0 to attempt to acquire a lock without waiting. Blocks indefinitely by default.

None
hold_timeout Optional[float]

Max number of seconds to hold the lock for. Holds the lock indefinitely by default.

None

Returns:

Name Type Description
bool bool

True if the lock was successfully acquired; False otherwise.

Source code in src/prefect/locking/protocol.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def acquire_lock(
    self,
    key: str,
    holder: str,
    acquire_timeout: Optional[float] = None,
    hold_timeout: Optional[float] = None,
) -> bool:
    """
    Acquire a lock for a transaction record with the given key. Will block other
    actors from updating this transaction record until the lock is
    released.

    Args:
        key: Unique identifier for the transaction record.
        holder: Unique identifier for the holder of the lock.
        acquire_timeout: Max number of seconds to wait for the record to become
            available if it is locked while attempting to acquire a lock. Pass 0
            to attempt to acquire a lock without waiting. Blocks indefinitely by
            default.
        hold_timeout: Max number of seconds to hold the lock for. Holds the lock
            indefinitely by default.

    Returns:
        bool: True if the lock was successfully acquired; False otherwise.
    """
    ...

await_for_lock(key, timeout=None) async

Wait for the corresponding transaction record to become free.

Parameters:

Name Type Description Default
key str

Unique identifier for the transaction record.

required
timeout Optional[float]

Maximum time to wait. None means to wait indefinitely.

None

Returns:

Name Type Description
bool bool

True if the lock becomes free within the timeout; False otherwise.

Source code in src/prefect/locking/protocol.py
110
111
112
113
114
115
116
117
118
119
120
121
122
async def await_for_lock(self, key: str, timeout: Optional[float] = None) -> bool:
    """
    Wait for the corresponding transaction record to become free.

    Args:
        key: Unique identifier for the transaction record.
        timeout: Maximum time to wait. None means to wait indefinitely.

    Returns:
        bool: True if the lock becomes free within the timeout; False
            otherwise.
    """
    ...

is_lock_holder(key, holder)

Check if the current holder is the lock holder for the transaction record.

Parameters:

Name Type Description Default
key str

Unique identifier for the transaction record.

required
holder str

Unique identifier for the holder of the lock.

required

Returns:

Name Type Description
bool bool

True if the current holder is the lock holder; False otherwise.

Source code in src/prefect/locking/protocol.py
83
84
85
86
87
88
89
90
91
92
93
94
def is_lock_holder(self, key: str, holder: str) -> bool:
    """
    Check if the current holder is the lock holder for the transaction record.

    Args:
        key: Unique identifier for the transaction record.
        holder: Unique identifier for the holder of the lock.

    Returns:
        bool: True if the current holder is the lock holder; False otherwise.
    """
    ...

is_locked(key)

Simple check to see if the corresponding record is currently locked.

Parameters:

Name Type Description Default
key str

Unique identifier for the transaction record.

required

Returns:

Type Description
bool

True is the record is locked; False otherwise.

Source code in src/prefect/locking/protocol.py
71
72
73
74
75
76
77
78
79
80
81
def is_locked(self, key: str) -> bool:
    """
    Simple check to see if the corresponding record is currently locked.

    Args:
        key: Unique identifier for the transaction record.

    Returns:
        True is the record is locked; False otherwise.
    """
    ...

release_lock(key, holder)

Releases the lock on the corresponding transaction record.

Parameters:

Name Type Description Default
key str

Unique identifier for the transaction record.

required
holder str

Unique identifier for the holder of the lock. Must match the holder provided when acquiring the lock.

required
Source code in src/prefect/locking/protocol.py
60
61
62
63
64
65
66
67
68
69
def release_lock(self, key: str, holder: str):
    """
    Releases the lock on the corresponding transaction record.

    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.
    """
    ...

wait_for_lock(key, timeout=None)

Wait for the corresponding transaction record to become free.

Parameters:

Name Type Description Default
key str

Unique identifier for the transaction record.

required
timeout Optional[float]

Maximum time to wait. None means to wait indefinitely.

None

Returns:

Name Type Description
bool bool

True if the lock becomes free within the timeout; False otherwise.

Source code in src/prefect/locking/protocol.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
def wait_for_lock(self, key: str, timeout: Optional[float] = None) -> bool:
    """
    Wait for the corresponding transaction record to become free.

    Args:
        key: Unique identifier for the transaction record.
        timeout: Maximum time to wait. None means to wait indefinitely.

    Returns:
        bool: True if the lock becomes free within the timeout; False
            otherwise.
    """
    ...