
    h|&                         S r SSKrSSKJr  SSKJr  SSKJrJrJ	r	J
r
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5      r " S	 S
\5      r " S S\5      r " S S\5      r/ SQrg)z2Classes for running 0MQ Devices in the background.    N)Process)Thread)AnyCallableListOptionalTuple)ENOTSOCKETERMPUSHQUEUEContextZMQBindErrorZMQErrorproxyc            	       ~   \ rS rSr% Sr\R                  r\/ \	R
                  4   \
S'    \\
S'   \\
S'   \\
S'   \\
S'   \\   \
S'   \\   \
S	'   \\\\4      \
S
'   \\   \
S'   \\   \
S'   \\\\4      \
S'   \\   \
S'   \\	R"                     \
S'   \SS4S\S\\   S\\   SS4S jjrS\SS4S jrS\S\4S jrS\SS4S jrS\S\SS4S jrS\SS4S jrS\S\4S jrS\4S jrS\S\4S jrS\S\4S jrS\\	R"                  \	R"                  4   4S jrS'S  jrS! r S'S" jr!S'S# jr"S(S$\\#   SS4S% jjr$S&r%g))Device   a  A 0MQ Device to be run in the background.

You do not pass Socket instances to this, but rather Socket types::

    Device(device_type, in_socket_type, out_socket_type)

For instance::

    dev = Device(zmq.QUEUE, zmq.DEALER, zmq.ROUTER)

Similar to zmq.device, but socket types instead of sockets themselves are
passed, and the sockets are created in the work thread, to avoid issues
with thread safety. As a result, additional bind_{in|out} and
connect_{in|out} methods and setsockopt_{in|out} allow users to specify
connections for the sockets.

Parameters
----------
device_type : int
    The 0MQ Device type
{in|out}_type : int
    zmq socket types, to be passed later to context.socket(). e.g.
    zmq.PUB, zmq.SUB, zmq.REQ. If out_type is < 0, then in_socket is used
    for both in_socket and out_socket.

Methods
-------
bind_{in_out}(iface)
    passthrough for ``{in|out}_socket.bind(iface)``, to be called in the thread
connect_{in_out}(iface)
    passthrough for ``{in|out}_socket.connect(iface)``, to be called in the
    thread
setsockopt_{in_out}(opt,value)
    passthrough for ``{in|out}_socket.setsockopt(opt, value)``, to be called in
    the thread

Attributes
----------
daemon : bool
    sets whether the thread should be run as a daemon
    Default is true, because if it is false, the thread will not
    exit unless it is killed
context_factory : callable
    This is a class attribute.
    Function for creating the Context. This will be Context.instance
    in ThreadDevices, and Context in ProcessDevices.  The only reason
    it is not instance() in ProcessDevices is that there may be a stale
    Context instance already initialized, and the forked environment
    should *never* try to use it.
context_factorydaemondevice_typein_typeout_type	_in_binds_in_connects_in_sockopts
_out_binds_out_connects_out_sockopts_random_addrs_socketsNreturnc                     Xl         Uc  [        S5      eUc  [        S5      eX l        X0l        / U l        / U l        / U l        / U l        / U l        / U l	        / U l
        SU l        SU l        / U l        g )Nzin_type must be specifiedzout_type must be specifiedTF)r   	TypeErrorr   r   r   r   r   r   r   r   r    r   doner!   )selfr   r   r   s       iC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\zmq/devices/basedevice.py__init__Device.__init__V   s     '?788899 	    addrc                 :    U R                   R                  U5        g)zPEnqueue ZMQ address for binding on in_socket.

See zmq.Socket.bind for details.
N)r   appendr&   r+   s     r'   bind_inDevice.bind_inn   s    
 	d#r*   c                 ^    U R                   " U/UQ70 UD6nU R                  U SU 35        U$ )zEnqueue a random port on the given interface for binding on
in_socket.

See zmq.Socket.bind_to_random_port for details.

.. versionadded:: 18.0
:)_reserve_random_portr/   r&   r+   argskwargsports        r'   bind_in_to_random_portDevice.bind_in_to_random_portu   s:     ((???vQtf%&r*   c                 :    U R                   R                  U5        g)zVEnqueue ZMQ address for connecting on in_socket.

See zmq.Socket.connect for details.
N)r   r-   r.   s     r'   
connect_inDevice.connect_in   s    
 	  &r*   optvaluec                 <    U R                   R                  X45        g)zUEnqueue setsockopt(opt, value) for in_socket

See zmq.Socket.setsockopt for details.
N)r   r-   r&   r=   r>   s      r'   setsockopt_inDevice.setsockopt_in   s    
 	  #.r*   c                 :    U R                   R                  U5        g)zQEnqueue ZMQ address for binding on out_socket.

See zmq.Socket.bind for details.
N)r   r-   r.   s     r'   bind_outDevice.bind_out   s    
 	t$r*   c                 ^    U R                   " U/UQ70 UD6nU R                  U SU 35        U$ )zEnqueue a random port on the given interface for binding on
out_socket.

See zmq.Socket.bind_to_random_port for details.

.. versionadded:: 18.0
r2   )r3   rD   r4   s        r'   bind_out_to_random_portDevice.bind_out_to_random_port   s:     ((???av&'r*   c                 :    U R                   R                  U5        g)zWEnqueue ZMQ address for connecting on out_socket.

See zmq.Socket.connect for details.
N)r   r-   r.   s     r'   connect_outDevice.connect_out   s    
 	!!$'r*   c                 <    U R                   R                  X45        g)zVEnqueue setsockopt(opt, value) for out_socket

