a
    ø£Be@s  ã                   @   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mZmZmZmZmZmZmZmZ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.m0Z1 G dd„ deƒZ2ej3dej4dejf d�Z5ej6eef dœdd„Z7e5e5dœdd„Z8G dd„ de+ƒZ9G dd „ d e9ƒZ:e- ;e9¡ d+e<e=ejej>e9e'f d#œd$d%„Z?d,e<e=ejej>e:e%f d#œd&d'„Z@eejejAe) d(œd)d*„ƒZBdS )-z%Base class to manage a running kernelé    N)ÚFuture)Úcontextmanager)ÚEnum)Úrun_sync)
ÚAnyÚBoolÚDottedObjectNameÚFloatÚInstanceÚTypeÚUnicodeÚdefaultÚobserveÚobserve_compat)Úimport_itemé   )Ú
kernelspec)ÚAsyncKernelClient)ÚBlockingKernelClient)ÚKernelClient)ÚConnectionFileMixin)ÚKernelManagerABC)ÚKernelProvisionerBase)ÚKernelProvisionerFactoryc                   @   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#   úY/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jupyter_client/manager.pyr   +   s
   r   ÚF.)Úbound©Úreturnc                   C   s.   zt  ¡  tƒ W S  ty(   tƒ  Y S 0 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 ˆ ¡t jt jt jt jdœ‡ fdd„ƒƒ}t  t|¡S )z¬Sets 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.
    )ÚselfÚargsÚkwargsr(   c              
   Ÿ   s¤   | j s| jstƒ | _zDˆ | g|¢R i |¤ŽI dH }t d¡I dH  | jrT| j d¡ |W S  tyž } z.| j |¡ | j	 
