
    hj                          d dl Z  G d d      Zy)    Nc                       e Zd ZdZd
dZed        Zej                  d        Zed        Zej                  d        Zd Z	d Z
y	)Throttlea(  
    Helper class that wrap a function with a given max execution rate.
    By default the rate is set to execute no more than once a second.

    :param fn: the function to call.
    :type fn: function

    :param ts: Number of seconds to wait before the next execution.
    :type ts: float
    c                 f    || _         || _        d| _        d| _        d | _        g | _        i | _        y )Nr   F)_ts_fn	_requests_pending_pending_task
_last_args_last_kwargs)selffntss      G/DATA/.local/lib/python3.12/site-packages/trame_common/exec/throttle.py__init__zThrottle.__init__   s5    !    c                      d| j                   z  S )z'Number of maximum executions per second      ?r   r   s    r   ratezThrottle.rate   s     TXX~r   c                     d|z  | _         y)z3Update the maximum number of executions per secondsr   Nr   )r   r   s     r   r   zThrottle.rate   s     :r   c                     | j                   S )z+Number of seconds to wait between executionr   r   s    r   delta_tzThrottle.delta_t#   s     xxr   c                     || _         y)z6Update the number of seconds to wait between executionNr   )r   secondss     r   r   zThrottle.delta_t(   s     r   c                 >  K   d| _         | j                  r- | j                  | j                  i | j                   d| _        t        j                  | j                         d {    | j                  dkD  r| j                          d {    d| _         y 7 37 w)NTr   F)	r	   r   r   r   r   asynciosleepr   _trottler   s    r   r    zThrottle._trottle-   sy     >>DHHdoo;):):;DNmmDHH%%%>>A--/!! 	&!s$   A#B%B&&BBBBc                     | xj                   dz  c_         || _        || _        | j                  s)t	        j
                  | j                               | _        yy)zUFunction call wrapper that will throttle the actual function provided at construction   N)r   r   r   r	   r   create_taskr    r
   )r   argskwargss      r   __call__zThrottle.__call__8   sD    !"}}!(!4!4T]]_!ED r   N)r"   )__name__
__module____qualname____doc__r   propertyr   setterr   r    r&    r   r   r   r      so    	   
[[    ^^ 	Fr   r   )r   r   r-   r   r   <module>r.      s    ;F ;Fr   