
    <h,                         S 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J	r	  SSK
JrJrJrJr  SSKrSSKJr  SSKJrJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr   " S S5      r " S S\	5      r  " S S\5      r!g)zbDefines a KernelClient that provides thread-safe sockets with async callbacks on message
replies.
    N)Future)partial)Thread)AnyDictListOptional)IOLoop)InstanceType)
get_logger)	zmqstream   )	HBChannel)KernelClient)Sessionc                     ^  \ rS rSrSrSrSrSrSrSr	S\
\R                     S\
\   S\
\   SS4U 4S jjrS	rS\4S
 jrSS jrSS jrSS jrS\\\4   SS4S jrS\SS4S jrS\\\4   SS4S jrSS jrSS\SS4S jjrSS jr Sr!U =r"$ )ThreadedZMQSocketChannel   z.A ZMQ socket invoking a callback in the ioloopNsocketsessionloopreturnc                    >^ ^ [         TT ]  5         UT l        UT l        UT l        [        5       mSUU 4S jjnT R                  c   eT R                  R                  U5        TR                  SS9  g)zCreate a channel.

Parameters
----------
socket : :class:`zmq.Socket`
    The ZMQ socket to use.
session : :class:`session.Session`
    The session to use.
loop
    A tornado ioloop to connect the socket to using a ZMQStream
Nc                  B  >  TR                   c   e[        R                  " TR                   TR                  5      Tl        TR                  R                  TR                  5        TR                  S 5        g ! [         a  n TR                  U 5         S n A g S n A ff = fN)
r   r   	ZMQStreamioloopstreamon_recv_handle_recv
set_result	Exceptionset_exceptionefselfs    jC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\jupyter_client/threaded.pysetup_stream7ThreadedZMQSocketChannel.__init__.<locals>.setup_stream<   s{    #{{...'11$++t{{K##D$5$56 T"  #""#s   A$A9 9
BBB
   timeoutr   N)super__init__r   r   r   r   add_callbackresult)r(   r   r   r   r*   r'   	__class__s   `    @r)   r1   !ThreadedZMQSocketChannel.__init__$   sh    " 	H	# 	# {{&&&  .	    Fc                     U R                   $ )zWhether the channel is alive.	_is_aliver(   s    r)   is_alive!ThreadedZMQSocketChannel.is_aliveM   s    ~~r6   c                     SU l         g)zStart the channel.TNr8   r:   s    r)   startThreadedZMQSocketChannel.startQ   s	    r6   c                     SU l         g)zStop the channel.FNr8   r:   s    r)   stopThreadedZMQSocketChannel.stopU   s	    r6   c                   ^ ^ T R                   bK  T R                  b>  [        5       mSUU 4S jjnT R                  R                  U5         TR	                  SS9  T R                  b"   T R                  R                  S	S
9  ST l	        gg! [
         a;  n[        5       nST R                    SU 3nUR                  U[        SS9   SnANpSnAff = f! [
         a     N]f = f)zClose the channel.Nc                     >  TR                   b   TR                   R                  SS9  S Tl         TR                  S 5        g ! [         a  n TR	                  U 5         S n A g S n A ff = f)Nr   linger)r   closer"   r#   r$   r%   s    r)   close_stream4ThreadedZMQSocketChannel.close.<locals>.close_stream_   s]    '{{.)))3&* LL& ! 'OOA&&'s   -A 
A'A""A'   r-   zError closing stream z:    )
stacklevelr   rE   r/   )r   r   r   r2   r3   r#   r   warningRuntimeWarningr   rG   )r(   rH   r&   logmsgr'   s   `    @r)   rG   ThreadedZMQSocketChannel.closeY   s    ;;"t{{'>A' ' KK$$\2?# ;;"!!!+ DK #  ? l-dkk]"QC@CA>?  s*   B )C 
C1CC
C C rP   c                 n   ^ ^ SUU 4S jjnT R                   c   eT R                   R                  U5        g)zQueue a message to be sent from the IOLoop's thread.

Parameters
----------
msg : message to send

