
    <hIX                    ,   S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSK	r	SSK
Jr  SSKJr  SSKrSSKJrJrJrJrJrJrJrJr  SSKJr  SSKJr  S	S
KJr  S	SKJrJ r   S	SK!J"r"  S	SK#J$r$J%r%J&r&   " S S\'5      r(SS jr) " S S\5      r* " S S\*5      r+g)z%A kernel manager for multiple kernels    )annotationsNwraps)Path)AnyBoolDictDottedObjectNameInstanceUnicodedefaultobserve)LoggingConfigurable)import_item   )KernelConnectionInfo)NATIVE_KERNEL_NAMEKernelSpecManager)KernelManager)ensure_asyncrun_syncutcnowc                      \ rS rSrSrg)DuplicateKernelError    N)__name__
__module____qualname____firstlineno____static_attributes__r       tC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\jupyter_client/multikernelmanager.pyr   r      s    r"   r   c                H   ^  [        T 5                SU 4S jj5       nU$ )zDdecorator for proxying MKM.method(kernel_id) to individual KMs by IDc                ~   > U R                  U5      n[        UTR                  5      nU" U0 UD6nT" X/UQ70 UD6  U$ N)
get_kernelgetattrr   )self	kernel_idargskwargskmmethodrfs          r#   wrappedkernel_method.<locals>.wrapped!   sK    
 __Y'QZZ(D#F# 	
