
    'j                     (    d dl Z  G d d          ZdS )    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
d
S )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                 h    || _         || _        d| _        d| _        d | _        g | _        i | _        d S )Nr   F)_ts_fn	_requests_pending_pending_task
_last_args_last_kwargs)selffntss      T/DATA/AppData/hermes/venv/lib/python3.11/site-packages/trame_common/exec/throttle.py__init__zThrottle.__init__   s;    !    c                     d| j         z  S )z'Number of maximum executions per second      ?r   r   s    r   ratezThrottle.rate   s     TX~r   c                     d|z  | _         dS )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     xr   c                     || _         dS )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 {V  | j        dk    r|                                  d {V  d| _         d S )NTr   F)	r
   r	   r   r   r   asynciosleepr   _trottler   s    r   r!   zThrottle._trottle-   s      > 	DHdo;):;;;DNmDH%%%%%%%%%>A--//!!!!!!!r   c                     | xj         dz  c_         || _        || _        | j        s-t	          j        |                                           | _        dS dS )zUFunction call wrapper that will throttle the actual function provided at constructionr   N)r	   r   r   r
   r   create_taskr!   r   )r   argskwargss      r   __call__zThrottle.__call__8   sY    !"} 	F!(!4T]]__!E!ED	F 	Fr   N)r   )__name__
__module____qualname____doc__r   propertyr   setterr   r!   r&    r   r   r   r      s        	 	      X 
[  [   X ^  ^	 	 	F F F F Fr   r   )r   r   r-   r   r   <module>r.      sJ    ;F ;F ;F ;F ;F ;F ;F ;F ;F ;Fr   