
    0Fie'                    .   U d dl mZ d dlZd dlZd dlZd dlZd dlZd dlmZm	Z	 d dl
mZmZ d dlZg d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d Zd Zej        dk    rd dlmZ n-d dlZd dlZd dlmZ d dl m!Z! d dlm"Z" d dl#m$Z$  G d d          Z ed          Z%ej        dk    r	ej&        Z'dS ej        dk    r
dddd'd!Z'dS dddd'd"Z'd(d&Z(dS ))    )annotationsN)Callable	Coroutine)AnyTypeVar)logging_namesPeriodicCallback	to_thread	randbyteszdict[str | int, int | str]r   linuxdarwinwin32c                 \    t          j        dt          d           t          j        | i |S )Nz_to_thread is deprecated and will be removed in a future release; use asyncio.to_thread instead.   
stacklevel)warningswarnDeprecationWarningasyncior
   argskwargss     9lib/python3.11/site-packages/distributed/compatibility.pyr
   r
      s<    Mi   
 d-f---    c                 \    t          j        dt          d           t          j        | i |S )Nz^randbytes is deprecated and will be removed in a future release; use random.randbytes instead.r   r   )r   r   r   randomr   r   s     r   r   r   !   s<    Mh   
 T,V,,,r   )   r   r   r   )r	   )	Awaitable)isawaitable)IOLoop)app_logc                  N    e Zd ZdZ	 dddZddZddZddZddZddZ	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   callbackCallable[[], Awaitable | None]callback_timedatetime.timedelta | floatjitterfloatreturnNonec                    || _         t          |t          j                  r|t          j        d          z  | _        n|dk    rt          d          || _        || _        d| _        d | _        d S )N   )millisecondsr   z4Periodic callback must have a positive callback_timeF)	r$   
isinstancedatetime	timedeltar&   
ValueErrorr(   _running_timeout)selfr$   r&   r(   s       r   __init__zPeriodicCallback.__init__a   s     %DM-);<< 3%2X5GUV5W5W5W%W"" A%%$N   &3" DK!DM DMMMr   c                    t          j                    | _        d| _        | j                                        | _        |                                  dS )zStarts the timer.TN)r!   currentio_loopr3   time_next_timeout_schedule_nextr5   s    r   startzPeriodicCallback.startt   sH    
 ">++DL DM!%!2!2!4!4D!!!!!r   c                r    d| _         | j        (| j                            | j                   d| _        dS dS )zStops the timer.FN)r3   r4   r9   remove_timeoutr=   s    r   stopzPeriodicCallback.stop~   s<    !DM}(++DM::: $ )(r   boolc                    | j         S )znReturns ``True`` if this `.PeriodicCallback` has been started.

            .. versionadded:: 4.1
            )r3   r=   s    r   
is_runningzPeriodicCallback.is_running   s    
 = r   c                ,  K   | j         sd S 	 |                                 }|t          |          r| d {V  n,# t          $ r t	          j        d| j        d           Y nw xY w|                                  d S # |                                  w xY w)NzException in callback %rT)exc_info)r3   r$   r    	Exceptionr"   errorr<   )r5   vals     r   _runzPeriodicCallback._run   s      = &mmoo?{3'7'7?IIIIIII X X X8$-RVWWWWWWX ##%%%%%##%%%%s&   -; A= &A$!A= #A$$A= =Bc                    | j         rX|                     | j                                                   | j                            | j        | j                  | _        d S d S )N)r3   _update_nextr9   r:   add_timeoutr;   rJ   r4   r=   s    r   r<   zPeriodicCallback._schedule_next   s]    } X!!$,"3"3"5"5666 $ 8 89KTY W WX Xr   current_timec                   | j         dz  }| j        r$|d| j        t          j                    dz
  z  z   z  }| j        |k    r5| xj        t	          j        || j        z
  |z            dz   |z  z  c_        d S | xj        |z  c_        d S )Ng     @@r-   g      ?)r&   r(   r   r;   mathfloor)r5   rN   callback_time_secs      r   rL   zPeriodicCallback._update_next   s     $ 2V ;{ Q!Q$+39N*O%PP!!\11
 ""Jt/A AEVVWW%'& &""""& ""&77""""r   N)r   )r$   r%   r&   r'   r(   r)   r*   r+   )r*   r+   )r*   rB   )rN   r)   r*   r+   )__name__
