Return a new Lock object using key ``name`` that mimics the behavior of threading.Lock. If specified, ``timeout`` indicates a maximum life for the lock. By default, it will remain locked until release() is called. ``sleep`` indicates the amount of time to s
(
self,
name: str,
timeout: Optional[float] = None,
sleep: float = 0.1,
blocking: bool = True,
blocking_timeout: Optional[float] = None,
lock_class: Union[None, Any] = None,
thread_local: bool = True,
raise_on_release_error: bool = True,
)
| 603 | continue |
| 604 | |
| 605 | def lock( |
| 606 | self, |
| 607 | name: str, |
| 608 | timeout: Optional[float] = None, |
| 609 | sleep: float = 0.1, |
| 610 | blocking: bool = True, |
| 611 | blocking_timeout: Optional[float] = None, |
| 612 | lock_class: Union[None, Any] = None, |
| 613 | thread_local: bool = True, |
| 614 | raise_on_release_error: bool = True, |
| 615 | ): |
| 616 | """ |
| 617 | Return a new Lock object using key ``name`` that mimics |
| 618 | the behavior of threading.Lock. |
| 619 | |
| 620 | If specified, ``timeout`` indicates a maximum life for the lock. |
| 621 | By default, it will remain locked until release() is called. |
| 622 | |
| 623 | ``sleep`` indicates the amount of time to sleep per loop iteration |
| 624 | when the lock is in blocking mode and another client is currently |
| 625 | holding the lock. |
| 626 | |
| 627 | ``blocking`` indicates whether calling ``acquire`` should block until |
| 628 | the lock has been acquired or to fail immediately, causing ``acquire`` |
| 629 | to return False and the lock not being acquired. Defaults to True. |
| 630 | Note this value can be overridden by passing a ``blocking`` |
| 631 | argument to ``acquire``. |
| 632 | |
| 633 | ``blocking_timeout`` indicates the maximum amount of time in seconds to |
| 634 | spend trying to acquire the lock. A value of ``None`` indicates |
| 635 | continue trying forever. ``blocking_timeout`` can be specified as a |
| 636 | float or integer, both representing the number of seconds to wait. |
| 637 | |
| 638 | ``lock_class`` forces the specified lock implementation. Note that as |
| 639 | of redis-py 3.0, the only lock class we implement is ``Lock`` (which is |
| 640 | a Lua-based lock). So, it's unlikely you'll need this parameter, unless |
| 641 | you have created your own custom lock class. |
| 642 | |
| 643 | ``thread_local`` indicates whether the lock token is placed in |
| 644 | thread-local storage. By default, the token is placed in thread local |
| 645 | storage so that a thread only sees its token, not a token set by |
| 646 | another thread. Consider the following timeline: |
| 647 | |
| 648 | time: 0, thread-1 acquires `my-lock`, with a timeout of 5 seconds. |
| 649 | thread-1 sets the token to "abc" |
| 650 | time: 1, thread-2 blocks trying to acquire `my-lock` using the |
| 651 | Lock instance. |
| 652 | time: 5, thread-1 has not yet completed. redis expires the lock |
| 653 | key. |
| 654 | time: 5, thread-2 acquired `my-lock` now that it's available. |
| 655 | thread-2 sets the token to "xyz" |
| 656 | time: 6, thread-1 finishes its work and calls release(). if the |
| 657 | token is *not* stored in thread local storage, then |
| 658 | thread-1 would see the token value as "xyz" and would be |
| 659 | able to successfully release the thread-2's lock. |
| 660 | |
| 661 | ``raise_on_release_error`` indicates whether to raise an exception when |
| 662 | the lock is no longer owned when exiting the context manager. By default, |
no outgoing calls