
    Mh                    n    d Z ddlmZ ddlZddlZddlmZmZm	Z	m
Z
mZmZ ddlmZ  G d de          ZdS )zA basic kernel monitor with autorestarting.

This watches a kernel's state using KernelManager.is_alive and auto
restarts the kernel if it dies.

It is an incomplete base class, and must be subclassed.
    )annotationsN)BoolDictFloatInstanceIntegerdefault)LoggingConfigurablec                     e Zd ZdZ ed          Z eddd          Z eddd          Z	 ed	dd
          Z
 eddd          Z eddd          Z ed          Z ed          Z ed          Z e            Z ed          d#d            Z e            Zd$dZd%dZd%dZd&d'dZd&d'dZd(d Zd%d!Zd"S ))KernelRestarterz!Monitor and autorestart a kernel.zjupyter_client.KernelManagerFTzWhether to include every poll event in debugging output.

        Has to be set explicitly, because there will be *a lot* of output.
        )confighelpg      @z%Kernel heartbeat interval in seconds.g      $@zOThe time in seconds to consider the kernel to have completed a stable start up.   zJThe number of consecutive autorestarts before the kernel is presumed dead.zNWhether to choose new random ports when restarting before the kernel is alive.r   
_last_deadreturnfloatc                (    t          j                     S )N)timeselfs    X/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/jupyter_client/restarter.py_default_last_deadz"KernelRestarter._default_last_dead9   s    y{{    dict[str, list]c                    g g dS )N)restartdead r   s    r   _callbacks_defaultz"KernelRestarter._callbacks_default?   s    r***r   Nonec                $    d}t          |          )z Start the polling of the kernel.!Must be implemented in a subclassNotImplementedErrorr   msgs     r   startzKernelRestarter.startB       1!#&&&r   c                $    d}t          |          )zStop the kernel polling.r"   r#   r%   s     r   stopzKernelRestarter.stopG   r(   r   r   ft.Callable[..., t.Any]eventstrc                F    | j         |                             |           dS )zregister a callback to fire on a particular event

        Possible values for event:

          'restart' (default): kernel has died, and will be restarted.
          'dead': restart has failed, kernel will be left dead.

        N)	callbacksappendr   r+   r-   s      r   add_callbackzKernelRestarter.add_callbackL   s%     	u$$Q'''''r   c                j    	 | j         |                             |           dS # t          $ r Y dS w xY w)zunregister a callback to fire on a particular event

        Possible values for event:

          'restart' (default): kernel has died, and will be restarted.
          'dead': restart has failed, kernel will be left dead.

        N)r0   remove
ValueErrorr2   s      r   remove_callbackzKernelRestarter.remove_callbackW   sJ    	N5!((+++++ 	 	 	DD	s    $ 
22t.Anyc                    | j         |         D ];}	  |             # t          $ r! | j                            d||d           Y 8w xY wdS )z)fire our callbacks for a particular eventz&KernelRestarter: %s callback %r failedT)exc_infoN)r0   	Exceptionlogerror)r   r-   callbacks      r   _fire_callbackszKernelRestarter._fire_callbackse   s    u- 		 		H



   <!	      		 		s   
(AAc                   | j         r| j                             d           | j        j        r| j                             d           d S t	          j                    }| j                                        s|| _        | j        r| xj        dz  c_        nd| _        | j        | j	        k    rS| j        
                    d           |                     d           d| _        d| _        |                                  d S | j        o| j        }| j                            d| j        | j	        |rd	nd
           |                     d           | j                            d|           d| _        d S | j        }| j        j        r | j        j                            |          }| j        r|| j        z
  |k    rd| _        | j        r1|| j        z
  |k    r%| j                             d           d| _        d S d S d S )NzPolling kernel...zKernel shutdown in progress...   zKernelRestarter: restart failedr   Fr   z;KernelRestarter: restarting kernel (%i/%i), %s random portsnewkeepr   T)nownewports)recommendedz-KernelRestarter: restart apparently succeeded)debugr<   kernel_managershutting_downr   is_aliver   _restarting_restart_countrestart_limitwarningr?   r*   random_ports_until_alive_initial_startupinforestart_kernelstable_start_timeprovisionerget_stable_start_time)r   rD   rE   rS   s       r   pollzKernelRestarter.pollr   s1   : 	0HNN.///, 	HNN;<<<Fikk"++-- )	)!DO (##q(###&'#"T%777  !BCCC$$V,,,#( &'#		8RT=RQ'&%1EE6	   $$Y///#22th2OOO#'    !% 6". $($7$C$Y$Y 1 %Z % %! $ .t)>BS)S)S(-% )C$/$9=N$N$NNOOO#(   ) )$N$Nr   N)r   r   )r   r   )r   r    )r   )r+   r,   r-   r.   r   r    )r-   r8   r   r    )__name__
__module____qualname____doc__r   rH   r   rG   r   time_to_deadrS   r   rM   rO   rK   rL   rP   r   r	   r   r   r0   r   r'   r*   r3   r7   r?   rV   r   r   r   r   r      s       ++X<==ND  E 5T0[\\\Lb   G	]  M  $ta     
 $u++KWQZZNtDzzJW\    I+ + + +' ' ' '
' ' ' '
	( 	( 	( 	( 	(       0) 0) 0) 0) 0) 0)r   r   )rZ   
__future__r   r   typingt	traitletsr   r   r   r   r   r	   traitlets.config.configurabler
   r   r   r   r   <module>ra      s     # " " " " "      C C C C C C C C C C C C C C C C = = = = = =O) O) O) O) O)) O) O) O) O) O)r   