o
    ¨D®fÁ  ã                   @  s˜   d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	 ej
dk r(ddlmZ nddlmZ er6ddlmZ d	Zed
ƒZG dd„ dƒZG dd„ dƒZdS )z&Semaphores and concurrency primitives.é    )ÚannotationsN)Údeque)ÚTYPE_CHECKINGÚCallableÚDeque)é   é
   )Ú	ParamSpec)ÚTracebackType)Ú	DummyLockÚLaxBoundedSemaphoreÚPc                   @  sZ   e Zd ZdZddd„Zd dd„Zd!dd„Zd"d#dd„Zd"d#dd„Zd!dd„Z	d$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:
    -------
        >>> 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
    ÚvalueÚintÚreturnÚNonec                 C  s,   | | _ | _tƒ | _| jj| _| jj| _d S ©N)Úinitial_valuer   r   Ú_waitingÚappendÚ_add_waiterÚpopleftÚ_pop_waiter)Úselfr   © r   úU/home/ubuntu/webapp/venv/lib/python3.10/site-packages/kombu/asynchronous/semaphore.pyÚ__init__.   s   
zLaxBoundedSemaphore.__init__ÚcallbackúCallable[P, None]Úpartial_argsúP.argsÚpartial_kwargsúP.kwargsÚboolc                 O  sD   | j }|dkr|  |||f¡ dS t|d dƒ| _ ||i |¤Ž dS )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   r   r   r!   r   r   r   r   Úacquire4   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   ÚreleaseN   s   ÿzLaxBoundedSemaphore.releaser$   Únc                 C  s6   |  j |7  _ |  j|7  _t|ƒD ]}|  ¡  qdS )z6Change the size of the semaphore to accept more users.N)r   r   Úranger,   )r   r-   Ú_r   r   r   Úgrow]   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   r   Úshrinkd   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   r2   i   s   
zLaxBoundedSemaphore.clearÚstrc                 C  s    d  | jjt| ƒ| jt| jƒ¡S )Nz!<{} at {:#x} value:{} waiting:{}>)ÚformatÚ	__class__Ú__name__Úidr   Úlenr   r3   r   r   r   Ú__repr__n   s   ÿzLaxBoundedSemaphore.__repr__N)r   r   r   r   )r   r   r   r    r!   r"   r   r#   )r   r   )r$   )r-   r   r   r   )r   r4   )r7   Ú
__module__Ú__qualname__Ú__doc__r   r&   r,   r0   r1   r2   r:   r   r   r   r   r      s    



r   c                   @  s$   e Zd ZdZddd„Zddd„ZdS )r   zPretending to be a lock.r   c                 C  s   | S r   r   r3   r   r   r   Ú	__enter__w   s   zDummyLock.__enter__Úexc_typeútype[BaseException] | NoneÚexc_valúBaseException | NoneÚexc_tbúTracebackType | Noner   c                 C  s   d S r   r   )r   r?   rA   rC   r   r   r   Ú__exit__z   s   zDummyLock.__exit__N)r   r   )r?   r@   rA   rB   rC   rD   r   r   )r7   r;   r<   r=   r>   rE   r   r   r   r   r   t   s    
r   )r=   Ú
__future__r   ÚsysÚcollectionsr   Útypingr   r   r   Úversion_infoÚtyping_extensionsr	   Útypesr
   Ú__all__r   r   r   r   r   r   r   Ú<module>   s    
^