key_lock
key_lock
#
KeyLock — an exclusive flock on one key, held from acquisition until released.
One lock file per key under a directory serializes holders across threads and
processes; the kernel drops the lock on any kind of death, so a crashed
holder's key is simply free. The file's content is the holder's to write
(claim) and anyone's to read (claimant): a note saying who holds it.
KeyLock
#
A handle on the lock of key: acquire or try_acquire it, release it.
Source code in capturegraph-lib/capturegraph/recipes/scratch/store/key_lock.py
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
held
property
#
Whether this handle holds the lock.
__del__()
#
__enter__()
#
__exit__(*_)
#
__init__(directory, key)
#
Open the lock file of key under directory without acquiring it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
Path
|
Where the lock files of this kind of key live. |
required |
key
|
str
|
The key to hold. |
required |
Source code in capturegraph-lib/capturegraph/recipes/scratch/store/key_lock.py
acquire(timeout_seconds)
#
Hold the lock, waiting for another holder up to timeout_seconds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout_seconds
|
float | None
|
How long to wait; |
required |
Raises:
| Type | Description |
|---|---|
RecipeError
|
If the lock is still held elsewhere when the timeout passes, or this handle has been released. |
Source code in capturegraph-lib/capturegraph/recipes/scratch/store/key_lock.py
claim(note)
#
Write note as the lock file's content, replacing what a past holder left.
Raises:
| Type | Description |
|---|---|
RecipeError
|
If this lock is not held. |
Source code in capturegraph-lib/capturegraph/recipes/scratch/store/key_lock.py
claimant()
#
The note the current or last holder wrote, or "".
release()
#
Let the next holder in and close the handle.
try_acquire()
#
Hold the lock if nobody else does, without waiting.
Raises:
| Type | Description |
|---|---|
RecipeError
|
If this handle has been released. |
Source code in capturegraph-lib/capturegraph/recipes/scratch/store/key_lock.py
lock_file_name(key)
#
A filesystem-safe name for the lock of key, unique per key.