mirror of
https://github.com/carrotquest/django-clickhouse.git
synced 2025-02-15 02:10:33 +03:00
36 lines
912 B
Python
36 lines
912 B
Python
|
"""
|
||
|
Contains additional components for redis-py to use in RedisStorage
|
||
|
"""
|
||
|
from .exceptions import RedisLockTimeoutError
|
||
|
|
||
|
|
||
|
class RedisLock:
|
||
|
"""
|
||
|
Fixes issue of https://github.com/andymccurdy/redis-py/issues/621
|
||
|
"""
|
||
|
|
||
|
def __init__(self, redis_client, *args, **kwargs):
|
||
|
self.lock = redis_client.lock(*args, **kwargs)
|
||
|
|
||
|
def __enter__(self):
|
||
|
if self.lock.acquire():
|
||
|
return self
|
||
|
else:
|
||
|
raise RedisLockTimeoutError()
|
||
|
|
||
|
def __exit__(self, type, value, tb):
|
||
|
self.lock.release()
|
||
|
|
||
|
def acquire(self):
|
||
|
self.lock.acquire()
|
||
|
|
||
|
def release(self):
|
||
|
self.lock.release()
|
||
|
|
||
|
def hard_release(self) -> bool:
|
||
|
"""
|
||
|
Drops the lock, not looking if it is acquired by anyone.
|
||
|
:return: Boolean - if lock has been acquired before releasing or not
|
||
|
"""
|
||
|
return bool(self.lock.redis.delete(self.lock.name))
|