| j 
¡ ¡ |‚W Y d}~n
d}~0 0 dS )z)Create a future for the decorated method.Ng{®Gáz„?)Ú_attempted_startÚ_readyr-   r)   ÚsleepÚowns_kernelÚ
set_resultÚ	ExceptionÚset_exceptionÚlogÚ	exception)r/   r0   r1   ÚoutÚe©r.   r#   r$   ÚwrapperM   s    z!in_pending_state.<locals>.wrapper)ÚtÚno_type_checkÚ	functoolsÚwrapsr   Úcastr%   )r.   r>   r#   r=   r$   Úin_pending_stateG   s    $rD   c                       s  e Zd ZU dZejejeef  e	d< ej
ej
d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e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jeef 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jeef 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(„ƒZ0eddd)d!�Z1ee	d*< ed*ƒe2d
œd+d,„ƒZ3e/ejeef d
œd-d.„ƒZ4e/e2d
œd/d0„ƒZ5e
ƒ Z6e
e	d1< e
ƒ Z7e
e	d2< e
ƒ Z8e
e	d3< eddd4d!�Z9ee	d5< dZ:e2e	d6< dd
œd7d8„Z;dd
œd9d:„Z<dd
œd;d<„Z=d{ej>edd>œd?d@„Z?d|ej>edd>œdAdB„Z@ej
eAdCœdDdE„ZBejeef ddFœdGdH„ZCd}ejejDe  ejDe dIœdJdK„ZEejDe ej
ddLœdMdN„ZFeGeFƒZHdd
œdOdP„ZIdd
œdQdR„ZJej
ejKejDe ejeej
f f dSœdTdU„ZLeGeLƒZMej
ddSœdVdW„ZNeGeNƒZOePej
ddSœdXdY„ƒZQeGeQƒZRd~e2ddZœd[d\„ZSeGeSƒZTdejeU eUe2dd^œd_d`„ZVeGeVƒZWd€e2ddZœdadb„ZXeGeXƒZYePd�e2e2ddcœddde„ƒZZeGeZƒZ[d‚e2e2ej
ddfœdgdh„Z\eGe\ƒZ]e/e2d
œdidj„ƒZ^e/e2d
œdkdl„ƒZ_dƒe2ddZœdmdn„Z`eGe`ƒZad„e2ddZœdodp„ZbeGebƒZcdd
œdqdr„ZdeGedƒZeefddsœdtdu„ZgeGegƒZhe2d
œdvdw„ZieGeiƒZjd…eUddxœdydz„Zk‡  ZlS )†ÚKernelManagerzgManages a single kernel in a subprocess on this host.

    This version starts kernels with Popen.
    r3   N)r0   r1   r(   c                    s8   |  dd¡| _tƒ jf i |¤Ž tj| _d| _d| _dS )zInitialize a kernel manager.r5   TFN)	ÚpopÚ_owns_kernelÚsuperÚ__init__r   r"   Ú_shutdown_statusr2   r3   )r/   r0   r1   ©Ú	__class__r#   r$   rI   k   s
    zKernelManager.__init__FÚ_created_contextÚcontextr'   c                 C   s   d| _ t ¡ S ©NT)rM   ÚzmqÚContext©r/   r#   r#   r$   Ú_context_defaultx   s    zKernelManager._context_defaultz,jupyter_client.blocking.BlockingKernelClientÚclient_class©ÚklassÚclient_factoryc                 C   s
   t | jƒS ©N)r   rT   rR   r#   r#   r$   Ú_client_factory_defaultƒ   s    z%KernelManager._client_factory_default)Úchanger(   c                 C   s   t t|d ƒƒ| _d S )NÚnew)r   ÚstrrW   ©r/   rZ   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   ÚKernelSpecManagerrc   rR   r#   r#   r$   Ú_kernel_spec_manager_default”   s    z*KernelManager._kernel_spec_manager_defaultc                 C   s
   d | _ d S rX   )Ú_kernel_specr]   r#   r#   r$   Ú_kernel_spec_manager_changed˜   s    z*KernelManager._kernel_spec_manager_changedç      @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 )Nr[   Úpython)rf   r   ÚNATIVE_KERNEL_NAMErl   r]   r#   r#   r$   Ú_kernel_name_changed­   s    z"KernelManager._kernel_name_changedrf   c                 C   s*   | j d u r$| jdkr$| j | j¡| _ | j S )NÚ )rf   rl   rb   Zget_kernel_specrR   r#   r#   r$   Úkernel_specµ   s    zKernelManager.kernel_speczQTrue if the MultiKernelManager should cache ports for this KernelManager instanceÚcache_portsc                 C   s
   | j dkS )NZtcp)Ú	transportrR   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)r3   r-   rR   r#   r#   r$   ÚreadyÅ   s    zKernelManager.readyc                 C   s
   | j dv S )N>   rm   Úpython3Úpython2©rl   rR   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 rX   )Ú_close_control_socketÚcleanup_connection_filerR   r#   r#   r$   Ú__del__Ü   s    zKernelManager.__del__c                 C   s   dS )zStart the kernel restarter.Nr#   rR   r#   r#   r$   Ústart_restarterä   s    zKernelManager.start_restarterc                 C   s   dS )zStop the kernel restarter.Nr#   rR   r#   r#   r$   Ústop_restarterè   s    zKernelManager.stop_restarterÚrestart)ÚcallbackÚeventr(   c                 C   s    | j du rdS | j  ||¡ dS )z;Register a callback to be called when a kernel is restartedN)r|   Zadd_callback©r/   r…   r†   r#   r#   r$   Úadd_restart_callbackì   s    
z"KernelManager.add_restart_callbackc                 C   s    | j du rdS | j  ||¡ dS )z=Unregister a callback to be called when a kernel is restartedN)r|   Zremove_callbackr‡   r#   r#   r$   Úremove_restart_callbackò   s    
z%KernelManager.remove_restart_callback©r1   r(   c                 K   sB   i }|  | jdd�¡ |  | j| dœ¡ |  |¡ | jf i |¤ŽS )z3Create a client configured to connect to our kernelT)Úsession)Úconnection_fileÚparent)ÚupdateZget_connection_inforŒ   rW   )r/   r1   Úkwr#   r#   r$   Úclientü   s    þÿ
zKernelManager.client)Úenvr(   c                C   s   | j d  |¡ dS )a°  
        Allow to update the environment of a kernel manager.

        This will take effect only after kernel restart when the new env is
        passed to the new kernel.

        This is useful as some of the information of the current kernel reflect
        the state of the session that started it, and those session information
        (like the attach file path, or name), are mutable.

        .. version-added: 8.5
        r‘   N)rz   rŽ   )r/   r‘   r#   r#   r$   Ú
update_env  s    zKernelManager.update_env)Úextra_argumentsr(   c                    sÄ   |pg }| j dusJ ‚| j j| }|rZ|d ddtjd  dtjdd…  hv rZtj|d< tj | j¡tj	dœ‰| j r‚| j j
ˆd< ˆ | j¡ t d	¡‰tjtjd
œ‡fdd„‰ ‡ ‡fdd„|D ƒS )z/Replace templated args (e.g. {connection_file})Nr   rm   zpython%izpython%i.%ié   )rŒ   ÚprefixÚresource_dirz\{([A-Za-z0-9_]+)\})Úmatchr(   c                    s   ˆ   |  d¡|  ¡ ¡S )z9Get the key out of ns if it's there, otherwise no change.r   )ÚgetÚgroup)r—   )Únsr#   r$   Úfrom_nsB  s    z0KernelManager.format_kernel_cmd.<locals>.from_nsc                    s   g | ]}ˆ  ˆ |¡‘qS r#   )Úsub)Ú.0Úarg)r›   Úpatr#   r$   Ú
<listcomp>F  ó    z3KernelManager.format_kernel_cmd.<locals>.<listcomp>)rq   ÚargvÚsysÚversion_infoÚ
executableÚosÚpathÚrealpathrŒ   r•   r–   rŽ   rz   ÚreÚcompiler?   r   )r/   r“   Úcmdr#   )r›   rš   rŸ   r$   Úformat_kernel_cmd  s$    
ý
þ
zKernelManager.format_kernel_cmd)Ú
kernel_cmdr�   r(   c                 Ë   sB   | j dusJ ‚| j j|fi |¤ŽI dH }| j js4J ‚|  |¡ dS )zÉactually 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)ra   Zlaunch_kernelÚhas_processZ_reconcile_connection_info)r/   r­   r�   Zconnection_infor#   r#   r$   Ú_async_launch_kernelH  s    z"KernelManager._async_launch_kernelc                 C   s"   | j d u r|  d¡| _ d| j _d S )NÚcontroléd   )r{   Z_create_connected_socketÚlingerrR   r#   r#   r$   Ú_connect_control_socketZ  s    
z%KernelManager._connect_control_socketc                 C   s"   | j d u rd S | j  ¡  d | _ d S rX   )r{   ÚcloserR   r#   r#   r$   r   _  s    

z#KernelManager._close_control_socket)r�   r(   c                 Ë   s€   d| _ | jp| dtt ¡ ƒ¡| _| ¡ | _| jdu rVt	j
| jd�j| j| j| d�| _| jjf i |¤Ž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).
        Fr`   N)r�   r«   )r~   r`   rF   r\   ÚuuidÚuuid4Úcopyrz   ra   ÚKPFÚinstancer�   Zcreate_provisioner_instancerq   Z
pre_launch©r/   r�   r­   r#   r#   r$   Ú_async_pre_start_kernele  s    

ý
z%KernelManager._async_pre_start_kernelc                 Ë   s:   |   ¡  |  ¡  | jdusJ ‚| jjf i |¤ŽI dH  dS )zÏPerforms any post startup tasks relative to the kernel.

        Parameters
        ----------
        `**kw` : optional
             keyword arguments that were used in the kernel process's launch.
        N)r‚   r³   ra   Zpost_launch)r/   r�   r#   r#   r$   Ú_async_post_start_kernelƒ  s    z&KernelManager._async_post_start_kernelc                 Ë   s`   d| _ | jf i |¤ŽI dH \}}| j d|¡ | j|fi |¤ŽI dH  | jf i |¤Ž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)r2   r»   r9   Údebugr¯   r¼   rº   r#   r#   r$   Ú_async_start_kernel’  s
    z!KernelManager._async_start_kernel)r„   r(   c                 Ã   s^   d|i}| j jd|d�}|  ¡  | j  | j|¡ | jdus>J ‚| jj|d�I dH  tj| _	dS )z+Send a shutdown request via control channelr„   Úshutdown_request©ÚcontentN©r„   )