$+D+F+r"   )
r)   t.Anyr*   strr+   r3   r,   r3   returnzt.Callable | t.Awaitabler   )r0   r1   s   ` r#   kernel_methodr6      sJ     1X #,1=B	!  Nr"   c                  |  ^  \ rS rSrSr\" \SS9R                  SS9r\	" \
SS9r\" SS	S9R                  SS9r\" S
5      S:S j5       r\" SS9r\" S5      S;S j5       rS;S jr\" SSS9R                  SS9r\	" S5      r\" S5      r\" 5       r\S<S j5       r\" S5      S=S j5       r\" S5      r\" SSS9r\" 5       r S>U 4S jjr!S?U 4S jjr"S@S jr#SAS jr$SBS jr%      SCS jr&SDS jr'        SES jr(      SFS  jr)SGS! jr*SS".SHS# jjr+\," \+5      r-  SI       SJS$ jjr.\," \.5      r/\0SKSLS% jj5       r1\0  SM       SNS& jj5       r2\0SKSOS' jj5       r3SPS( jr4SKSQS) jjr5\," \55      r6SRS* jr7\0SSS+ j5       r8SKSTS, jjr9\," \95      r:\0SBS- j5       r;SRS. jr<SPS/ jr=\0 SU       SVS0 jj5       r>\0 SU       SVS1 jj5       r?\0SWS2 j5       r@\0 SX     SYS3 jj5       rA\0 SX     SYS4 jj5       rB\0 SX     SYS5 jj5       rC\0 SX     SYS6 jj5       rD\0 SX     SYS7 jj5       rESZS8 jrFS9rGU =rH$ )[MultiKernelManager3   z&A class for managing multiple kernels.z'The name of the default kernel to starthelpTconfig)
allow_nonez)jupyter_client.ioloop.IOLoopKernelManagerzThe kernel manager class.  This is configurable to allow
        subclassing of the KernelManager for customized behavior.
        kernel_manager_classc                .    U R                  5       U l        g r&   )_create_kernel_manager_factorykernel_manager_factory)r)   changes     r#   _kernel_manager_class_changed0MultiKernelManager._kernel_manager_class_changedC   s    &*&I&I&K#r"   z)this is kernel_manager_class after importrB   c                "    U R                  5       $ r&   )rA   r)   s    r#   _kernel_manager_factory_default2MultiKernelManager._kernel_manager_factory_defaultI   s    2244r"   c                F   ^ ^ [        T R                  5      mSUU 4S jjnU$ )Nc                    > TR                   (       aL  TR                  R                  (       a  TR                  5       Tl        UR	                  STR                  5        T" U 0 UD6nU$ )Ncontext)shared_contextrL   closed_context_default
setdefault)r+   r,   r-   kernel_manager_ctorr)   s      r#   create_kernel_managerPMultiKernelManager._create_kernel_manager_factory.<locals>.create_kernel_managerP   sQ    ""<<&&#'#8#8#:DL!!)T\\:$d5f5BIr"   )r+   r3   r,   r3   r5   r   )r   r?   )r)   rR   rQ   s   ` @r#   rA   1MultiKernelManager._create_kernel_manager_factoryM   s&    )$*C*CD	 	 %$r"   z4Share a single zmq.Context to talk to all my kernelszmq.ContextFc                    U R                   $ )z#A shim for backwards compatibility.)_pending_kernelsrG   s    r#   _starting_kernels$MultiKernelManager._starting_kernelsf   s     $$$r"   rL   c                :    SU l         [        R                  " 5       $ NT)_created_contextzmqContextrG   s    r#   rO   #MultiKernelManager._context_defaultk   s     ${{}r"    Nc                4   > [         TU ]  " U0 UD6  0 U l        g r&   )super__init__kernel_id_to_connection_file)r)   r+   r,   	__class__s      r#   rc   MultiKernelManager.__init__u   s    $)&)=?)r"   c                R  > U R                   (       as  U R                  (       ab  U R                  R                  (       dG  U R                  (       a  U R                  R	                  SU 5        U R                  R                  5          [        TU ]  nU" 5         g! [         a     gf = f)z:Handle garbage collection.  Destroy context if applicable.zDestroying zmq context for %sN)	r\   rL   rN   logdebugdestroyrb   __del__AttributeError)r)   	super_delre   s     r#   rk   MultiKernelManager.__del__y   sq      T\\$,,:M:Mxx>ELL  "	I K  		s   
B 
B&%B&c                   U R                   Gb  [        U R                   5      nUR                  5       (       Ga  UR                  5        Vs/ s H  o"R	                  5       (       d  M  UPM     nn[        U R                  R                  5       5      n[        U R                  R                  5       5      n[        U R                  R                  5       5       H7  nXc;  d  M
  XER                  U5         nU R                  U	 U R                  U	 M9     U GH  nX`R                  R                  5       ;   a  M#   [        R                  " UR                  5       5      nU R                  R!                  SU5        SU;   a  SU;   d  Mr  U R#                  5       nX`R                  U'   U R%                  U U R                  SS9n	U	R'                  U5        [)        5       U	l        SU	l        SU	l        Xyl        US   U	l        U	R4                  R7                  S5        XR                  U'   GM     [        U R                  R                  5       5      $ s  snf ! [         a     GMM  f = f)	z6Return a list of the kernel ids of the active kernels.NzLoading connection file %skernel_namekeyF)parentrh   owns_kernelidler   )external_connection_dirr   is_diriterdiris_filelistrd   keysvaluesindex_kernelsjsonloads	read_text	Exceptionrh   ri   new_kernel_idrB   load_connection_infor   last_activityexecution_stateconnectionsr*   rp   ready
set_result)
r)   ru   pconnection_fileskvconnection_filer*   connection_infor-   s
             r#   list_kernel_ids"MultiKernelManager.list_kernel_ids   s   ''3&*4+G+G&H#&--///F/N/N/P#`/P!T]T]T_A/P #` ::??AB::AACD'+D,M,M,T,T,V'WO&>$%ggo&>$?	 ==iH MM)4	 (X (8O&*K*K*R*R*TT !@D

+557A
 HHNN#?Q)_</AY  $ 2 2 4ICR55i@44# HH$) 5 B
 ++O<'-xB$)/B&%&BN#,L%4]%CBNHH''-/1MM),9 (8@ DMM&&())Y $a& % ! !s   I(I:$I
