o
    	h                     @  s   d Z ddlmZ ddlZddlmZ ddlmZ er>ddlm	Z	 ddlm
Z
mZ ejdk r4dd	lmZ ndd	lmZ ed
ZdZG dd dZG dd dZdS )z&Semaphores and concurrency primitives.    )annotationsN)deque)TYPE_CHECKING)TracebackType)CallableDeque)   
   )	ParamSpecP)	DummyLockLaxBoundedSemaphorec                   @  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   ^/var/www/html/optinet_system/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   acquire3   s   zLaxBoundedSemaphore.acquirec                 C  sN   z	|   \}}}W n ty   t| jd | j| _Y dS w ||i | dS )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kwargsr   r   r   releaseM   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   shrinkc   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   h   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__m   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__v   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__y   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   s   s    
r   )r=   
__future__r   syscollectionsr   typingr   typesr   r   r   version_infotyping_extensionsr
   r   __all__r   r   r   r   r   r   <module>   s    
^