B
    2dW                 @   st   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 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.
    N)Bool)default)Dict)Float)Instance)Integer)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 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_deadc             C   s   t   S )N)time)self r   e/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/jupyter_client/restarter.py_default_last_dead;   s    z"KernelRestarter._default_last_deadc             C   s   t g g dS )N)restartdead)dict)r   r   r   r   _callbacks_defaultA   s    z"KernelRestarter._callbacks_defaultc             C   s   t ddS )z Start the polling of the kernel.z!Must be implemented in a subclassN)NotImplementedError)r   r   r   r   startD   s    zKernelRestarter.startc             C   s   t ddS )zStop the kernel polling.z!Must be implemented in a subclassN)r   )r   r   r   r   stopH   s    zKernelRestarter.stopr   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   feventr   r   r   add_callbackL   s    	zKernelRestarter.add_callbackc             C   s.   y| 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
ValueError)r   r   r   r   r   r   remove_callbackW   s    	zKernelRestarter.remove_callbackc          
   C   sL   xF| j | D ]8}y
|  W q tk
rB   | jjd||dd Y qX qW 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warningr'   r   random_ports_until_alive_initial_startupinfoZrestart_kernelstable_start_timeZprovisionerZget_stable_start_time)r   r*   r+   r6   r   r   r   pollr   sF    



	
zKernelRestarter.pollN)r   )r   )__name__
__module____qualname____doc__r   r-   r   r,   r   Ztime_to_deadr6   r   r1   r3   r/   r0   r4   r   r   r   r   r   r   r   r   r   r!   r'   r7   r   r   r   r   r	      s@   

r	   )r;   r   Z	traitletsr   r   r   r   r   r   Ztraitlets.config.configurabler   r	   r   r   r   r   <module>   s   