I$#I$c                4    [        U R                  5       5      $ )z%Return the number of running kernels.)lenr   rG   s    r#   __len__MultiKernelManager.__len__   s    4'')**r"   c                    XR                   ;   $ r&   )r}   r)   r*   s     r#   __contains__MultiKernelManager.__contains__   s    MM))r"   c                n   UR                  SU R                  " S0 UD65      nX0;   a  [        SU-  5      eUc  U R                  n0 nU R                  (       a  U R                  US'   U R
                  " S[        R                  R                  U R                  SU-  5      U U R                  US.UD6nXQU4$ )Nr*   zKernel already exists: %skernel_spec_managerzkernel-%s.json)r   rr   rh   rp   r   )popr   r   default_kernel_namer   rB   ospathjoinconnection_dirrh   )r)   rp   r,   r*   constructor_kwargsr-   s         r#   pre_start_kernel#MultiKernelManager.pre_start_kernel   s     JJ{D,>,>,H,HI	&'BY'NOO22K  ##8<8P8P45(( 
GGLL)<)<>NQZ>Z[#	

 !
 	))r"   c               H    X;   a  U R                   U   R                  US9  gg)z
Allow to update the environment of the given kernel.

Forward the update env request to the corresponding kernel.

.. version-added: 8.5
)envN)r}   
update_env)r)   r*   r   s      r#   r   MultiKernelManager.update_env   s)     MM)$//C/8 r"   c                   #     UI S h  vN   X R                   U'   U R                  R                  US 5        g  N/! [         a%  nU R                  R                  U5         S nAg S nAff = f7fr&   )r}   rW   r   r   rh   	exception)r)   r*   r-   kernel_awaitablees        r#   _add_kernel_when_ready)MultiKernelManager._add_kernel_when_ready   s`     	""""')MM)$!!%%i6 #  	"HHq!!	"s7   A-; 9.; A-; 
A*A% A-%A**A-c                   #     UI S h  vN   U R                  U5        U R                  R                  US 5        g  N2! [         a%  nU R                  R                  U5         S nAg S nAff = f7fr&   )remove_kernelrW   r   r   rh   r   )r)   r*   r   r   s       r#   _remove_kernel_when_ready,MultiKernelManager._remove_kernel_when_ready   sb     	""""y)!!%%i6 #  	"HHq!!	"s7   A0> <1> A0> 
A-A(#A0(A--A0c                    [        U SS5      $ )zoReturns a boolean; a clearer method for determining if
this multikernelmanager is using pending kernels or not
use_pending_kernelsF)r(   rG   s    r#   _using_pending_kernels)MultiKernelManager._using_pending_kernels   s     t2E::r"   )rp   c               R  #    U R                  X5      u  p1n[        U[        5      (       d<  U R                  R	                  SR                  U R                  R                  S95        XBS'   [        UR                  " S0 UD65      n[        R                  " U R                  XCU5      5      nX`R                  U'   U R                  5       (       a  X0R                  U'   U$ UI Sh  vN   UR                   R#                  5       (       a  UR                   R#                  5       eU$  N?7f)zStart a new kernel.

The caller can pick a kernel_id by passing one in as a keyword arg,
otherwise one will be generated using new_kernel_id().

The kernel ID for the newly started kernel is returned.
zHKernel manager class ({km_class}) is not an instance of 'KernelManager'!)km_classr*   Nr   )r   
isinstancer   rh   warningformatr?   re   r   start_kernelasynciocreate_taskr   rW   r   r}   r   r   )r)   rp   r,   r-   r*   startertasks          r#   _async_start_kernel&MultiKernelManager._async_start_kernel   s     &*%:%:;%O""m,,HHZaa!66@@ b 
 ({r889""4#>#>yg#VW+/i(&&(( (*MM)$  JJxx!!##hh((** s   C#D'%D%&A D'c                  #    U R                   R                  SU5        XR                  ;   ab  U R                  U   n UI Sh  vN   U R                  U5      n[        R
                  " [        R                  UR                  5      I Sh  vN   U R                  U5      nUR                  R                  5       (       d1  UR                  R                  5       (       a  U R                  U5        g[        UR                  X#5      5      n[        R                   " U R#                  X5      5      nXpR                  U'   U R%                  5       (       dD  UI Sh  vN   UR                  R                  5       (       a  UR                  R                  5       egg GNU GN! [        R                   a     GN%[         a    U R                  U5         gf = f N|7f)zShutdown a kernel by its kernel uuid.

Parameters
==========
kernel_id : uuid
    The id of the kernel to shutdown.
now : bool
    Should the kernel be shutdown forcibly using a signal.
restart : bool
    Will the kernel be restarted?
Kernel shutdown: %sN)rh   inforW   r'   tcastr   Futurer   CancelledErrorr   r   	cancelledr   r   shutdown_kernelensure_futurer   r   )r)   r*   nowrestartr   r-   stopperfuts           r#   _async_shutdown_kernel)MultiKernelManager._async_shutdown_kernel!  st    " 	+Y7---((3D

__Y/ffW^^RXX666 __Y'xx!!##(:(:(<(<y)r11#?@##D$B$B9$VW+.i(**,,IIxx!!##hh((** $ -# 6))  ""9- sf   ;GF FAF FF CGG?GF F G2G5GGGGc                    g)z,Ask a kernel to shut down by its kernel uuidNr   r)   r*   r   s      r#   request_shutdown#MultiKernelManager.request_shutdownP      r"   c                <    U R                   R                  SU5        g)zDWait for a kernel to finish shutting down, and kill it if it doesn'tr   Nrh   r   )r)   r*   waittimepollintervals       r#   finish_shutdown"MultiKernelManager.finish_shutdownT  s     	+Y7r"   c                    g)zClean up a kernel's resourcesNr   r   s      r#   cleanup_resources$MultiKernelManager.cleanup_resources^  r   r"   c                :    U R                   R                  US5      $ )zremove a kernel from our mapping.