r‹   Úmsgr³   Úsendr{   ra   Zshutdown_requestedr   r   rJ   )r/   r„   rÁ   rÃ   r#   r#   r$   Ú_async_request_shutdown©  s    z%KernelManager._async_request_shutdownçš™™™™™¹?)ÚwaittimeÚpollintervalr„   r(   c                 Ã   s  |du rt | jdƒ}| jr(| jj|d�}z$tj| j|d�|d d�I dH  W n6 tjy‚   | j 	d¡ t
j| _|  ¡ I dH  Y n0 z$tj| j|d�|d d�I dH  W n: tjyâ   | j 	d¡ t
j| _| j|d	�I dH  Y n(0 | j�r
| jdusúJ ‚| j ¡ I dH  dS )
z¨Wait 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, killingrÂ   )Úmaxrk   ra   Zget_shutdown_wait_timer)   Úwait_forÚ_async_waitÚTimeoutErrorr9   r½   r   r   rJ   Ú_async_send_kernel_sigtermr   Ú_async_kill_kernelÚ
has_kernelÚwait)r/   rÇ   rÈ   r„   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²   rÂ   )
r€   Zcleanup_ipc_filesr   r‹   r�   rM   rN   Údestroyra   Úcleanup©r/   r„   r#   r#   r$   Ú_async_cleanup_resourcesß  s    
z&KernelManager._async_cleanup_resources)Únowr„   r(   c                 Ã   sz   | j s
dS d| _|  ¡  | jr,|  ¡ I dH  |r@|  ¡ I dH  n$| j|d�I dH  | j|d�I dH  | 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.
        NTrÂ   )	r5   r~   rƒ   rÑ   Ú_async_interrupt_kernelrÐ   rÅ   rÓ   r×   )r/   rØ   r„   r#   r#   r$   Ú_async_shutdown_kernelð  s    z$KernelManager._async_shutdown_kernel)rØ   Únewportsr�   r(   c                 Ë   s^   | j du rd}t|ƒ‚| j|dd�I dH  |r6|  ¡  | j  |¡ | jf i | 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Ø   r„   )rz   r+   rÚ   Zcleanup_random_portsrŽ   r¾   )r/   rØ   rÛ   r�   rÃ   r#   r#   r$   Ú_async_restart_kernel  s    
