B
    \                 @   s@   d Z ddlmZ ddlmZmZmZmZ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.
    )LoggingConfigurable)InstanceFloatDictBoolIntegerc               @   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Ze
dZe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.
        )Zconfighelpg      @z%Kernel heartbeat interval in seconds.   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   c             C   s   t g g dS )N)restartdead)dict)self r   7lib/python3.7/site-packages/jupyter_client/restarter.py_callbacks_default.   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   start1   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   stop5   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_callback9   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_callbackD   s    	zKernelRestarter.remove_callbackc             C   sZ   xT| j | D ]F}y
|  W q tk
rP } z| jjd||dd W dd}~X 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   callbacker   r   r   _fire_callbacksR   s
    
zKernelRestarter._fire_callbacksc             C   s   | j r| j d | j s| jr2|  jd7  _nd| _| j| jkrp| jd | d d| _d| _| 	  q| j
oz| j}| jd| j| j|rdnd	 | d
 | jjd|d d| _n$| jrd| _| jr| j d d| _d S )NzPolling kernel...   zKernelRestarter: restart failedr   Fr   z;KernelRestarter: restarting kernel (%i/%i), %s random portsnewZkeepr   T)Znownewportsz-KernelRestarter: restart apparently succeeded)debugr   kernel_managerZis_alive_restarting_restart_countrestart_limitZwarningr#   r   random_ports_until_alive_initial_startupinfoZrestart_kernel)r   r&   r   r   r   pollZ   s2    



zKernelRestarter.pollN)r   )r   )__name__
__module____qualname____doc__r   r(   r   r'   r   Ztime_to_deadr   r+   r,   r)   r*   r-   r   r   r   r   r   r   r   r#   r/   r   r   r   r   r      s*   

r   N)
r3   Ztraitlets.config.configurabler   Z	traitletsr   r   r   r   r   r   r   r   r   r   <module>   s   