B
    2dh                 @   s@  d Z ddlZddlZddlZddlZddlZddlZddl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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 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% 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.ej/dej0dejf dZ1ej2eef dd d!Z3e1e1d"d#d$Z4G d%d& d&e!Z5G d'd( d(e5Z6e#7e5 d3e8e9ejej:e5e,f d+d,d-Z;d4e8e9ejej:e6e,f d+d.d/Z<eejej=e, d0d1d2Z>dS )5z%Base class to manage a running kernel    N)Future)contextmanager)Enum)Any)Bool)default)DottedObjectName)Float)Instance)observe)observe_compat)Type)Unicode)import_item   )ConnectionFileMixin)KernelManagerABC)KernelProvisionerBase)KernelProvisionerFactory)ensure_async)run_sync)KernelClient)
kernelspecc               @   s    e Zd ZdZdZdZdZdZdS )_ShutdownStatusz

    This is so far used only for testing in order to track the internal state of
    the shutdown logic, and verifying which path is taken for which
    missbehavior.

    NShutdownRequestSigtermRequestSigkillRequest)__name__
__module____qualname____doc__Unsetr   r   r    r"   r"   c/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/jupyter_client/manager.pyr   (   s
   r   F.)bound)returnc               C   s*   yt   t S  tk
r$   t S X dS )z Get an appropriate Future objectN)asyncioget_running_loopr   RuntimeErrorCFuturer"   r"   r"   r#   _get_future:   s
    r+   )methodr&   c                s(   t jt  fdd}t t|S )zSets the kernel to a pending state by
    creating a fresh Future for the KernelManager's `ready`
    attribute. Once the method is finished, set the Future's results.
    c          
      s   | j s| jst | _y6 | f||I d H }tdI d H  | jd  |S  tk
r } z&| j| | j	| j	  |W d d }~X Y nX d S )Ng{Gz?)
_attempted_start_readyr+   r'   sleep
set_result	Exceptionset_exceptionlog	exception)selfargskwargsoute)r,   r"   r#   wrapperJ   s    z!in_pending_state.<locals>.wrapper)tno_type_check	functoolswrapscastr$   )r,   r:   r"   )r,   r#   in_pending_stateD   s    r@   c                   s  e Zd ZU dZejejeef  e	d<  fddZ
edZee	d< eejZee	d< edejdd	d
ZedZee	d< eddZee	d< ededddZedejeef ddddZedddZee	d< dZeje e	d< ee j!Z"ee	d< ede j!dddZ#ede$ejeef ddddZ%e&ddd d!Z'e&e	d"< ee j(Z)ee	d#< ed#ejeef ddd$d%Z*dZ+eje j, e	d&< e-eje j, dd'd(Z.ed)d*Z/ee	d+< ed+e0dd,d-Z1e-ejeef dd.d/Z2e-e0dd0d1Z3e4 Z5e4e	d2< e4 Z6e4e	d3< e4 Z7e4e	d4< eddd5d!Z8ee	d6< dZ9e0e	d7< ddd8d9Z:ddd:d;Z;ddd<d=Z<dwej=edd?d@dAZ>dxej=edd?dBdCZ?ej4e@dDdEdFZAdyejejBe  ejBe dGdHdIZCejBe ej4ddJdKdLZDeEeDZFdddMdNZGdddOdPZHej4ejIejBe ejeej4f f dQdRdSZJeEeJZKej4ddQdTdUZLeEeLZMeNej4ddQdVdWZOeEeOZPdze0ddXdYdZZQeEeQZRd{ejeS eSeje0 dd\d]d^ZTeEeTZUd|e0ddXd_d`ZVeEeVZWeNd}e0e0ddadbdcZXeEeXZYd~e0e0ej4ddddedfZZeEeZZ[e-e0ddgdhZ\de0ddXdidjZ]eEe]Z^de0ddXdkdlZ_eEe_Z`dddmdnZaeEeaZbecddodpdqZdeEedZee0ddrdsZfeEefZgdeSddtdudvZh  ZiS )KernelManagerzgManages a single kernel in a subprocess on this host.

    This version starts kernels with Popen.
    r.   c                s&   t  jf | tj| _d| _d | _d S )NF)super__init__r   r!   _shutdown_statusr-   r.   )r5   r6   r7   )	__class__r"   r#   rC   g   s    zKernelManager.__init__F_created_contextcontext)r&   c             C   s   d| _ t S )NT)rF   zmqContext)r5   r"   r"   r#   _context_defaultr   s    zKernelManager._context_defaultz,jupyter_client.blocking.BlockingKernelClientclient_classzjupyter_client.KernelClient)klassclient_factoryc             C   s
   t | jS )N)r   rK   )r5   r"   r"   r#   _client_factory_default}   s    z%KernelManager._client_factory_defaultN)changer&   c             C   s   t t|d | _d S )Nnew)r   strrM   )r5   rO   r"   r"   r#   _client_class_changed   s    z#KernelManager._client_class_changedT)