z#KernelManager._async_restart_kernelc                 C   s   | j S rX   )rG   rR   r#   r#   r$   r5   E  s    zKernelManager.owns_kernelc                 C   s   | j duo| j jS )z@Has a kernel process been started that we are actively managing.N)ra   r®   rR   r#   r#   r$   rÑ   I  s    zKernelManager.has_kernelc                 Ã   s,   | j r(| jdusJ ‚| jj|d�I dH  dS )zIsimilar to _kill_kernel, but with sigterm (not sigkill), but do not blockNrÂ   )rÑ   ra   Ú	terminaterÖ   r#   r#   r$   rÏ   N  s    z(KernelManager._async_send_kernel_sigtermc                 Ã   s€   | j r|| jdusJ ‚| jj|d�I dH  ztj|  ¡ dd�I dH  W n  tjyd   | j d¡ Y n0 | j r|| j 	¡ I dH  dS )zrKill the running kernel.

        This is a private method, callers should use shutdown_kernel(now=True).
        NrÂ   rh   rÉ   z>Wait for final termination of kernel timed out - continuing...)
rÑ   ra   Úkillr)   rÌ   rÍ   rÎ   r9   ÚwarningrÒ   rÖ   r#   r#   r$   rÐ   V  s    z KernelManager._async_kill_kernelc                 Ã   sÒ   | j sX| jdurXt| jtƒr8t t ttj	 | j¡¡}n| j}| j
rN|I dH  |I dH  | j rÂ| jduslJ ‚| jj}|dkr�|  tj¡I dH  qÎ|dkrÎ| jjdi d�}|  ¡  | j | j|¡ nd}t|ƒ‚dS )z”Interrupts the kernel by sending it a signal.

        Unlike ``signal_kernel``, this operation is well supported on all
        platforms.
        NÚsignalÚmessageZinterrupt_requestrÀ   z.Cannot interrupt kernel. No kernel is running!)rÑ   r3   Ú
isinstancer,   r)   Úensure_futurer?   rC   r   r   r~   rq   Úinterrupt_modeÚ_async_signal_kernelrà   ÚSIGINTr‹   rÃ   r³   rÄ   r{   r+   )r/   ru   rä   rÃ   r#   r#   r$   rÙ   m  s$    

z%KernelManager._async_interrupt_kernel)Úsignumr(   c                 Ã   s8   | j r(| jdusJ ‚| j |¡I dH  nd}t|ƒ‚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Ñ   ra   Úsend_signalr+   )r/   rç   rÃ   r#   r#   r$   rå   Ž  s
    z"KernelManager._async_signal_kernelc                 Ã   s>   | j s