Mainly so that a kernel can be removed if it is already dead,
without having to call shutdown_kernel.

The kernel object is returned, or `None` if not found.
N)r}   r   r   s     r#   r    MultiKernelManager.remove_kernelb  s     }}  D11r"   c                <  #    U R                  5       nU[        U R                  5      -  n[        U R                  R	                  5       5      n[        U5       Vs/ s H  o@R                  XAS9PM     nn[        R                  " U6 I Sh  vN   U R                  5       (       a  U H  n UR                  I Sh  vN   M     ggs  snf  N> N! [        R                   a+    U R                  UR                     R                  5          Me  [         a     Mq  f = f7f)zShutdown all kernels.r   N)r   ry   rW   r}   r{   setr   r   gatherr   r   r   r*   cancelr   )r)   r   kidskmskidfutsr-   s          r#   _async_shutdown_all&MultiKernelManager._async_shutdown_alll  s     ##%T**++4=='')*EHYOYc++C+9YOnnd###&&((((NN  ) P#
 #-- A))",,7>>@  s`   ADC3DC
D+C:C;C?DC;D	D	DDDDc                    U R                  U5      nUR                  R                  5       (       d  Sn[        U5      eUR	                  5       nU R
                  R                  SU5        U$ )zzInterrupt (SIGINT) the kernel by its uuid.

Parameters
==========
kernel_id : uuid
    The id of the kernel to interrupt.
z/Kernel is in a pending state. Cannot interrupt.zKernel interrupted: %s)r'   r   doneRuntimeErrorinterrupt_kernelrh   r   )r)   r*   kernelmsgouts        r#   r   #MultiKernelManager.interrupt_kernel  s[     +||  ""CCs##%%'.	:
r"   c                <    U R                   R                  SX5        g)a
  Sends a signal to the kernel by its uuid.

Note that since only SIGTERM is supported on Windows, this function
is only useful on Unix systems.

Parameters
==========
kernel_id : uuid
    The id of the kernel to signal.
signum : int
    Signal number to send kernel.
zSignaled Kernel %s with %sNr   )r)   r*   signums      r#   signal_kernel MultiKernelManager.signal_kernel  s     	2IFr"   c                ,  #    U R                  U5      nU R                  5       (       a,  UR                  R                  5       (       d  Sn[	        U5      e[        UR                  US95      I Sh  vN   U R                  R                  SU5        g N!7f)a  Restart a kernel by its uuid, keeping the same ports.