allow_none	kernel_idprovisionerkernel_spec_managerc             C   s   t j| jdS )N)data_dir)r   KernelSpecManagerrW   )r5   r"   r"   r#   _kernel_spec_manager_default   s    z*KernelManager._kernel_spec_manager_defaultc             C   s
   d | _ d S )N)_kernel_spec)r5   rO   r"   r"   r#   _kernel_spec_manager_changed   s    z*KernelManager._kernel_spec_manager_changedg      @a  Time to wait for a kernel to terminate before killing it, in seconds. When a shutdown request is initiated, the kernel will be immediately sent an interrupt (SIGINT), followedby a shutdown_request message, after 1/2 of `shutdown_wait_time`it will be sent a terminate (SIGTERM) request, and finally at the end of `shutdown_wait_time` will be killed (SIGKILL). terminate and kill may be equivalent on windows.  Note that this value can beoverridden by the in-use kernel provisioner since shutdown times mayvary by provisioned environment.)confighelpshutdown_wait_timekernel_namec             C   s   d | _ |d dkrtj| _d S )NrP   python)rZ   r   NATIVE_KERNEL_NAMEr_   )r5   rO   r"   r"   r#   _kernel_name_changed   s    z"KernelManager._kernel_name_changedrZ   c             C   s*   | j d kr$| jdkr$| j| j| _ | j S )N )rZ   r_   rV   Zget_kernel_spec)r5   r"   r"   r#   kernel_spec   s    zKernelManager.kernel_speczQTrue if the MultiKernelManager should cache ports for this KernelManager instance)r]   cache_portsc             C   s
   | j dkS )NZtcp)	transport)r5   r"   r"   r#   _default_cache_ports   s    z"KernelManager._default_cache_portsc             C   s   | j st | _ | j S )zMA future that resolves when the kernel process has started for the first time)r.   r+   )r5   r"   r"   r#   ready   s    zKernelManager.readyc             C   s
   | j dkS )N>   r`   python2python3)r_   )r5   r"   r"   r#   	ipykernel   s    zKernelManager.ipykernel_launch_args_control_socket
_restarterz,Should we autorestart the kernel if it dies.autorestartshutting_downc             C   s   |    |   d S )N)_close_control_socketcleanup_connection_file)r5   r"   r"   r#   __del__   s    zKernelManager.__del__c             C   s   d S )Nr"   )r5   r"   r"   r#   start_restarter   s    zKernelManager.start_restarterc             C   s   d S )Nr"   )r5   r"   r"   r#   stop_restarter   s    zKernelManager.stop_restarterrestart)callbackeventr&   c             C   s    | j dkrdS | j || dS )z;register a callback to be called when a kernel is restartedN)rn   Zadd_callback)r5   rw   rx   r"   r"   r#   add_restart_callback   s    
z"KernelManager.add_restart_callbackc             C   s    | j dkrdS | j || dS )z=unregister a callback to be called when a kernel is restartedN)rn   Zremove_callback)r5   rw   rx   r"   r"   r#   remove_restart_callback   s    
z%KernelManager.remove_restart_callback)r7   r&   c             K   s@   i }| | jdd | t| j| d | | | jf |S )z3Create a client configured to connect to our kernelT)session)connection_fileparent)updateZget_connection_infodictr|   rM   )r5   r7   kwr"   r"   r#   client   s    

zKernelManager.client)extra_argumentsr&   c                s   |pg }| j dk	st| j j| }|rZ|d ddtjd  dtjdd  hkrZtj|d< ttj	| j
tjd| j r| j jd< | j td	fd
d  fdd|D S )z/replace templated args (e.g. {connection_file})Nr   r`   zpython%izpython%i.%i   )r|   prefixresource_dirz\{([A-Za-z0-9_]+)\}c                s     | d|  S )z9Get the key out of ns if it's there, otherwise no change.r   )getgroup)match)nsr"   r#   from_ns)  s    z0KernelManager.format_kernel_cmd.<locals>.from_nsc                s   g | ]}  |qS r"   )sub).0arg)r   patr"   r#   
<listcomp>-  s    z3KernelManager.format_kernel_cmd.<locals>.<listcomp>)rd   AssertionErrorargvsysversion_info
executabler   ospathrealpathr|   r   r   r~   rl   recompile)r5   r   cmdr"   )r   r   r   r#   format_kernel_cmd  s"    



