
    ԋg                    t    d dl mZ d dlZd dlZd dlmZ  ej                  e      Z e	       Z
 G d de      Zy)    )annotationsN)	Semaphorec                  P     e Zd ZdZdeddf fd	Zd	 fd	Zd Zd Zd Z	d Z
 xZS )
Locka  Distributed Centralized Lock

    .. warning::

        This is using the ``distributed.Semaphore`` as a backend, which is
        susceptible to lease overbooking. For the Lock this means that if a
        lease is timing out, two or more instances could acquire the lock at the
        same time. To disable lease timeouts, set
        ``distributed.scheduler.locks.lease-timeout`` to `inf`, e.g.

        .. code-block:: python

            with dask.config.set({"distributed.scheduler.locks.lease-timeout": "inf"}):
                lock = Lock("x")
                ...

        Note, that without lease timeouts, the Lock may deadlock in case of
        cluster downscaling or worker failures.

    Parameters
    ----------
    name: string (optional)
        Name of the lock to acquire.  Choosing the same name allows two
        disconnected processes to coordinate a lock.  If not given, a random
        name will be generated.
    client: Client (optional)
        Client to use for communication with the scheduler.  If not given, the
        default global client will be used.

    Examples
    --------
    >>> lock = Lock('x')  # doctest: +SKIP
    >>> lock.acquire(timeout=1)  # doctest: +SKIP
    >>> # do things with protected resource
    >>> lock.release()  # doctest: +SKIP
    Nc                    |t         urdd l}|j                  dt        d       |xs! dt	        j
                         j                  z   | _        t        | %  d|||       y )Nr   zYThe `client` parameter is deprecated. It is no longer necessary to pass a client to Lock.   )
stacklevelzlock-   )
max_leasesnamescheduler_rpcloop)
	_no_valuewarningswarnDeprecationWarninguuiduuid4hexr   super__init__)selfr   clientr   r   r   	__class__s         0lib/python3.12/site-packages/distributed/lock.pyr   zLock.__init__3   sg     "MMk"   6Gdjjl&6&66	'	 	 	
    c                F    |s|t        d      d}t        | 	  |      S )a/  Acquire the lock

        Parameters
        ----------
        blocking : bool, optional
            If false, don't wait on the lock in the scheduler at all.
        timeout : string or number or timedelta, optional
            Seconds to wait on the lock in the scheduler.  This does not
            include local coroutine time, network transfer time, etc..
            It is forbidden to specify a timeout when blocking is false.
            Instead of number of seconds, it is also possible to specify
            a timedelta in string format, e.g. "200ms".

        Examples
        --------
        >>> lock = Lock('x')  # doctest: +SKIP
        >>> lock.acquire(timeout="1s")  # doctest: +SKIP

        Returns
        -------
        True or False whether or not it successfully acquired the lock
        z/can't specify a timeout for a non-blocking callr   )timeout)
ValueErrorr   acquire)r   blockingr   r   s      r   r    zLock.acquireK   s0    . " !RSSGww//r   c                t   K   | j                   j                  | j                         d {   }|dk(  S 7 	w)Nr   r
   )	schedulersemaphore_valuer   )r   vals     r   _lockedzLock._lockedh   s3     NN22		2BBax Cs   *86
8c                8    | j                  | j                        S N)syncr'   r   s    r   lockedzLock.lockedl   s    yy&&r   c                    | j                   S r)   r#   r+   s    r   __getstate__zLock.__getstate__o   s    yyr   c                (    | j                  |       y )Nr#   )r   )r   states     r   __setstate__zLock.__setstate__r   s    5!r   )TN)__name__
__module____qualname____doc__r   r   r    r'   r,   r.   r1   __classcell__)r   s   @r   r   r      s5    #N 
00:'"r   r   )
__future__r   loggingr   distributed.semaphorer   	getLoggerr2   loggerobjectr   r    r   r   <module>r>      s8    "   +			8	$H	f"9 f"r   