U
    /e                     @  sn  U d dl mZ d dlZd dlZd dlZi Zded< eej eej	 ej
dkZej
dkZej
dkZejdkrd d	lmZ n$d dlZd dlZd d
lmZ dd Zejdkrd dlmZ n:zd dlZdd ZW n$ ek
r   d dlZdd ZY nX ejdkrd dlmZ nZd dlZd dlZd dlZd dlmZ d dlm Z m!Z! d dlm"Z" d dl#m$Z$ G dd dZdS )    )annotationsNzdict[str | int, int | str]logging_nameslinuxdarwinwin32)   	   )	to_thread)eventsc                  s8   t  }t }tj|j| f||}|d|I dH S )a+  Asynchronously run function *func* in a separate thread.
        Any *args and **kwargs supplied for this function are directly passed
        to *func*. Also, the current :class:`contextvars.Context` is propagated,
        allowing context variables from the main thread to be accessed in the
        separate thread.

        Return a coroutine that can be awaited to get the eventual result of *func*.

        backport from
        https://github.com/python/cpython/blob/3f1ea163ea54513e00e0e9d5442fee1b639825cc/Lib/asyncio/threads.py#L12-L25
        N)r
   Zget_running_loopcontextvarsZcopy_context	functoolspartialrunZrun_in_executor)funcargskwargsZloopctxZ	func_call r   =/tmp/pip-unpacked-wheel-g426oqom/distributed/compatibility.pyr	      s    r	   )	randbytesc                 C  s   t jjd| dd S )N   u8)sizeZdtype)numpyrandomrandinttobytesr   r   r   r   r   0   s    r   c                 C  s
   t | S N)secretsZtoken_bytesr   r   r   r   r   6   s    )      r   r   )PeriodicCallback)isawaitable)	AwaitableCallable)IOLoop)app_logc                   @  s|   e Zd ZdZdddddddd	Zdd
ddZdd
ddZdd
ddZdd
ddZdd
ddZ	dddddZ
dS )r"   a  Schedules the given callback to be called periodically.

        The callback is called every ``callback_time`` milliseconds when
        ``callback_time`` is a float. Note that the timeout is given in
        milliseconds, while most other time-related functions in Tornado use
        seconds. ``callback_time`` may alternatively be given as a
        `datetime.timedelta` object.

        If ``jitter`` is specified, each callback time will be randomly selected
        within a window of ``jitter * callback_time`` milliseconds.
        Jitter can be used to reduce alignment of events with similar periods.
        A jitter of 0.1 means allowing a 10% variation in callback time.
        The window is centered on ``callback_time`` so the total number of calls
        within a given interval should not be significantly affected by adding
        jitter.

        If the callback runs for longer than ``callback_time`` milliseconds,
        subsequent invocations will be skipped to get back on schedule.

        `start` must be called after the `PeriodicCallback` is created.

        .. versionchanged:: 5.0
        The ``io_loop`` argument (deprecated since version 4.1) has been removed.

        .. versionchanged:: 5.1
        The ``jitter`` argument is added.

        .. versionchanged:: 6.2
        If the ``callback`` argument is a coroutine, and a callback runs for
        longer than ``callback_time``, subsequent invocations will be skipped.
        Previously this was only true for regular functions, not coroutines,
        which were "fire-and-forget" for `PeriodicCallback`.

        The ``callback_time`` argument now accepts `datetime.timedelta` objects,
        in addition to the previous numeric milliseconds.
        r   zCallable[[], Awaitable | None]zdatetime.timedelta | floatfloatNone)callbackcallback_timejitterreturnc                 C  sR   || _ t|tjr&|tjdd | _n|dkr6td|| _|| _d| _d | _d S )N   )Zmillisecondsr   z4Periodic callback must have a positive callback_timeF)	r*   
isinstancedatetime	timedeltar+   
ValueErrorr,   _running_timeout)selfr*   r+   r,   r   r   r   __init__r   s    zPeriodicCallback.__init__)r-   c                 C  s(   t  | _d| _| j | _|   dS )zStarts the timer.TN)r&   currentio_loopr3   time_next_timeout_schedule_nextr5   r   r   r   start   s    
zPeriodicCallback.startc                 C  s(   d| _ | jdk	r$| j| j d| _dS )zStops the timer.FN)r3   r4   r8   Zremove_timeoutr<   r   r   r   stop   s    
zPeriodicCallback.stopboolc                 C  s   | j S )znReturns ``True`` if this `.PeriodicCallback` has been started.

            .. versionadded:: 4.1
            )r3   r<   r   r   r   
is_running   s    zPeriodicCallback.is_runningc                   sl   | j s
d S zRz&|  }|d k	r0t|r0|I d H  W n& tk
rX   tjd| jdd Y nX W 5 |   X d S )NzException in callback %rT)exc_info)r3   r;   r*   r#   	Exceptionr'   error)r5   valr   r   r   _run   s    zPeriodicCallback._runc                 C  s.   | j r*| | j  | j| j| j| _d S r   )r3   _update_nextr8   r9   Zadd_timeoutr:   rE   r4   r<   r   r   r   r;      s    zPeriodicCallback._schedule_next)current_timer-   c                 C  sn   | j d }| jr*|d| jt d   9 }| j|kr\|  jt|| j | d | 7  _n|  j|7  _d S )Ng     @@r.   g      ?)r+   r,   r   r:   mathfloor)r5   rG   Zcallback_time_secr   r   r   rF      s    


zPeriodicCallback._update_nextN)r   )__name__
__module____qualname____doc__r6   r=   r>   r@   rE   r;   rF   r   r   r   r   r"   L   s   ) 
r"   )%
__future__r   loggingsysZtornador   __annotations__update_levelToName_nameToLevelplatformZLINUXZMACOSWINDOWSversion_infoZasyncior	   r   r   r
   r   r   r   ImportErrorr   Ztornado.ioloopr"   r0   rH   inspectr#   typingr$   r%   r&   Ztornado.logr'   r   r   r   r   <module>   sD    