zKernelManager.format_kernel_cmd)
kernel_cmdr   r&   c                s>   | j dk	st| j j|f|I dH }| j js0t| | dS )zactually launch the kernel

        override in a subclass to launch kernel subprocesses differently
        Note that provisioners can now be used to customize kernel environments
        and
        N)rU   r   Zlaunch_kernelhas_processZ_reconcile_connection_info)r5   r   r   Zconnection_infor"   r"   r#   _async_launch_kernel/  s    z"KernelManager._async_launch_kernelc             C   s"   | j d kr| d| _ d| j _d S )Ncontrold   )rm   Z_create_connected_socketlinger)r5   r"   r"   r#   _connect_control_socketA  s    
z%KernelManager._connect_control_socketc             C   s"   | j d krd S | j   d | _ d S )N)rm   close)r5   r"   r"   r#   rq   F  s    

z#KernelManager._close_control_socket)r   r&   c                s|   d| _ | jp|dtt | _| | _| jdkrVt	j
| jdj| j| j| d| _| jjf |I dH }|d}||fS )a{  Prepares a kernel for startup in a separate process.

        If random ports (port=0) are being used, this method must be called
        before the channels are created.

        Parameters
        ----------
        `**kw` : optional
             keyword arguments that are passed down to build the kernel_cmd
             and launching the kernel (e.g. Popen kwargs).
        FrT   N)r}   r   )rp   rT   poprQ   uuiduuid4copyrl   rU   KPFinstancer}   Zcreate_provisioner_instancerd   Z
pre_launch)r5   r   r   r"   r"   r#   _async_pre_start_kernelL  s    