Parameters
==========
kernel_id : uuid
    The id of the kernel to interrupt.
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.
z-Kernel is in a pending state. Cannot restart.r   NzKernel restarted: %s)	r'   r   r   r   r   r   restart_kernelrh   r   )r)   r*   r   r   r   s        r#   _async_restart_kernel(MultiKernelManager._async_restart_kernel  sz      +&&((1B1B1D1DACs##600S09:::,i8 	;s   A.B0B1"Bc                    g)zIs the kernel alive.

This calls KernelManager.is_alive() which calls Popen.poll on the
actual kernel subprocess.

Parameters
==========
kernel_id : uuid
    The id of the kernel.
Nr   r   s     r#   is_aliveMultiKernelManager.is_alive  r   r"   c                *    X;  a  [        SU-  5      eg)zcheck that a kernel id is validzKernel with id not found: %sN)KeyErrorr   s     r#   _check_kernel_id#MultiKernelManager._check_kernel_id  s     9IEFF !r"   c                B    U R                  U5        U R                  U   $ )zGet the single KernelManager object for a kernel by its uuid.

Parameters
==========
kernel_id : uuid
    The id of the kernel.
)r  r}   r   s     r#   r'   MultiKernelManager.get_kernel  s!     	i(}}Y''r"   c                    g)z&add a callback for the KernelRestarterNr   r)   r*   callbackevents       r#   add_restart_callback'MultiKernelManager.add_restart_callback  r   r"   c                    g)z)remove a callback for the KernelRestarterNr   r  s       r#   remove_restart_callback*MultiKernelManager.remove_restart_callback  r   r"   c                    g)ak  Return a dictionary of connection data for a kernel.

Parameters
==========
kernel_id : uuid
    The id of the kernel.

Returns
=======
connection_dict : dict
    A dict of the information needed to connect to a kernel.
    This includes the ip address and the integer port
    numbers of the different channels (stdin_port, iopub_port,
    shell_port, hb_port).
Nr   r   s     r#   get_connection_info&MultiKernelManager.get_connection_info  r   r"   c                    g)zReturn a zmq Socket connected to the iopub channel.

Parameters
==========
kernel_id : uuid
    The id of the kernel
identity : bytes (optional)
    The zmq identity of the socket

Returns
=======
stream : zmq Socket or ZMQStream
Nr   r)   r*   identitys      r#   connect_iopub MultiKernelManager.connect_iopub  r   r"   c                    g)zReturn a zmq Socket connected to the shell channel.

Parameters
==========
kernel_id : uuid
    The id of the kernel
identity : bytes (optional)
    The zmq identity of the socket

Returns
=======
stream : zmq Socket or ZMQStream
Nr   r  s      r#   connect_shell MultiKernelManager.connect_shell  r   r"   c                    g)zReturn a zmq Socket connected to the control channel.

Parameters
==========
kernel_id : uuid
    The id of the kernel
identity : bytes (optional)
    The zmq identity of the socket

Returns
=======
stream : zmq Socket or ZMQStream
Nr   r  s      r#   connect_control"MultiKernelManager.connect_control  r   r"   c                    g)zReturn a zmq Socket connected to the stdin channel.

Parameters
==========
kernel_id : uuid
    The id of the kernel
identity : bytes (optional)
    The zmq identity of the socket

Returns
=======
stream : zmq Socket or ZMQStream
Nr   r  s      r#   connect_stdin MultiKernelManager.connect_stdin)  r   r"   c                    g)zReturn a zmq Socket connected to the hb channel.

Parameters
==========
kernel_id : uuid
    The id of the kernel
identity : bytes (optional)
    The zmq identity of the socket