dS | jr:| jdusJ ‚| j ¡ I dH }|du r:dS dS )z$Is the kernel process still running?TNF)r5   rÑ   ra   Úpoll)r/   Úretr#   r#   r$   Ú_async_is_aliveŸ  s    zKernelManager._async_is_alive)rÈ   r(   c                 Ã   s$   |   ¡ I d H r t |¡I d H  q d S rX   )rë   r)   r4   )r/   rÈ   r#   r#   r$   rÍ   ­  s    zKernelManager._async_wait)r„   )r„   )N)F)NrÆ   F)F)FF)FF)F)F)rÆ   )mr   r   r    r!   r?   ÚOptionalÚUnionr   r,   Ú__annotations__r   rI   r   rM   r
   rP   rQ   rN   r   rS   r   rT   r   r   rW   rY   r   ÚDictr\   r^   r   r`   ra   r   r   rd   rb   re   r   rg   r	   rk   rn   rl   ro   rf   Z
KernelSpecÚpropertyrq   rr   Úboolrt   ru   ry   rz   r{   r|   r}   r~   r�   r‚   rƒ   ÚCallablerˆ   r‰   r   r�   r’   ÚListr¬   r¯   r   Ú_launch_kernelr³   r   ÚTupler»   Úpre_start_kernelr¼   Úpost_start_kernelrD   r¾   Ústart_kernelrÅ   Úrequest_shutdownÚfloatrÓ   Úfinish_shutdownr×   Úcleanup_resourcesrÚ   Úshutdown_kernelrÜ   Úrestart_kernelr5   rÑ   rÏ   Ú_send_kernel_sigtermrÐ   Ú_kill_kernelrÙ   Úinterrupt_kernelÚintrå   Úsignal_kernelrë   Úis_aliverÍ   Ú__classcell__r#   r#   rK   r$   rE   c   sÞ   
ÿýýÿ
$*þ   üû'' ÿþ)rE   c                       s¬  e Zd ZU dZedƒZeed< edd�Zeed< e	e
jjƒZe	ed< edƒe
jjdœdd	„ƒZejed
œ‡ fdd„ZejZejZejdejf ed< ejZejdejf ed< ejZejdejf ed< ejZ ejdejf ed< ej!Z"ejdejf ed< ej#Z$ejdejf ed< ej%Z&ejdejf ed< ej'Z(ejdejf ed< ej)Z*ej+Z,ej-Z.ejdejf ed< ej/Z0ejdejf ed< ej1Z2ejdejf ed< ‡  Z3S )ÚAsyncKernelManagerzAn async kernel manager.z-jupyter_client.asynchronous.AsyncKernelClientrT   rU   rW   rN   r'   c                 C   s   d| _ tj ¡ S rO   )rM   rP   r)   rQ   rR   r#   r#   r$   rS   Â  s    z#AsyncKernelManager._context_defaultrŠ   c                    s   t ƒ jf i |¤ŽS )zGet a client for the manager.)rH   r�   )r/   r1   rK   r#   r$   r�   Ç  s    zAsyncKernelManager.client.rø   rö   r÷   rù   rû   rü   rý   rþ   r  r  r  )4r   r   r    r!   r   rT   rî   r   rW   r
   rP   r)   rQ   rN   r   rS   r?   r   r   r�   rE   r¯   rô   r¾   rø   rò   Ú	Awaitabler»   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#   rK   r$   r  ¶  st   
ÿþþÿþÿþÿþÿþÿþÿþÿþÿþÿþÿþÿr  é<   rm   )Ústartup_timeoutrl   r1   r(   c                 K   sh   t |d�}|jf i |¤Ž | ¡ }| ¡  z|j| d� W n$ ty^   | ¡  | ¡  ‚ Y n0 ||fS )ú5Start a new kernel, and return its Manager and Clientrx   rÉ   )rE   rø   r�   Ústart_channelsÚwait_for_readyr+   Ústop_channelsrý   ©r	  rl   r1   ÚkmÚkcr#   r#   r$   Ústart_new_kernelö  s    
r  c                 Ë   sz   t |d�}|jf i |¤ŽI dH  | ¡ }| ¡  z|j| d�I dH  W n* typ   | ¡  | ¡ I dH  ‚ Y n0 ||fS )r
  rx   NrÉ   )r  rø   r�   r  r  r+   r  rý   r  r#   r#   r$   Ústart_new_async_kernel  s    
r  rŠ   c               	   k   sL   t f i | ¤Ž\}}z|V  W | ¡  |jdd� n| ¡  |jdd� 0 dS )z¹Context manager to create a kernel in a subprocess.

    The kernel is shut down when the context exits.

    Returns
    -------
    kernel_client: connected KernelClient instance
    T)rØ   N)r  r  rý   )r1   r  r  r#   r#   r$   Ú
run_kernel  s    
ÿr  )r  rm   )r  rm   )Cr!   r)   rA   r¦   r©   rà   r£   Útypingr?   rµ   Zasyncio.futuresr   Úconcurrent.futuresr,   Ú
contextlibr   Úenumr   rP   Zjupyter_core.utilsr   Z	traitletsr   r   r   r	   r
   r   r   r   r   r   Ztraitlets.utils.importstringr   rp   r   Zasynchronousr   Úblockingr   r�   r   Úconnectr   Z
managerabcr   Zprovisioningr   r   r¸   r   ÚTypeVarrò   r%   rí   r-   rD   rE   r  Úregisterrú   r\   rõ   r  r  ÚIteratorr  r#   r#   r#   r$   Ú<module>   s^   0
    W=
 ÿþ ÿþ