Lock - Lock Parameters in Py-Cachify¶
Parameters¶
Here, we will detail the various parameters that you can configure when creating a lock and how to use them effectively.
Explanation of Parameters¶
-
key:
- This is a mandatory parameter that uniquely identifies the lock. Each operation you wish to manage with a lock should have a unique key.
-
nowait:
- Setting
nowait=True(default) means that if the lock is already held by another lock, your current lock won't wait and will immediately raise aCachifyLockError. - If
nowait=False, your lock will wait until the lock becomes available, up to the duration specified bytimeout.
- Setting
-
timeout:
- Use this parameter to specify how long (in seconds) the lock should wait for to acquire a lock. If the lock does not become available within this time, a
CachifyLockErroris raised. - Timeout only works if
nowaitisFalse.
- Use this parameter to specify how long (in seconds) the lock should wait for to acquire a lock. If the lock does not become available within this time, a
- exp:
- This parameter sets an expiration time (in seconds) for the lock. After this time, the lock will automatically be released, regardless of whether the operation has been completed.
- This can help to prevent deadlocks in cases where an app may fail to release the lock due to an error or abrupt termination.
Lock Polling Interval (Global Setting)¶
When nowait=False, the lock uses a polling mechanism to check if the lock has become available. The interval between these polling attempts is controlled by the lock_poll_interval parameter in init_cachify():
- Default:
0.1seconds (100 milliseconds) - Lower values: More responsive lock acquisition but higher load on the cache backend
- Higher values: Reduced backend load but potentially longer wait times after a lock is released
You can configure this when initializing py-cachify:
from py_cachify import init_cachify
# Poll every 500ms instead of the default 100ms
init_cachify(lock_poll_interval=0.5)
This setting applies to all locks and once decorators that use this Cachify instance.
Some examples¶
Let's write the example showcasing every parameter and then go through the output to understand what is happening:
import asyncio
from py_cachify import init_cachify, lock
# Initialize py-cachify to use in-memory cache
init_cachify()
async def main() -> None:
example_lock = lock(key='example-lock', nowait=False, timeout=4, exp=2)
async with example_lock:
print('This code is executing under a lock with a timeout of 4 seconds and expiration set to 2 seconds')
async with example_lock:
print('This code is acquiring the same lock under the previous one.')
if __name__ == '__main__':
asyncio.run(main())
After running the example:
example-lock is already locked!
example-lock is already locked!
example-lock is already locked!
example-lock is already locked!
This code is acquiring the same lock under the previous one.
As you can see we got no errors, because the lock expired after 2 seconds, and the timeout (maximum wait time to acquire a lock) is set to 4, which is enough to wait for the first expiration of the first acquire.
What's next¶
Next, we'll see what methods do lock objects have in py-cachify.