See zmq.Socket.setsockopt for details.
N)r   r-   r@   s      r'   setsockopt_outDevice.setsockopt_out   s    
 	!!3,/r*   c                 z   [        5        nUR                  [        5       n[        S5       H1  nUR                  " U/UQ70 UD6nU SU 3nXR
                  ;   a  M1    O   [        S5      eU R
                  R                  U5        S S S 5        S S S 5        W$ ! , (       d  f       N= f! , (       d  f       W$ = f)N   r2   zCould not reserve random port.)r   socketr   rangebind_to_random_portr    r   r-   )	r&   r+   r5   r6   ctxbinderir7   new_addrs	            r'   r3   Device._reserve_random_port   s    Y#D!VqA!55dLTLVLD"&q/H#5#55  " ''GHH""))(3 "    "! Y  s#   B+A'BB+
B(	$B++
B:c                    U R                  5       nXl        UR                  U R                  5      nU R                  R                  U5        U R                  S:  a  UnO6UR                  U R                  5      nU R                  R                  U5        U R                   H  u  pEUR                  XE5        M     U R                   H  u  pEUR                  XE5        M     U R                   H  nUR                  U5        M     U R                   H  nUR                  U5        M     U R                   H  nUR                  U5        M     U R                   H  nUR                  U5        M     X#4$ )Nr   )r   _contextrQ   r   r!   r-   r   r   
setsockoptr   r   bindr   r   connectr   )r&   rT   insoutsr=   r>   ifaces          r'   _setup_socketsDevice._setup_sockets   s'   '+';';'= jj&S!==1D::dmm,DMM  & ++JCNN3& ,,,JCOOC' - ^^EHHUO $__EIIe % &&EKK '''ELL ( yr*   c                 >    U R                  5       u  p[        X5        g)z`The runner method.

Do not call me directly, instead call ``self.start()``, just like a Thread.
N)ra   r   )r&   r^   r_   s      r'   
run_deviceDevice.run_device   s    
 '')	cr*   c                     U R                    H/  nU(       d  M  UR                  (       a  M  UR                  5         M1     g)zCleanup sockets we createdN)r!   closedclose)r&   ss     r'   _close_socketsDevice._close_sockets   s&    Aq	 r*   c                      U R                  5         SU l        U R                  5         g! [         a%  nUR                  [        [        1;   a   SnANAe SnAff = f! SU l        U R                  5         f = f)z"wrap run_device in try/catch ETERMNT)rd   r   errnor   r
   r%   rj   )r&   es     r'   run
Device.run   se    
	"OO DI!  	ww5(++	 DI!s,   * 
AAA AAA A5c                 "    U R                  5       $ )z>Start the device. Override me in subclass for other launchers.)ro   r&   s    r'   startDevice.start  s    xxzr*   timeoutc                    [         R                  " 5       nUnU R                  (       dY  Ub  X2-
  U:  dM  [         R                  " S5        [         R                  " 5       nU R                  (       d  Uc  MA  X2-
  U:  d  MK  gggg)zTwait for me to finish, like Thread.join.

Reimplemented appropriately by subclasses.NgMbP?)time	monotonicr%   sleep)r&   ru   tictocs       r'   joinDevice.join  s`     nn))W%8SY=PJJu.."C ))W%8SY=P)=P)r*   )rZ   r   r   r   r   r   r   r    r!   r   r   r%   r   r   r"   NN)&__name__
__module____qualname____firstlineno____doc__r   instancer   r   zmq__annotations__boolintr   strr	   r   Socketr   r   r(   r/   r8   r;   rA   rD   rG   rJ   rM   r3   ra   rd   rj   ro   rs   floatr|   __static_attributes__ r*   r'   r   r      s   1f 291A1AOXb#++o.A LLMCys)uS#X''S	9c3h((93:: !!%"&	 # 3-	
 
0$C $D $3 C 's 't '/ /S /T /%S %T %C S ( (0# 0c 0 # &cjj#**&< = >"#HUO #t # #r*   r   c                   \    \ rS rSr% SrSr\\S'   Sr\\S'   SS jr	SS\
\   SS4S	 jjrS
rg)BackgroundDevicei  zEBase class for launching Devices in background processes and threads.Nlauncher_launch_classr"   c                     U R                  U R                  S9U l        U R                  U R                  l        U R                  R	                  5       $ )N)target)r   ro   r   r   rs   rr   s    r'   rs   BackgroundDevice.start  s>    **$((*;#{{}}""$$r*   ru   c                 4    U R                   R                  US9$ )N)ru   )r   r|   )r&   ru   s     r'   r|   BackgroundDevice.join  s    }}!!'!22r*   )r   r~   r   )r   r   r   r   r   r   r   r   r   rs   r   r   r|   r   r   r*   r'   r   r     s:    OHcM3%
3HUO 3t 3 3r*   r   c                       \ rS rSrSr\rSrg)ThreadDevicei   zKA Device that will be run in a background Thread.

See Device for details.
r   N)r   r   r   r   r   r   r   r   r   r*   r'   r   r      s    
 Mr*   r   c                        \ rS rSrSr\r\rSr	g)ProcessDevicei)  zLA Device that will be run in a background Process.

See Device for details.
r   N)
r   r   r   r   r   r   r   r   r   r   r   r*   r'   r   r   )  s    
 MOr*   r   )r   r   r   )r   rw   multiprocessingr   	threadingr   typingr   r   r   r   r	   r   r
   r   r   r   r   r   r   r   r   r   r   r   __all__r   r*   r'   <module>r      s_    8
  #  7 7 
 T T T# #D3v 3# 
$ 
 6r*   