This is threadsafe, as it uses IOLoop.add_callback to give the loop's
thread control of the action.
Nc                  p   > TR                   c   eTR                   R                  TR                  T 5        g r   )r   sendr   )rP   r(   s   r)   thread_send2ThreadedZMQSocketChannel.send.<locals>.thread_send   s+    <<+++LLdkk3/r6   r/   )r   r2   )r(   rP   rU   s   `` r)   rT   ThreadedZMQSocketChannel.sendy   s1    	0 	0 {{&&&  -r6   msg_listc                    U R                   c   eU R                  c   eU R                  R                  U5      u  p#U R                  R                  U5      nU R                  (       a  U R	                  U5        U R                  U5        g)zKCallback for stream.on_recv.

Unpacks message, and calls handlers with it.
N)r   r   feed_identitiesdeserialize_inspectcall_handlers)r(   rX   identsmsgrP   s        r)   r!   %ThreadedZMQSocketChannel._handle_recv   so    
 {{&&&||'''ll228<ll&&t,==MM#3r6   c                     g)aA  This method is called in the ioloop thread when a message arrives.

Subclasses should override this method to handle incoming messages.
It is important to remember that this method is called in the thread
so that some logic must be done to ensure that the application level
handlers are called in the application thread.
N r(   rP   s     r)   r]   &ThreadedZMQSocketChannel.call_handlers   s     	r6   c                     g)zQSubclasses should override this with a method
processing any pending GUI events.
Nrb   r:   s    r)   process_events'ThreadedZMQSocketChannel.process_events   s     	r6   r.   c                 0  ^  [         R                  " 5       U-   nT R                  c   eT R                  b  T R                  R	                  5       (       a  Sn[        U5      eS[        SS4U 4S jjn[        S5       H  n[        5       nT R                  R                  [        XF5      5        [        U[         R                  " 5       -
  S5      n UR                  [        U[         R                  " 5       -
  S5      5        M     g! [         a       gf = f)a  Immediately processes all pending messages on this channel.

This is only used for the IOPub channel.

Callers should use this method to ensure that :meth:`call_handlers`
has been called for all messages that have been received on the
0MQ SUB socket of this channel.

This method is thread safe.

Parameters
----------
timeout : float, optional
    The maximum amount of time to spend flushing, in seconds. The
    default is one second.
NzAttempt to flush closed streamr'   r   c                    >  TR                  5         U R                  S 5        g ! [         a  nU R                  U5         S nAg S nAff = fr   )_flushr"   r#   r$   )r'   r&   r(   s     r)   flush-ThreadedZMQSocketChannel.flush.<locals>.flush   s@    # T"  #""#s   % 
A
AA
rK   r   )time	monotonicr   r   closedOSErrorr   ranger   r2   r   maxr3   TimeoutError)r(   r.   	stop_time_msgrk   _r'   s   `      r)   rk   ThreadedZMQSocketChannel.flush   s    & NN$w.	{{&&&;;$++"4"4"6"63D$-	#S 	#T 	# qAAKK$$WU%67)dnn&66:GY)991=>    s   1D
DDc                 d    U R                   c   eU R                   R                  5         SU l        g)z"Callback for :method:`self.flush`.NT)r   rk   _flushedr:   s    r)   rj   ThreadedZMQSocketChannel._flush   s)    {{&&&r6   )ry   r9   r   r   r   r   r/   )g      ?)#__name__
__module____qualname____firstlineno____doc__r   r   r   r   r\   r	   zmqSocketr   r
   r1   r9   boolr;   r>   rA   rG   r   strr   rT   r   r!   r]   rf   floatrk   rj   __static_attributes____classcell__r4   s   @r)   r   r      s    8GFFFH%$% '"% v	%
 
%N I$ @.S#X .4 .$ T  d  c3h D +U +T +Z r6   r   c                      ^  \ rS rSrSrSrSrSU 4S jjr\\	R                  SS j5       5       rSS jrSS jrSS	 jrSS
 jrSS jrSS jrSrU =r$ )IOLoopThread   z;Run a pyzmq ioloop in a thread to send and receive messagesFNc                 0   > [         TU ]  5         SU l        g)zInitialize an io loop thread.TN)r0   r1   daemonr(   r4   s    r)   r1   IOLoopThread.__init__   s    r6   c                  *    [         b  S[         l        g g )NT)r   _exitingrb   r6   r)   _notice_exitIOLoopThread._notice_exit   s    
 #$(L! $r6   c                     [        5       U l        [        R                  " U 5        U R                  R	                  SS9  g)zcStart the IOLoop thread

Don't return until self.ioloop is defined,
which is created in the thread
r,   r-   N)r   _start_futurer   r>   r3   r:   s    r)   r>   IOLoopThread.start   s1     &,XT!!"!-r6   c                 r  ^   [         R                  " 5       n[         R                  " U5        SU 4S jjnUR                  U" 5       5        T R                  R                  S5        WR                  T R                  5       5        g! [         a%  nT R                  R                  U5         SnANJSnAff = f)z0Run my loop, ignoring EINTR events in the pollerNc                  B   >#    [         R                  " 5       T l        g 7fr   )r
   currentr   r:   s   r)   assign_ioloop'IOLoopThread.run.<locals>.assign_ioloop  s     $nn.s   r/   )	asyncionew_event_loopset_event_looprun_until_completer   r"   r#   r$   
_async_run)r(   r   r   r&   s   `   r)   runIOLoopThread.run   s    	0))+D""4(/ ##MO4 ))$/ 12  	0,,Q//	0s   A	B 
B6B11B6c                    #    U R                   (       d2  [        R                  " S5      I Sh  vN   U R                   (       d  M1  gg N7f)z(Run forever (until self._exiting is set)r   N)r   r   sleepr:   s    r)   r   IOLoopThread._async_run  s,     ----""" ---"s   +A	AA	A	c                 `    SU l         U R                  5         U R                  5         SU l        g)zStop the channel's event loop and join its thread.

This calls :meth:`~threading.Thread.join` and returns when the thread
terminates. :class:`RuntimeError` will be raised if
:meth:`~threading.Thread.start` is called again.
TN)r   joinrG   r   r:   s    r)   rA   IOLoopThread.stop  s$     		

r6   c                 $    U R                  5         g r   )rG   r:   s    r)   __del__IOLoopThread.__del__  s    

r6   c                 t    U R                   b   U R                   R                  SS9  gg! [         a     gf = f)zClose the io loop thread.NT)all_fds)r   rG   r#   r:   s    r)   rG   IOLoopThread.close   s?    ;;"!!$!/ #  s   * 
77)r   r   r   r   r/   )r{   r|   r}   r~   r   r   r   r1   staticmethodatexitregisterr   r>   r   r   rA   r   rG   r   r   r   s   @r)   r   r      sX    EHF
 __)  )	.3"#

 r6   r   c                     ^  \ rS rSrSr\S\\   4S j5       r\	" \
SS9r     SS\S\S	\S
\S\SS4U 4S jjjrS\\\4   SS4S jrSU 4S jjr\" \5      r\" \5      r\" \5      r\" \5      r\" \5      rS\4S jrSrU =r$ )ThreadedKernelClienti)  zYA KernelClient that provides thread-safe sockets with async callbacks on message replies.r   c                 R    U R                   (       a  U R                   R                  $ g r   )ioloop_threadr   r:   s    r)   r   ThreadedKernelClient.ioloop,  s     %%,,,r6   T)
allow_noneshelliopubstdinhbcontrolNc                    > [        5       U l        U R                  R                  5         U(       a  U R                  U R                  l        [        TU ]  XX4U5        g)z!Start the channels on the client.N)r   r   r>   _check_kernel_info_replyshell_channelr\   r0   start_channels)r(   r   r   r   r   r   r4   s         r)   r   #ThreadedKernelClient.start_channels4  sI     *^  "*.*G*GD'uU@r6   rP   c                 \    US   S:X  a#  U R                  U5        SU R                  l        gg)zGThis is run in the ioloop thread when the kernel info reply is receivedmsg_typekernel_info_replyN)_handle_kernel_info_replyr   r\   rc   s     r)   r   -ThreadedKernelClient._check_kernel_info_replyE  s0    z?11**3/*.D' 2r6   c                    > [         TU ]  5         U R                  (       a;  U R                  R                  5       (       a  U R                  R	                  5         ggg)z Stop the channels on the client.N)r0   stop_channelsr   r;   rA   r   s    r)   r   "ThreadedKernelClient.stop_channelsK  sF    $"4"4"="="?"?##% #@r6   c                 R    U R                   b  U R                   R                  5       $ g)z$Is the kernel process still running?T)_hb_channel
is_beatingr:   s    r)   r;   ThreadedKernelClient.is_aliveW  s*    ' ##..00 r6   )r   )TTTTTr/   )r{   r|   r}   r~   r   propertyr	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   iopub_channel_classshell_channel_classstdin_channel_classr   hb_channel_classcontrol_channel_classr;   r   r   r   s   @r)   r   r   )  s    c(  
 \d;M AA A 	A
 A A 
A A"/DcN /t /& 787878I !9:$  r6   r   )"r   r   r   rm   concurrent.futuresr   	functoolsr   	threadingr   typingr   r   r   r	   r   tornado.ioloopr
   	traitletsr   r   traitlets.logr   zmq.eventloopr   channelsr   clientr   r   r   r   r   r   rb   r6   r)   <module>r      sd       %   , , 
 ! $ $ #     DI6 IX6< 6r6   