__module____qualname____doc__r6   r>   rA   rD   rJ   r<   rL    r   r   r	   r	   ;   s        #	 #	R 		! 	! 	! 	! 	!&	" 	" 	" 	"	% 	% 	% 	%	! 	! 	! 	!
	& 
	& 
	& 
	&	X 	X 	X 	X
	8 	8 	8 	8 	8 	8r   r	   _T)      )rY      Fdebugloop_factorymainCoroutine[Any, Any, _T]r]   rB   r^   .Callable[[], asyncio.AbstractEventLoop] | Noner*   c                   t          j        ||          5 }|                    |           cd d d            S # 1 swxY w Y   d S )Nr\   )r   Runnerrun)r_   r]   r^   runners       r   asyncio_runrf      s     ^%lCCC 	$v::d##	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$s   9= =c               `   	 t          j                     t          d          # t          $ r Y nw xY wt          j        |           st	          d|           |t          j                    }n
 |            }	 |t          j        |           ||                    |           |                    |           	 t          |           |                    |
                                           |                    |                                           |t          j        d            |                                 S # |t          j        d            |                                 w xY w# 	 t          |           |                    |
                                           |                    |                                           |t          j        d            |                                 w # |t          j        d            |                                 w xY wxY w)Nz8asyncio.run() cannot be called from a running event loopza coroutine was expected, got )r   get_running_loopRuntimeErroriscoroutiner2   new_event_loopset_event_loop	set_debugrun_until_complete_cancel_all_tasksshutdown_asyncgensshutdown_default_executorclose)r_   r]   r^   loops       r   rf   rf      s0   	$&&& J    	 	 	D	 "4(( 	HFdFFGGG)++DD<>>D	#&t,,, u%%%**400!$'''''(?(?(A(ABBB''(F(F(H(HIII'*4000

  '*4000

!$'''''(?(?(A(ABBB''(F(F(H(HIII'*4000

  '*4000

s?   $ 
11;AE4 =AE,E14H-6AG>+H->,H**H-rs   asyncio.AbstractEventLoopr+   c                b   t          j        |           }|sd S |D ]}|                                 |                     t          j        |ddi           |D ]V}|                                r|                                +|                     d|                                |d           Wd S )Nreturn_exceptionsTz1unhandled exception during asyncio.run() shutdown)message	exceptiontask)r   	all_taskscancelrn   gather	cancelledrx   call_exception_handler)rs   	to_cancelry   s      r   ro   ro      s    %d++	 	F 	 	DKKMMMM	 RT R RSSS 
	 
	D~~ ~~+++#V%)^^%5%5 $   	
	 
	r   )r_   r`   r]   rB   r^   ra   r*   rX   )rs   rt   r*   r+   ))
__future__r   r   loggingr   sysr   collections.abcr   r   typingr   r   tornado__all__r   __annotations__update_levelToName_nameToLevelplatformLINUXMACOSWINDOWSr
   r   version_infotornado.ioloopr	   r0   rP   r   inspectr    r!   tornado.logr"   rX   rd   rf   ro   rW   r   r   <module>r      si   " " " " " " "    



  / / / / / / / /         
I
I
I,. . . . .   W) * * *   W) * * * 
,'
!. . .- - - <''/////// OOOKKK))))))######%%%%%%######8 8 8 8 8 8 8 8D WT]]w+KKK  
 GK		$ 	$ 	$ 	$ 	$ 	$ 	$ 	$" GK	$ $ $ $ $ $L     r   