o
    wvXjZ  ã                   @   sX   d Z ddlmZmZ ddlmZ ddlmZ dZeG dd„ de	ƒƒZ
G dd	„ d	e	ƒZd
S )z&Semaphores and concurrency primitives.é    )Úabsolute_importÚunicode_literals)Údeque)Úpython_2_unicode_compatible)Ú	DummyLockÚLaxBoundedSemaphorec                   @   sL   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
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:
        >>> from future import print_statement as printf
        # ^ ignore: just fooling stupid pyflakes

        >>> x = LaxBoundedSemaphore(2)

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

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

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

        >>> x.release()
        HELLO 3
    c                 C   s,   | | _ | _tƒ | _| jj| _| jj| _d S ©N)Úinitial_valueÚvaluer   Ú_waitingÚappendÚ_add_waiterÚpopleftÚ_pop_waiter)Úselfr
   © r   úY/var/www/html/myproject/venv/lib/python3.10/site-packages/kombu/asynchronous/semaphore.pyÚ__init__'   s   
zLaxBoundedSemaphore.__init__c                 O   sD   | j }|dkr|  |||f¡ dS t|d 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
   r   r   r   Úacquire-   s   zLaxBoundedSemaphore.acquirec                 C   sN   z	|   ¡ \}}}W n ty   t| jd | jƒ| _Y dS w ||i |¤Ž dS )z«Release 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Úkwargsr   r   r   ÚreleaseA   s   ÿzLaxBoundedSemaphore.releaser   c                    s6   ˆ  j |7  _ ˆ  j|7  _‡ fdd„t|ƒD ƒ dS )z6Change the size of the semaphore to accept more users.c                    s   g | ]}ˆ   ¡ ‘qS r   )r   )Ú.0Ú_©r   r   r   Ú
<listcomp>S   s    z,LaxBoundedSemaphore.grow.<locals>.<listcomp>N)r	   r
   Úrange©r   Únr   r"   r   ÚgrowO   s   zLaxBoundedSemaphore.growc                 C   s(   t | j| dƒ| _t | j| dƒ| _dS )z6Change the size of the semaphore to accept less users.r   N)r   r	   r
   r%   r   r   r   ÚshrinkU   s   zLaxBoundedSemaphore.shrinkc                 C   s   | j  ¡  | j| _dS )z@Reset the semaphore, which also wipes out any waiting callbacks.N)r   Úclearr	   r
   r"   r   r   r   r)   Z   s   
zLaxBoundedSemaphore.clearc                 C   s    d  | jjt| ƒ| jt| jƒ¡S )Nz%<{0} at {1:#x} value:{2} waiting:{3}>)ÚformatÚ	__class__Ú__name__Úidr
   Úlenr   r"   r   r   r   Ú__repr___   s   ÿzLaxBoundedSemaphore.__repr__N)r   )r,   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r'   r(   r)   r/   r   r   r   r   r      s    

r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r   zPretending to be a lock.c                 C   s   | S r   r   r"   r   r   r   Ú	__enter__h   ó   zDummyLock.__enter__c                 G   s   d S r   r   )r   Úexc_infor   r   r   Ú__exit__k   r4   zDummyLock.__exit__N)r,   r0   r1   r2   r3   r6   r   r   r   r   r   e   s    r   N)r2   Ú
__future__r   r   Úcollectionsr   Ú
kombu.fiver   Ú__all__Úobjectr   r   r   r   r   r   Ú<module>   s   X