锁(Lock)

class redis.lock.Lock(redis, name, timeout=None, sleep=0.1, blocking=True, blocking_timeout=None, thread_local=True)[源代码]

A shared, distributed Lock. Using Redis for locking allows the Lock to be shared across processes and/or machines.

It's left to the user to resolve deadlock issues and make sure multiple clients play nicely together.

参数:
  • name (str)

  • timeout (int | float | None)

  • sleep (int | float)

  • blocking (bool)

  • blocking_timeout (int | float | None)

  • thread_local (bool)

acquire(sleep=None, blocking=None, blocking_timeout=None, token=None)[源代码]

Use Redis to hold a shared, distributed lock named name. Returns True once the lock is acquired.

If blocking is False, always return immediately. If the lock was acquired, return True, otherwise return False.

blocking_timeout specifies the maximum number of seconds to wait trying to acquire the lock.

token specifies the token value to be used. If provided, token must be a bytes object or a string that can be encoded to a bytes object with the default encoding. If a token isn't specified, a UUID will be generated.

参数:
  • sleep (int | float | None)

  • blocking (bool | None)

  • blocking_timeout (int | float | None)

  • token (str | None)

extend(additional_time, replace_ttl=False)[源代码]

Adds more time to an already acquired lock.

additional_time can be specified as an integer or a float, both representing the number of seconds to add.

replace_ttl if False (the default), add additional_time to the lock's existing ttl. If True, replace the lock's ttl with additional_time.

参数:
  • additional_time (int)

  • replace_ttl (bool)

返回类型:

bool

locked()[源代码]

Returns True if this key is locked by any process, otherwise False.

返回类型:

bool

owned()[源代码]

Returns True if this key is locked by this lock, otherwise False.

返回类型:

bool

reacquire()[源代码]

Resets a TTL of an already acquired lock back to a timeout value.

返回类型:

bool

release()[源代码]

Releases the already acquired lock

返回类型:

None