
    ԋg'                    v   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j#                  ej$                         ej#                  ej&                         ej(                  dk(  Zej(                  d	k(  Zej(                  d
k(  Zd Zd Zej4                  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j4                  dk\  rejL                  Z'yej4                  dk\  rddd	 	 	 	 	 	 	 ddZ'yddd	 	 	 	 	 	 	 ddZ'ddZ(y)    )annotationsN)Callable	Coroutine)AnyTypeVar)logging_namesPeriodicCallback	to_thread	randbyteszdict[str | int, int | str]r   linuxdarwinwin32c                 d    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.12/site-packages/distributed/compatibility.pyr
   r
      s/    MMi
 d-f--    c                 d    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/    MMh
 T,V,,r   )   r   r   r   )r	   )	Awaitable)isawaitable)IOLoop)app_logc                  Z    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
y	)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.
        c                    || _         t        |t        j                        r|t        j                  d      z  | _        n|dk  rt        d      || _        || _        d| _        d | _        y )N   )millisecondsr   z4Periodic callback must have a positive callback_timeF)	callback
isinstancedatetime	timedeltacallback_time
ValueErrorjitter_running_timeout)selfr'   r+   r-   s       r   __init__zPeriodicCallback.__init__a   sk     %DM-););<%2X5G5GUV5W%W" A%$N  &3" DK!DM DMr   c                    t        j                         | _        d| _        | j                  j	                         | _        | j                          y)zStarts the timer.TN)r!   currentio_loopr.   time_next_timeout_schedule_nextr0   s    r   startzPeriodicCallback.startt   s:    
 ">>+DL DM!%!2!2!4D!r   c                    d| _         | j                  -| j                  j                  | j                         d| _        yy)zStops the timer.FN)r.   r/   r4   remove_timeoutr8   s    r   stopzPeriodicCallback.stop~   s5    !DM}}(++DMM: $ )r   c                    | j                   S )znReturns ``True`` if this `.PeriodicCallback` has been started.

            .. versionadded:: 4.1
            )r.   r8   s    r   
is_runningzPeriodicCallback.is_running   s    
 == r   c                &  K   | j                   sy 	 | j                         }|t        |      r
| d {    | j                          y 7 # t        $ r% t	        j
                  d| j                  d       Y @w xY w# | j                          w xY ww)NzException in callback %rT)exc_info)r.   r'   r    	Exceptionr"   errorr7   )r0   vals     r   _runzPeriodicCallback._run   s{     ==&mmo?{3'7II ##%	  X8$--RVWX ##%sC   B"A A	A B	A +A96A< 8A99A< <BBc                    | j                   r_| j                  | j                  j                                | j                  j	                  | j
                  | j                        | _        y y )N)r.   _update_nextr4   r5   add_timeoutr6   rD   r/   r8   s    r   r7   zPeriodicCallback._schedule_next   sK    }}!!$,,"3"3"56 $ 8 89K9KTYY W r   c                T   | j                   dz  }| j                  r*|d| j                  t        j                         dz
  z  z   z  }| j                  |k  r?| xj                  t	        j
                  || j                  z
  |z        dz   |z  z  c_        y | xj                  |z  c_        y )Ng     @@r%   g      ?)r+   r-   r   r6   mathfloor)r0   current_timecallback_time_secs      r   rF   zPeriodicCallback._update_next   s     $ 2 2V ;{{!Q$++39N*O%PP!!!\1
 ""JJt/A/A AEVVW%'& &"& ""&77"r   N)r   )r'   zCallable[[], Awaitable | None]r+   zdatetime.timedelta | floatr-   floatreturnNone)rN   rO   )rN   bool)rK   rM   rN   rO   )__name__
__module____qualname____doc__r1   r9   r<   r>   rD   r7   rF    r   r   r	   r	   ;   sW    #	R 		!4	! 6	! 		!
 	!&	"	%	!
	&	X
	8r   r	   _T)      )rW      Fdebugloop_factoryc               ~    t        j                  ||      5 }|j                  |       cd d d        S # 1 sw Y   y xY w)NrZ   )r   Runnerrun)mainr[   r\   runners       r   asyncio_runrb      s-     ^^%lCv::d# DCCs   3<c                  	 t        j                          t        d      # t        $ r Y nw xY wt        j                  |       st	        d|       |t        j
                         }n |       }	 |t        j                  |       ||j                  |       |j                  |       	 t        |       |j                  |j                                |j                  |j                                |t        j                  d        |j                          S # |t        j                  d        |j                          w xY w# 	 t        |       |j                  |j                                |j                  |j                                |t        j                  d        |j                          w # |t        j                  d        |j                          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iscoroutiner,   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   rb   rb      s   	$$& J   		 ""4(=dXFGG))+D>D	#&&t, u%**40!$'''(?(?(AB''(F(F(HI'**40

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

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

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

s>   ! 	--2:E
 -A	D)E
G)A	F=(G)=)G&&G)c                B   t        j                  |       }|sy |D ]  }|j                           | j                  t        j                  |ddi       |D ]G  }|j                         r|j                         %| j                  d|j                         |d       I y )Nreturn_exceptionsTz1unhandled exception during asyncio.run() shutdown)message	exceptiontask)r   	all_taskscancelrj   gather	cancelledrs   call_exception_handler)ro   	to_cancelrt   s      r   rk   rk      s    %%d+	DKKM  		 RT RSD~~~~+++#V%)^^%5 $	 r   )r`   zCoroutine[Any, Any, _T]r[   rP   r\   z.Callable[[], asyncio.AbstractEventLoop] | NonerN   rV   )ro   zasyncio.AbstractEventLooprN   rO   ))
__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	   r)   rI   r   inspectr    r!   tornado.logr"   rV   r_   rb   rk   rU   r   r   <module>r      s^   "    
  /  
I,.) .   W)) *   W)) * 
,,'
!.- <'/ )#%#8 8D T]w++K 
 GK		$%	$ 	$ E		$
 
	$" GK	$%$ $ E	$
 
$Lr   