a
    ø£BeÜ  ã                   @  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 )zÊA 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.KernelManagerFTz�Whether 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   ú[/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/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 )zçregister 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y&   Y n0 dS )zéunregister 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  sF   | j | D ]6}z
|ƒ  W q
 ty>   | jjd||dd� Y q
0 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	|rºd	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   	 