o
    >hF                     @   s8   d dl Z d dlmZ G dd deZG dd deZdS )    N)IBatchedTimerc                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	_BatchedCallz
    Wraps IDelayedCall-implementing objects, implementing only the API
    which txaio promised in the first place: .cancel

    Do not create these yourself; use _BatchedTimer.call_later()
    c                 C   s   || _ || _|| _d S N)_timer_index_call)selftimerindexthe_call r   m/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/txaio/_common.py__init__'   s   
z_BatchedCall.__init__c                 C   s   | j | j|  d | _ d S r   )r   _remove_callr   r   r   r   r   cancel-   s   
z_BatchedCall.cancelc                 C   s   |   S r   )r   r   r   r   r   __call__1   s   z_BatchedCall.__call__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r      s
    r   c                   @   s4   e Zd ZdZ	dddZdd Zdd Zd	d
 ZdS )_BatchedTimera=  
    Internal helper.

    Instances of this are returned from
    :meth:`txaio.make_batched_timer` and that is the only way they
    should be instantiated. You may depend on methods from the
    interface class only (:class:`txaio.IBatchedTimer`)

    **NOTE** that the times are in milliseconds in this class!
    Nc                 C   s>   |dkrt dt|| _|| _|| _|| _t | _|| _d S )N        z!bucket_milliseconds must be > 0.0)	
ValueErrorfloat_bucket_milliseconds_chunk_size_get_seconds_create_delayed_calldict_buckets_loop)r   bucket_milliseconds
chunk_sizeseconds_providerdelayed_call_creatorloopr   r   r   r   A   s   

z_BatchedTimer.__init__c           
         s   |   }t|| d }|t|| j 8 }t| | fdd}z| j| d | W |S  tyQ   |d | }| td|| j	|}	|	|gf| j|< Y |S w )z#
        IBatchedTimer API
        i  c                      s    i S r   r   r   argsfunckwargsr   r   <lambda>V   s    z*_BatchedTimer.call_later.<locals>.<lambda>        @@r   )
r   intr   r   r    appendKeyErrorr   max_notify_bucket)
r   delayr)   r(   r*   now	real_timecalldiffdelayed_callr   r'   r   
call_laterN   s    z_BatchedTimer.call_laterc                    s`   j | \}}j |= g   fddjttt|j  }|jtd| dS )z
        Internal helper. This 'does' the callbacks in a particular bucket.

        :param real_time: the bucket to do callbacks on
        c                    s   | d | D ]}z|  W q t y$ } z | W Y d }~qd }~ww | |d  } | r:|d | || d S t rPd} D ]	}|d|7 }qBt|d S )Nr-   z'Error(s) processing call_later bucket:
z{}
)	Exceptionr/   r   lenformatRuntimeError)callsr#   chunk_delay_msr6   emsgerrorsnotify_one_chunkr   r   r   rD   p   s&   
z6_BatchedTimer._notify_bucket.<locals>.notify_one_chunkr   N)r    r   mathceilr   r;   r   r1   )r   r5   r8   r>   delay_msr   rB   r   r2   f   s   z_BatchedTimer._notify_bucketc                 C   sN   z	| j | \}}W n
 ty   Y dS w || |s%| j |= |  dS dS )z
        Internal helper. Removes a (possibly still pending) call from a
        bucket. It is *not* an error of the bucket is gone (e.g. the
        call has already happened).
        N)r    r0   remover   )r   r5   r6   r8   r>   r   r   r   r      s   
z_BatchedTimer._remove_callr   )r   r   r   r   r   r9   r2   r   r   r   r   r   r   5   s    
$r   )rE   txaio.interfacesr   objectr   r   r   r   r   r   <module>   s   