
    NeW                     z    d 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 ddlmZ ddlmZ dd	l	m
Z
  G d
 de
      Zy)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.
    N)Bool)default)Dict)Float)Instance)Integer)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        Z e       Zd Zd Zd ZddZddZd Zd Zy)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_deadc                 *    t        j                          S )N)timeselfs    :/usr/lib/python3/dist-packages/jupyter_client/restarter.py_default_last_deadz"KernelRestarter._default_last_dead;   s    yy{    c                     t        g g       S )N)restartdead)dictr   s    r   _callbacks_defaultz"KernelRestarter._callbacks_defaultA   s    BR((r   c                     t        d      )z Start the polling of the kernel.!Must be implemented in a subclassNotImplementedErrorr   s    r   startzKernelRestarter.startD       !"EFFr   c                     t        d      )zStop the kernel polling.r   r   r   s    r   stopzKernelRestarter.stopH   r!   r   c                 @    | j                   |   j                  |       y)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   fevents      r   add_callbackzKernelRestarter.add_callbackL   s     	u$$Q'r   c                 `    	 | j                   |   j                  |       y# t        $ r Y y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)r%   remove
ValueErrorr'   s      r   remove_callbackzKernelRestarter.remove_callbackW   s0    	NN5!((+ 		s   ! 	--c                     | j                   |   D ]
  }	  |         y# t        $ r" | j                  j                  d||d       Y 8w xY w)z)fire our callbacks for a particular eventz&KernelRestarter: %s callback %r failedT)exc_infoN)r%   	Exceptionlogerror)r   r)   callbacks      r   _fire_callbackszKernelRestarter._fire_callbackse   sV    u- 		H
		  <!	  s   (A	A	c                    | j                   r| j                  j                  d       | j                  j                  r| j                  j                  d       y t	        j                         }| j                  j                         s|| _        | j                  r| xj                  dz  c_        nd| _        | j                  | j                  kD  rK| j                  j                  d       | j                  d       d| _        d| _        | j                          y | j                  xr | j                  }| j                  j                  d| j                  | j                  |rd	nd
       | j                  d       | j                  j!                  d|       d| _        y | j"                  }| j                  j$                  r&| j                  j$                  j'                  |      }| j                  r|| j                  z
  |k\  rd| _        | j                  r6|| j                  z
  |k\  r#| j                  j                  d       d| _        y y y )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)debugr2   kernel_managershutting_downr   is_aliver   _restarting_restart_countrestart_limitwarningr5   r#   random_ports_until_alive_initial_startupinforestart_kernelstable_start_timeprovisionerget_stable_start_time)r   r:   r;   rI   s       r   pollzKernelRestarter.pollr   s   ::HHNN./,,HHNN;<iik""++-!DO##q(#&'#""T%7%77  !BC$$V,#( &'#		88RT=R=RQ''&&%E6	 $$Y/##22th2O#'  !% 6 6""..$($7$7$C$C$Y$Y 1 %Z %! $$t)>BS)S(-%C$//$9=N$NNO#(  %Or   N)r   )__name__
__module____qualname____doc__r   r>   r   r=   r   time_to_deadrI   r   rC   rE   rA   rB   rF   r   r   r   r   r%   r   r    r#   r*   r.   r5   rL    r   r   r   r      s    +<=NE T0[\Lb 	]M  $a 
 u+KQZNDzJ\  I)GG	(0)r   r   )rP   r   	traitletsr   r   r   r   r   r   traitlets.config.configurabler	   r   rR   r   r   <module>rU      s3           =M)) M)r   