
    (phU,                     N    S SK r S SKrS SKrS SKJrJr  S SKJr   " S S\	5      r
g)    N)	LockErrorLockNotOwnedError)dummyc                       \ rS rSrSrSrSrSrSrSr	Sr
  SS jrS rS	 rS
 rSS jrS rS rS rS rS rSS jrS rS rS rSrg)Lock   z
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.
Nz
        local token = redis.call('get', KEYS[1])
        if not token or token ~= ARGV[1] then
            return 0
        end
        redis.call('del', KEYS[1])
        return 1
    a  
        local token = redis.call('get', KEYS[1])
        if not token or token ~= ARGV[1] then
            return 0
        end
        local expiration = redis.call('pttl', KEYS[1])
        if not expiration then
            expiration = 0
        end
        if expiration < 0 then
            return 0
        end

        local newttl = ARGV[2]
        if ARGV[3] == "0" then
            newttl = ARGV[2] + expiration
        end
        redis.call('pexpire', KEYS[1], newttl)
        return 1
    z
        local token = redis.call('get', KEYS[1])
        if not token or token ~= ARGV[1] then
            return 0
        end
        redis.call('pexpire', KEYS[1], ARGV[2])
        return 1
    c                    Xl         X l        X0l        X@l        XPl        X`l        [        U5      U l        U R                  (       a  [        R                  " 5       O	[        5       U l	        SU R                  l        U R                  5         g)aH	  
Create a new Lock instance named ``name`` using the Redis client
supplied by ``redis``.

``timeout`` indicates a maximum life for the lock.
By default, it will remain locked until release() is called.
``timeout`` can be specified as a float or integer, both representing
the number of seconds to wait.

``sleep`` indicates the amount of time to sleep per loop iteration
when the lock is in blocking mode and another client is currently
holding the lock.

``blocking`` indicates whether calling ``acquire`` should block until
the lock has been acquired or to fail immediately, causing ``acquire``
to return False and the lock not being acquired. Defaults to True.
Note this value can be overridden by passing a ``blocking``
argument to ``acquire``.

``blocking_timeout`` indicates the maximum amount of time in seconds to
spend trying to acquire the lock. A value of ``None`` indicates
continue trying forever. ``blocking_timeout`` can be specified as a
float or integer, both representing the number of seconds to wait.

``thread_local`` indicates whether the lock token is placed in
thread-local storage. By default, the token is placed in thread local
storage so that a thread only sees its token, not a token set by
another thread. Consider the following timeline:

    time: 0, thread-1 acquires `my-lock`, with a timeout of 5 seconds.
             thread-1 sets the token to "abc"
    time: 1, thread-2 blocks trying to acquire `my-lock` using the
             Lock instance.
    time: 5, thread-1 has not yet completed. redis expires the lock
             key.
    time: 5, thread-2 acquired `my-lock` now that it's available.
             thread-2 sets the token to "xyz"
    time: 6, thread-1 finishes its work and calls release(). if the
             token is *not* stored in thread local storage, then
             thread-1 would see the token value as "xyz" and would be
             able to successfully release the thread-2's lock.

In some use cases it's necessary to disable thread local storage. For
example, if you have code where one thread acquires a lock and passes
that lock instance to a worker thread to release later. If thread
local storage isn't disabled in this case, the worker thread won't see
the token set by the thread that acquired the lock. Our assumption
is that these cases aren't common and as such default to using
thread local storage.
N)redisnametimeoutsleepblockingblocking_timeoutboolthread_local	threadinglocalr   tokenregister_scripts)selfr
   r   r   r   r   r   r   s           =/var/www/html/venv/lib/python3.13/site-packages/redis/lock.py__init__Lock.__init__I   sc    h 
	
  0 .*.*;*;Y__&


    c                 D   U R                   nU R                  nUR                  c   UR                  UR                  5      Ul        UR
                  c   UR                  UR                  5      Ul        UR                  c!  UR                  UR                  5      Ul        g g N)		__class__r
   lua_releaseregister_scriptLUA_RELEASE_SCRIPT
lua_extendLUA_EXTEND_SCRIPTlua_reacquireLUA_REACQUIRE_SCRIPT)r   clsclients      r   r   Lock.register_scripts   s    nn??"$44S5K5KLCO>>!#33C4I4IJCN$&&s'?'?@  %r   c                 D    U R                  SS9(       a  U $ [        S5      e)NT)r   z0Unable to acquire lock within the time specified)acquirer   r   s    r   	__enter__Lock.__enter__   s#     <<<&KJKKr   c                 $    U R                  5         g r   )release)r   exc_type	exc_value	tracebacks       r   __exit__Lock.__exit__   s    r   c                 .   U R                   nUc.  [        R                  " 5       R                  R	                  5       nO5U R
                  R                  R                  5       nUR	                  U5      nUc  U R                  nUc  U R                  nSnUb  [        R                  " 5       U-   n U R                  U5      (       a  X0R                  l        gU(       d  g[        R                  " 5       U-   nUb  Xv:  a  g[        R                   " U5        Mh  )a  
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.
NTF)r   uuiduuid1hexencoder
   connection_poolget_encoderr   r   mod_timetime
do_acquirer   r   )r   r   r   r   r   encoderstop_trying_atnext_try_ats           r   r)   Lock.acquire   s      

=JJL$$++-Ejj00<<>GNN5)E}}H##44'%]]_/??Nu%%#(

 "--/E1K)k.JNN5! r   c                     U R                   (       a  [        U R                   S-  5      nOS nU R                  R                  U R                  USUS9(       a  gg)N  T)nxpxF)r   intr
   setr   )r   r   r   s      r   r=   Lock.do_acquire   sC    <<$,,-.GG::>>$))Ut>@r   c                 P    U R                   R                  U R                  5      SL$ )zE
Returns True if this key is locked by any process, otherwise False.
N)r
   getr   r*   s    r   lockedLock.locked   s      zz~~dii(44r   c                 \   U R                   R                  U R                  5      nU(       aJ  [        U[        5      (       d5  U R                   R
                  R                  5       nUR                  U5      nU R                  R                  SL=(       a    XR                  R                  :H  $ )zC
Returns True if this key is locked by this lock, otherwise False.
N)
r
   rJ   r   
isinstancebytesr9   r:   r8   r   r   )r   stored_tokenr>   s      r   owned
Lock.owned   s}     zz~~dii0 
< ? ?jj00<<>G">>,7Lzzt+ -JJ,,,	-r   c                     U R                   R                  nUc  [        S5      eSU R                   l        U R                  U5        g)z"Releases the already acquired lockNzCannot release an unlocked lock)r   r   r   
do_releaser   expected_tokens     r   r.   Lock.release   s;    ))!=>>

'r   c                     [        U R                  U R                  /U/U R                  S95      (       d  [	        S5      eg )Nkeysargsr&   z,Cannot release a lock that's no longer owned)r   r   r   r
   r   rU   s     r   rT   Lock.do_release   sO    D$$499++9*:,0JJ % 8 9 9 $ %> ? ?9r   c                     U R                   R                  c  [        S5      eU R                  c  [        S5      eU R	                  X5      $ )a0  
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`.
zCannot extend an unlocked lockz$Cannot extend a lock with no timeout)r   r   r   r   	do_extendr   additional_timereplace_ttls      r   extendLock.extend   sE     ::#<==<<BCC~~o;;r   c           	          [        US-  5      n[        U R                  U R                  /U R                  R
                  UU=(       a    S=(       d    S/U R                  S95      (       d  [        S5      eg)NrC   10rY   z+Cannot extend a lock that's no longer ownedT)rF   r   r!   r   r   r   r
   r   r_   s      r   r^   Lock.do_extend  sz    o45OOii[JJ$$#'C.3
 zz  

 

 $@  r   c                     U R                   R                  c  [        S5      eU R                  c  [        S5      eU R	                  5       $ )zC
Resets a TTL of an already acquired lock back to a timeout value.
z!Cannot reacquire an unlocked lockz'Cannot reacquire a lock with no timeout)r   r   r   r   do_reacquirer*   s    r   	reacquireLock.reacquire  sE     ::#?@@<<EFF  ""r   c                     [        U R                  S-  5      n[        U R                  U R                  /U R
                  R                  U/U R                  S95      (       d  [        S5      eg)NrC   rY   z.Cannot reacquire a lock that's no longer ownedT)	rF   r   r   r#   r   r   r   r
   r   )r   r   s     r   ri   Lock.do_reacquire  sl    dllT)*D&&TYYK-1ZZ-=-=w,G.2jj ' : ; ; $ %7 8 8r   )r   r   r   r   r
   r   r   r   )Ng?TNT)NNN)F)__name__
__module____qualname____firstlineno____doc__r   r!   r#   r    r"   r$   r   r   r+   r2   r)   r=   rK   rQ   r.   rT   rb   r^   rj   ri   __static_attributes__ r   r   r   r      s     KJM
2 9<DH= ~	AL&"P5-(?<"$#r   r   )r   r<   r;   r5   redis.exceptionsr   r   redis.utilsr   objectr   rt   r   r   <module>rx      s"       9 ]6 ]r   