portalocker.redis module

class portalocker.redis.PubSubWorkerThread(pubsub, sleep_time, daemon=False)[source]

Bases: redis.client.PubSubWorkerThread

run()[source]

Method representing the thread’s activity.

You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

class portalocker.redis.RedisLock(channel: str, connection: Optional[redis.client.Redis] = None, timeout: Optional[float] = None, check_interval: Optional[float] = None, fail_when_locked: Optional[bool] = False, thread_sleep_time: float = 0.1, unavailable_timeout: float = 1, redis_kwargs: Optional[Dict] = None)[source]

Bases: portalocker.utils.LockBase

An extremely reliable Redis lock based on pubsub with a keep-alive thread

As opposed to most Redis locking systems based on key/value pairs, this locking method is based on the pubsub system. The big advantage is that if the connection gets killed due to network issues, crashing processes or otherwise, it will still immediately unlock instead of waiting for a lock timeout.

To make sure both sides of the lock know about the connection state it is recommended to set the health_check_interval when creating the redis connection..

Parameters
  • channel – the redis channel to use as locking key.

  • connection (or if you need to specify the redis) – an optional redis connection if you already have one

  • connection

  • timeout – timeout when trying to acquire a lock

  • check_interval – check interval while waiting

  • fail_when_locked – after the initial lock failed, return an error or lock the file. This does not wait for the timeout.

  • thread_sleep_time – sleep time between fetching messages from redis to prevent a busy/wait loop. In the case of lock conflicts this increases the time it takes to resolve the conflict. This should be smaller than the check_interval to be useful.

  • unavailable_timeout – If the conflicting lock is properly connected this should never exceed twice your redis latency. Note that this will increase the wait time possibly beyond your timeout and is always executed if a conflict arises.

  • redis_kwargs – The redis connection arguments if no connection is given. The DEFAULT_REDIS_KWARGS are used as default, if you want to override these you need to explicitly specify a value (e.g. health_check_interval=0)

DEFAULT_REDIS_KWARGS = {'health_check_interval': 10}
acquire(timeout: Optional[float] = None, check_interval: Optional[float] = None, fail_when_locked: Optional[bool] = None)[source]
channel: str
channel_handler(message)[source]
check_interval: float

check interval while waiting for timeout

check_or_kill_lock(connection, timeout)[source]
property client_name
close_connection: bool
connection: Optional[redis.client.Redis]
fail_when_locked: bool

skip the timeout and immediately fail if the initial lock fails

get_connection() redis.client.Redis[source]
pubsub: Optional[redis.client.PubSub] = None
redis_kwargs: Dict[str, Any]
release()[source]
thread: Optional[portalocker.redis.PubSubWorkerThread]
timeout: float

timeout when trying to acquire a lock