
     h6                     N    d Z ddlmZ dZ G d d          Z G d d          ZdS )	z&Semaphores and concurrency primitives.    )deque)	DummyLockLaxBoundedSemaphorec                   @    e Zd ZdZd Zd Zd ZddZddZd Z	d	 Z
d
S )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        | _        d S N)initial_valuevaluer   _waitingappend_add_waiterpopleft_pop_waiter)selfr
   s     X/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/kombu/asynchronous/semaphore.py__init__zLaxBoundedSemaphore.__init__   s<    *//TZ=/=0    c                     | j         }|dk    r|                     |||f           dS t          |dz
  d          | _          ||i | dS )aL  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.acquire%   sc     
A::hnEFFF5UQY**DJHl5n5554r   c                     	 |                                  \  }}} ||i | dS # t          $ r& t          | j        dz   | j                  | _        Y dS 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.release9   s}    	$#'#3#3#5#5 FD& FD#F#####  	A 	A 	ATZ!^T-?@@DJJJJ	As   $ ,AAr   c                 ~      xj         |z  c_          xj        |z  c_         fdt          |          D              dS )z6Change the size of the semaphore to accept more users.c                 8    g | ]}                                 S  )r!   ).0_r   s     r   
<listcomp>z,LaxBoundedSemaphore.grow.<locals>.<listcomp>K   s!    ***A***r   N)r	   r
   ranger   ns   ` r   growzLaxBoundedSemaphore.growG   sL    a

a

****q******r   c                 z    t          | j        |z
  d          | _        t          | j        |z
  d          | _        dS )z6Change the size of the semaphore to accept less users.r   N)r   r	   r
   r)   s     r   shrinkzLaxBoundedSemaphore.shrinkM   s7     !3a!7;;a++


r   c                 P    | j                                          | j        | _        dS )z@Reset the semaphore, which also wipes out any waiting callbacks.N)r   clearr	   r
   r   s    r   r/   zLaxBoundedSemaphore.clearR   s$    '


r   c                     d                     | j        j        t          |           | j        t          | j                            S )Nz!<{} at {:#x} value:{} waiting:{}>)format	__class____name__idr
   lenr   r0   s    r   __repr__zLaxBoundedSemaphore.__repr__W   s:    299N#RXXtz3t};M;M
 
 	
r   N)r   )r4   
__module____qualname____doc__r   r   r!   r+   r-   r/   r7   r$   r   r   r   r      s         ,1 1 1  ($ $ $+ + + +, , , ,
( ( (

 
 
 
 
r   r   c                       e Zd ZdZd Zd ZdS )r   zPretending to be a lock.c                     | S r   r$   r0   s    r   	__enter__zDummyLock.__enter__`   s    r   c                     d S r   r$   )r   exc_infos     r   __exit__zDummyLock.__exit__c   s    r   N)r4   r8   r9   r:   r=   r@   r$   r   r   r   r   ]   s8        ""      r   r   N)r:   collectionsr   __all__r   r   r$   r   r   <module>rC      s    , ,      
.R
 R
 R
 R
 R
 R
 R
 R
j         r   