
    0Fie
                    x    d dl mZ d dlZd dlmZmZ d dlmZ  eded          Z	 G d dee	                   Z
dS )    )annotationsN)GenericTypeVar)time_Tc                      e Zd ZU dZded<   ded<   ded<   ded<   d	ed
<   ded<   ddZddZedd            Zedd            Z	edd            Z
ddZddZddZdS ) ResourceLimitera  Limit an abstract resource

    This allows us to track usage of an abstract resource. If the usage of this
    resources goes beyond a defined limit, we can block further execution

    Example::

        limiter = ResourceLimiter(2)
        limiter.increase(1)
        limiter.increase(2)
        limiter.decrease(1)

        # This will block since we're still not below limit
        await limiter.wait_for_available()
    r   limitfloattime_blocked_totaltime_blocked_avgint	_acquiredzasyncio.Condition
_condition_waitersc                |    || _         d| _        t          j                    | _        d| _        d| _        d| _        d S )Nr           )r
   r   asyncio	Conditionr   r   r   r   )selfr
   s     <lib/python3.11/site-packages/distributed/shuffle/_limiter.py__init__zResourceLimiter.__init__$   s=    
!+--"% #    returnstrc                (    d| j          d| j         dS )Nz<ResourceLimiter limit: z available: >)r
   	availabler   s    r   __repr__zResourceLimiter.__repr__,   s    S$*SS$.SSSSr   c                X    | j         | j         S t          d| j         | j        z
            S )z2How far can the value be increased before blockingNr   )r
   maxr   r   s    r   r   zResourceLimiter.available/   s-     ::1dj4>1222r   boolc                $    | j         duo| j          S )z)Return True if the limit has been reachedN)r   r   s    r   fullzResourceLimiter.full6   s     ~T)@$..@@r   c                    | j         dk    S )zLReturn True if nothing has been acquired / the limiter is in a neutral stater   r   r   s    r   emptyzResourceLimiter.empty;   s     ~""r   Nonec                4   K   t                      }d}	  j        s(	  xj        |z  c_         j        dz  |dz  z    _        dS  j        4 d{V   xj        dz  c_         j                             fd           d{V   xj        dz  c_        t                      |z
  }ddd          d{V  n# 1 d{V swxY w Y    xj        |z  c_         j        dz  |dz  z    _        dS #  xj        |z  c_         j        dz  |dz  z    _        w xY w)z)Block until the counter drops below limitr   g?g?N   c                      j          S )N)r%   r   s   r   <lambda>z4ResourceLimiter.wait_for_available.<locals>.<lambda>I   s    49} r   )r   r%   r   r   r   r   wait_for)r   startdurations   `  r   wait_for_availablez"ResourceLimiter.wait_for_available@   s     
	Q9  ##x/##$($9C$?(S.$PD!!!  * * * * * * * *"o../D/D/D/DEEEEEEEEE"66E>	* * * * * * * * * * * * * * * * * * * * * * * * * * * ##x/##$($9C$?(S.$PD!!! ##x/##$($9C$?(S.$PD!PPPPs6   C0 C0 AB8&C0 8
CC0 CC0 0'Dvaluec                &    | xj         |z  c_         dS )z&Increase the internal counter by valueNr'   r   r2   s     r   increasezResourceLimiter.increaseP   s    %r   c                
  K   || j         k    rt          d| d| j                    | xj         |z  c_         | j        4 d{V  | j                                         ddd          d{V  dS # 1 d{V swxY w Y   dS )z&Decrease the internal counter by valuez5Cannot release more than what was acquired! release: z acquired: N)r   RuntimeErrorr   
notify_allr4   s     r   decreasezResourceLimiter.decreaseT   s(     4>!!jjjZ^Zhjj   	%? 	) 	) 	) 	) 	) 	) 	) 	)O&&(((	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)s   A22
A<?A<N)r
   r   )r   r   )r   r   )r   r#   )r   r)   )r2   r   r   r)   )__name__
__module____qualname____doc____annotations__r   r    propertyr   r%   r(   r1   r5   r9    r   r   r	   r	      s1           IIINNN!!!!MMM$ $ $ $T T T T 3 3 3 X3 A A A XA # # # X#Q Q Q Q        ) ) ) ) ) )r   r	   )
__future__r   r   typingr   r   distributed.metricsr   r   r   r	   r@   r   r   <module>rD      s    " " " " " "  # # # # # # # # $ $ $ $ $ $WT3Q) Q) Q) Q) Q)gbk Q) Q) Q) Q) Q)r   