z%KernelManager._async_pre_start_kernelc                s6   |    |   | jdk	st| jjf |I dH  dS )zPerforms any post startup tasks relative to the kernel.

        Parameters
        ----------
        `**kw` : optional
             keyword arguments that were used in the kernel process's launch.
        N)rt   r   rU   r   Zpost_launch)r5   r   r"   r"   r#   _async_post_start_kernelj  s    z&KernelManager._async_post_start_kernelc                s`   d| _ t| jf |I dH \}}| jd| t| j|f|I dH  t| jf |I dH  dS )az  Starts a kernel on this host in a separate process.

        If random ports (port=0) are being used, this method must be called
        before the channels are created.

        Parameters
        ----------
        `**kw` : optional
             keyword arguments that are passed down to build the kernel_cmd
             and launching the kernel (e.g. Popen kwargs).
        TNzStarting kernel: %s)r-   r   pre_start_kernelr3   debug_launch_kernelpost_start_kernel)r5   r   r   r"   r"   r#   _async_start_kernely  s
    z!KernelManager._async_start_kernel)rv   r&   c                s`   t |d}| jjd|d}|   | j| j| | jdk	s@t| jj|dI dH  t	j
| _dS )z+Send a shutdown request via control channel)rv   shutdown_request)contentN)r   r{   msgr   sendrm   rU   r   Zshutdown_requestedr   r   rD   )r5   rv   r   r   r"   r"   r#   _async_request_shutdown  s    
z%KernelManager._async_request_shutdown皙?)waittimepollintervalrv   r&   c                s  |dkrt | jd}| jr(| jj|d}y$tj| j|d|d dI dH  W n< tjk
r   | j	d t
j| _t|  I dH  Y nX y$tj| j|d|d dI dH  W n@ tjk
r   | j	d t
j| _t| j|d	I dH  Y n*X | jr| jdk	st| j I dH  dS )
zWait for kernel shutdown, then kill process if it doesn't shutdown.

        This does not send shutdown requests - use :meth:`request_shutdown`
        first.
        Nr   )Zrecommended)r   r   )timeoutz0Kernel is taking too long to finish, terminatingz,Kernel is taking too long to finish, killing)rv   )maxr^   rU   Zget_shutdown_wait_timer'   wait_for_async_waitTimeoutErrorr3   r   r   r   rD   r   _send_kernel_sigtermr   _kill_kernel
has_kernelr   wait)r5   r   r   rv   r"   r"   r#   _async_finish_shutdown  s*      z$KernelManager._async_finish_shutdownc                sZ   |s|    |   |   d| j_| jr<|s<| jjdd | jrV| jj	|dI dH  dS )z/Clean up resources when the kernel is shut downNr   )r   )rv   )
rr   Zcleanup_ipc_filesrq   r{   r}   rF   rG   destroyrU   cleanup)r5   rv   r"   r"   r#   _async_cleanup_resources  s    
z&KernelManager._async_cleanup_resources)nowrv   r&   c                s   d| _ |   | jr&t|  I dH  |r>t|  I dH  n,t| j|dI dH  t| j|dI dH  t| j|dI dH  dS )aZ  Attempts to stop the kernel process cleanly.

        This attempts to shutdown the kernels cleanly by:

        1. Sending it a shutdown message over the control channel.
        2. If that fails, the kernel is shutdown forcibly by sending it
           a signal.

        Parameters
        ----------
        now : bool
            Should the kernel be forcible killed *now*. This skips the
            first, nice shutdown attempt.
        restart: bool
            Will this kernel be restarted after it is shutdown. When this
            is True, connection files will not be cleaned up.
        TN)rv   )	rp   ru   r   r   interrupt_kernelr   request_shutdownfinish_shutdowncleanup_resources)r5   r   rv   r"   r"   r#   _async_shutdown_kernel  s    z$KernelManager._async_shutdown_kernel)r   newportsr   r&   c                s^   | j dkrtdt| j|ddI dH  |r6|   | j | t| jf | j I dH  dS )ay  Restarts a kernel with the arguments that were used to launch it.

        Parameters
        ----------
        now : bool, optional
            If True, the kernel is forcefully restarted *immediately*, without
            having a chance to do any cleanup action.  Otherwise the kernel is
            given 1s to clean up before a forceful restart is issued.

            In all cases the kernel is restarted, the only difference is whether
            it is given a chance to perform a clean shutdown or not.

        newports : bool, optional
            If the old kernel was launched with random ports, this flag decides
            whether the same ports and connection file will be used again.
            If False, the same ports and connection file are used. This is
            the default. If True, new random port numbers are chosen and a
            new connection file is written. It is still possible that the newly
            chosen random port numbers happen to be the same as the old ones.

        `**kw` : optional
            Any options specified here will overwrite those used to launch the
            kernel.
        Nz>Cannot restart the kernel. No previous call to 'start_kernel'.T)r   rv   )rl   r)   r   shutdown_kernelZcleanup_random_portsr~   start_kernel)r5   r   r   r   r"   r"   r#   _async_restart_kernel  s    
z#KernelManager._async_restart_kernelc             C   s   | j dk	o| j jS )z@Has a kernel process been started that we are actively managing.N)rU   r   )r5   r"   r"   r#   r   (  s    zKernelManager.has_kernelc                s,   | j r(| jdk	st| jj|dI dH  dS )zIsimilar to _kill_kernel, but with sigterm (not sigkill), but do not blockN)rv   )r   rU   r   	terminate)r5   rv   r"   r"   r#   _async_send_kernel_sigterm-  s    z(KernelManager._async_send_kernel_sigtermc                s   | j r~| jdk	st| jj|dI dH  ytj|  ddI dH  W n" tjk
rf   | j	d Y nX | j r~| j
 I dH  dS )zrKill the running kernel.

        This is a private method, callers should use shutdown_kernel(now=True).
        N)rv   g      @)r   z>Wait for final termination of kernel timed out - continuing...)r   rU   r   killr'   r   r   r   r3   warningr   )r5   rv   r"   r"   r#   _async_kill_kernel5  s    z KernelManager._async_kill_kernelc                sz   | j rn| jdk	st| jj}|dkr<t| tjI dH  qv|dkrv| jj	di d}| 
  | j| j| ntddS )zInterrupts the kernel by sending it a signal.

        Unlike ``signal_kernel``, this operation is well supported on all
        platforms.
        NsignalmessageZinterrupt_request)r   z.Cannot interrupt kernel. No kernel is running!)r   rd   r   interrupt_moder   signal_kernelr   SIGINTr{   r   r   r   rm   r)   )r5   r   r   r"   r"   r#   _async_interrupt_kernelL  s    z%KernelManager._async_interrupt_kernel)signumr&   c                s4   | j r(| jdk	st| j|I dH  ntddS )a  Sends a signal to the process group of the kernel (this
        usually includes the kernel and any subprocesses spawned by
        the kernel).

        Note that since only SIGTERM is supported on Windows, this function is
        only useful on Unix systems.
        Nz+Cannot signal kernel. No kernel is running!)r   rU   r   send_signalr)   )r5   r   r"   r"   r#   _async_signal_kernela  s    z"KernelManager._async_signal_kernelc                s4   | j r0| jdk	st| j I dH }|dkr0dS dS )z$Is the kernel process still running?NTF)r   rU   r   poll)r5   retr"   r"   r#   _async_is_aliveq  s    zKernelManager._async_is_alive)r   r&   c                s,   x&t |  I d H r&t|I d H  qW d S )N)r   is_aliver'   r/   )r5   r   r"   r"   r#   r   |  s    zKernelManager._async_wait)rv   )rv   )N)F)Nr   F)F)FF)FF)F)F)r   )jr   r   r   r    r;   OptionalUnionr   r*   __annotations__rC   r   rF   r
   rH   rI   rG   r   rJ   r   rK   r   rM   rN   r   DictrQ   rR   r   rT   rU   r   r   rX   rV   rY   r   r[   r	   r^   ra   r_   rb   rZ   Z
KernelSpecpropertyrd   re   boolrg   rh   rk   r   rl   rm   rn   ro   rp   rs   rt   ru   Callablery   rz   r   r   Listr   r   r   r   r   rq   Tupler   r   r   r   r@   r   r   r   r   floatr   r   r   r   r   r   r   restart_kernelr   r   r   r   r   r   r   intr   r   r   r   r   __classcell__r"   r"   )rE   r#   rA   _   s   

$**  "$
&	rA   c               @   s   e Zd ZU edZeed< eddZeed< ej	Z
ejZejZejZejZejZejZejZejZejZejZejZ ej!Z"ej#Z$dS )AsyncKernelManagerz-jupyter_client.asynchronous.AsyncKernelClientrK   )rL   rM   N)%r   r   r   r   rK   r   r   rM   rA   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   r"   r"   r#   r     s"   
r   <   r`   )startup_timeoutr_   r7   r&   c             K   sf   t |d}|jf | | }|  y|j| d W n& tk
r\   |  |   Y nX ||fS )z5Start a new kernel, and return its Manager and Client)r_   )r   )rA   r   r   start_channelswait_for_readyr)   stop_channelsr   )r   r_   r7   kmkcr"   r"   r#   start_new_kernel  s    
r   c                sx   t |d}|jf |I dH  | }|  y|j| dI dH  W n, tk
rn   |  | I dH   Y nX ||fS )z5Start a new kernel, and return its Manager and Client)r_   N)r   )r   r   r   r   r   r)   r   r   )r   r_   r7   r   r   r"   r"   r#   start_new_async_kernel  s    
r   )r7   r&   c           	   k   s4   t f | \}}z
|V  W d|  |jdd X dS )zContext manager to create a kernel in a subprocess.

    The kernel is shut down when the context exits.

    Returns
    -------
    kernel_client: connected KernelClient instance
    NT)r   )r   r   r   )r7   r   r   r"   r"   r#   
run_kernel  s
    

r   )r   r`   )r   r`   )?r    r'   r=   r   r   r   r   typingr;   r   Zasyncio.futuresr   concurrent.futuresr*   
contextlibr   enumr   rH   Z	traitletsr   r   r   r   r	   r
   r   r   r   r   Ztraitlets.utils.importstringr   connectr   Z
managerabcr   Zprovisioningr   r   r   utilsr   r   Zjupyter_clientr   r   r   TypeVarr   r$   r   r+   r@   rA   r   registerr   rQ   r   r   r   Iteratorr   r"   r"   r"   r#   <module>   sb   
    *