Returns
=======
stream : zmq Socket or ZMQStream
Nr   r  s      r#   
connect_hbMultiKernelManager.connect_hb;  r   r"   c                >    [        [        R                  " 5       5      $ )z
Returns the id to associate with the kernel for this request. Subclasses may override
this method to substitute other sources of kernel ids.
:param kwargs:
:return: string-ized version 4 uuid
)r4   uuiduuid4)r)   r,   s     r#   r    MultiKernelManager.new_kernel_idM  s     4::<  r"   )r\   rL   rd   rB   )rC   r3   r5   None)r5   
t.Callable)r5   dict)r5   rU   )r+   r3   r,   r3   r5   r'  )r5   r'  )r5   z	list[str])r5   int)r*   r4   r5   bool)rp   
str | Noner,   r3   r5   ztuple[KernelManager, str, str])r*   r4   r   zt.Dict[str, str]r5   r'  )r*   r4   r-   r   r   t.Awaitabler5   r'  )r*   r4   r   r-  r5   r'  )r5   r+  )rp   r,  r,   r3   r5   r4   )FF)r*   r4   r   bool | Noner   r.  r5   r'  )F)r*   r4   r   r.  r5   r'  )Ng?)r*   r4   r   float | Noner   r/  r5   r'  )r*   r4   r   r+  r5   r'  )r*   r4   r5   r   )r   r+  r5   r'  )r*   r4   r5   r'  )r*   r4   r   r*  r5   r'  )r*   r4   r   r+  r5   r'  )r   )r*   r4   r  r(  r	  r4   r5   r'  )r*   r4   r5   zdict[str, t.Any]r&   )r*   r4   r  zbytes | Noner5   zsocket.socket)r,   r3   r5   r4   )Ir   r   r   r    __doc__r   r   tagr   r   r   r   r
   r?   r   rD   r   rB   r   rH   rA   r   rM   rL   r\   r	   rW   propertyrX   rO   r   ru   r}   rc   rk   r   r   r   r   r   r   r   r   r   r   r   r   r   r6   r   r   r   r   r   shutdown_allr   r   r   r   r   r  r'   r
  r  r  r  r  r  r  r!  r   r!   __classcell__)re   s   @r#   r8   r8   3   sI   0!!J	cc  ##4F+3
 
cc  #$L %L !&QR%&5 '5% C 
cc 
 }%GE{v% % Y  R[N%dt<vH@1*f+**%*/4*	'*2	9"""/"CN"	"""0;"	"; FJ B /0L
 !$	++++ ++ 	++
 
++Z 56O; ;  "&%(	88 8 #	8
 
8 8 , ,2$ /0L  G G9, 34N
 
G
	( AJ55(25;>5	5 5
 AJ88(28;>8	8 8
  " 7;(4	 " 7;(4	 " 7;(4	 " 7;(4	 " 7;(4	 "! !r"   r8   c                     \ rS rSr% \" SSSS9r\" SSS9R                  SS	9r\	" S
5      r
\" S5      SS j5       r\R                  rS\S'   \R"                  rS\S'   \R&                  rS\S'   \R*                  rS\S'   Srg)AsyncMultiKernelManageriW  z.jupyter_client.ioloop.AsyncIOLoopKernelManagerTzThe kernel manager class.  This is configurable to allow
        subclassing of the AsyncKernelManager for customized behavior.
        )r=   r;   FzWhether to make kernels available before the process has started.  The
        kernel has a `.ready` future which can be awaited before connectingr:   r<   zmq.asyncio.ContextrL   c                L    SU l         [        R                  R                  5       $ r[   )r\   r]   r   r^   rG   s    r#   rO   (AsyncMultiKernelManager._context_defaulth  s     ${{""$$r"   zt.Callable[..., t.Awaitable]r   r   r   r3  )r\   N)r5   r7  )r   r   r   r    r
   r?   r   r1  r   r   rL   r   rO   r8   r   r   __annotations__r   r   r   r   r   r3  r!   r   r"   r#   r6  r6  W  s    +8 O 
cc	  ,-GY% % 2D1W1WL.W3E3[3[N0[4F4]4]O1]1C1W1WL.Wr"   r6  )r0   r(  r5   r(  ),r0  
__future__r   r   r~   r   sockettypingr   r$  	functoolsr   pathlibr   r]   	traitletsr   r   r	   r
   r   r   r   r   traitlets.config.configurabler   traitlets.utils.importstringr   connectr   
kernelspecr   r   managerr   utilsr   r   r   r   r   r6   r8   r6  r   r"   r#   <module>rG     sv    + #   	      
 \ \ \ = 4 ) = " 1 1	9 	*a!, a!HX0 Xr"   