U
    luf                     @  s`   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 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                   @  s  e Zd ZdZedZeddddZeddddZ	ed	dd
dZ
eddddZeddddZedZedZedZe ZedddddZe ZddddZddddZddddZd+ddddd d!Zd,ddddd"d#Zd$dd%d&d'Zdd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float)returnc                 C  s   t   S )N)timeself r   </tmp/pip-unpacked-wheel-naub1w99/jupyter_client/restarter.py_default_last_dead9   s    z"KernelRestarter._default_last_deadzdict[str, list]c                 C  s
   g g dS )N)restartdeadr   r   r   r   r   _callbacks_default?   s    z"KernelRestarter._callbacks_defaultNonec                 C  s   d}t |dS )z Start the polling of the kernel.!Must be implemented in a subclassNNotImplementedErrorr   msgr   r   r   startB   s    zKernelRestarter.startc                 C  s   d}t |dS )zStop the kernel polling.r   Nr   r   r   r   r   stopG   s    zKernelRestarter.stopr   zt.Callable[..., t.Any]str)feventr   c                 C  s   | 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$   r   r   r   add_callbackL   s    	zKernelRestarter.add_callbackc                 C  s.   z| j | | W n tk
r(   Y nX dS )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'   r   r   r   remove_callbackW   s    	zKernelRestarter.remove_callbackzt.Any)r$   r   c              
   C  sH   | j | D ]8}z
|  W q
 tk
r@   | jjd||dd Y q
X q
dS )z)fire our callbacks for a particular eventz&KernelRestarter: %s callback %r failedT)exc_infoN)r%   	Exceptionlogerror)r   r$   callbackr   r   r   _fire_callbackse   s    
zKernelRestarter._fire_callbacksc                 C  sN  | j r| j d | jjr*| j d d S t }| j s|| _| jrX|  jd7  _nd| _| j| j	kr| j
d | d d| _d| _|   nJ| jo| j}| jd| j| j	|rd	nd
 | d | jjd|d d| _nh| j}| jjr| jjj|d}| jr || j |kr d| _| jrJ|| j |krJ| j d d| _d S )NzPolling kernel...zKernel shutdown in progress...   zKernelRestarter: restart failedr   Fr   z;KernelRestarter: restarting kernel (%i/%i), %s random portsnewZkeepr   T)nownewports)Zrecommendedz-KernelRestarter: restart apparently succeeded)debugr.   kernel_managerZshutting_downr   is_aliver   _restarting_restart_countrestart_limitwarningr1   r!   random_ports_until_alive_initial_startupinfoZrestart_kernelstable_start_timeZprovisionerZget_stable_start_time)r   r4   r5   r@   r   r   r   pollr   sJ    




	
zKernelRestarter.pollN)r   )r   )__name__
__module____qualname____doc__r   r7   r   r6   r   Ztime_to_deadr@   r   r;   r=   r9   r:   r>   r   r   r   r   r%   r   r    r!   r(   r+   r1   rA   r   r   r   r   r
      sJ   	r
   )rE   
__future__r   r   typingtZ	traitletsr   r   r   r   r   r   Ztraitlets.config.configurabler	   r
   r   r   r   r   <module>   s   	 