
    Ih                        d Z ddlmZ ddlZddlmZ ddlmZ er2ddlm	Z	 ddlm
Z
mZ ej                  dk  rdd	lmZ ndd	lmZ  ed
      ZdZ G d d      Z G d d      Zy)z&Semaphores and concurrency primitives.    )annotationsN)deque)TYPE_CHECKING)TracebackType)CallableDeque)   
   )	ParamSpecP)	DummyLockLaxBoundedSemaphorec                  \    e Zd ZdZd
dZ	 	 	 	 	 	 	 	 ddZddZdddZdddZddZ	ddZ
y	)r   a  Asynchronous Bounded Semaphore.

    Lax means that the value will stay within the specified
    range even if released more times than it was acquired.

    Example:
    -------
        >>> x = LaxBoundedSemaphore(2)

        >>> x.acquire(print, 'HELLO 1')
        HELLO 1

        >>> x.acquire(print, 'HELLO 2')
        HELLO 2

        >>> x.acquire(print, 'HELLO 3')
        >>> x._waiters   # private, do not access directly
        [print, ('HELLO 3',)]

        >>> x.release()
        HELLO 3
    c                    |x| _         | _        t               | _        | j                  j                  | _        | j                  j                  | _        y N)initial_valuevaluer   _waitingappend_add_waiterpopleft_pop_waiter)selfr   s     U/var/www/html/planif/env/lib/python3.12/site-packages/kombu/asynchronous/semaphore.py__init__zLaxBoundedSemaphore.__init__-   s>    *//TZ&+g==//==00    c                    | j                   }|dk  r| j                  |||f       yt        |dz
  d      | _          ||i | y)a^  Acquire semaphore.

        This will immediately apply ``callback`` if
        the resource is available, otherwise the callback is suspended
        until the semaphore is released.

        Arguments:
        ---------
            callback (Callable): The callback to apply.
            *partial_args (Any): partial arguments to callback.
        r   F   T)r   r   max)r   callbackpartial_argspartial_kwargsr   s        r   acquirezLaxBoundedSemaphore.acquire3   sN    " 

A:hnEFUQY*DJl5n5r   c                    	 | j                         \  }}} ||i | y# t        $ r+ t        | j                  dz   | j                        | _        Y yw xY w)zRelease semaphore.

        Note:
        ----
            If there are any waiters this will apply the first waiter
            that is waiting for the resource (FIFO order).
        r   N)r   
IndexErrorminr   r   )r   waiterargskwargss       r   releasezLaxBoundedSemaphore.releaseM   sZ    	$#'#3#3#5 FD& D#F#  	ATZZ!^T-?-?@DJ	As    1AAc                    | xj                   |z  c_         | xj                  |z  c_        t        |      D ]  }| j                           y)z6Change the size of the semaphore to accept more users.N)r   r   ranger*   )r   n_s      r   growzLaxBoundedSemaphore.grow\   s<    a

a
q 	ALLN	r   c                |    t        | j                  |z
  d      | _        t        | j                  |z
  d      | _        y)z6Change the size of the semaphore to accept less users.r   N)r   r   r   )r   r-   s     r   shrinkzLaxBoundedSemaphore.shrinkc   s2     !3!3a!7;a+
r   c                Z    | j                   j                          | j                  | _        y)z@Reset the semaphore, which also wipes out any waiting callbacks.N)r   clearr   r   r   s    r   r3   zLaxBoundedSemaphore.clearh   s    ''
r   c                    dj                  | j                  j                  t        |       | j                  t        | j                              S )Nz!<{} at {:#x} value:{} waiting:{}>)format	__class____name__idr   lenr   r4   s    r   __repr__zLaxBoundedSemaphore.__repr__m   s9    299NN##RXtzz3t}};M
 	
r   N)r   intreturnNone)r    zCallable[P, None]r!   zP.argsr"   zP.kwargsr=   bool)r=   r>   )r   )r-   r<   r=   r>   )r=   str)r8   
__module____qualname____doc__r   r#   r*   r/   r1   r3   r;    r   r   r   r      sP    .1#  #	
 
4$,
(

r   r   c                  0    e Zd ZdZddZ	 	 	 	 	 	 	 	 ddZy)r   zPretending to be a lock.c                    | S r   rD   r4   s    r   	__enter__zDummyLock.__enter__v   s    r   c                     y r   rD   )r   exc_typeexc_valexc_tbs       r   __exit__zDummyLock.__exit__y   s     	r   N)r=   r   )rI   ztype[BaseException] | NonerJ   zBaseException | NonerK   zTracebackType | Noner=   r>   )r8   rA   rB   rC   rG   rL   rD   r   r   r   r   s   s5    ", & %	
 
r   r   )rC   
__future__r   syscollectionsr   typingr   typesr   r   r   version_infotyping_extensionsr   r   __all__r   r   rD   r   r   <module>rU      sV    , " 
   #&
'!/$#A
.[
 [
| r   