
    h3                     R   S 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rSSKJ	r	  SSK
JrJrJr  SSKJr  SSKJr  SSKJr  SSKrSSKJrJrJr  \R0                  (       a  SS	KJr  \R4                  rS
\4S jrSS jrSq SS\\   S\\   S
\4S jjrS
\\   4S jr  " S S5      r!g)zUtilities for working with multiple processes, including both forking
the server into multiple processes and managing subprocesses.
    N)hexlify)Future"future_set_result_unless_cancelled%future_set_exception_unless_cancelled)ioloop)PipeIOStream)gen_log)OptionalAnyCallable)Listreturnc                      [         c  g [         R                  " 5       $ ! [         a     Of = f [        R                  " S5      $ ! [
        [        4 a     Of = f[        R                  " S5        g)z1Returns the number of processors on this machine.   SC_NPROCESSORS_CONFz1Could not detect number of processors; assuming 1)	multiprocessing	cpu_countNotImplementedErrorossysconfAttributeError
ValueErrorr	   error     bC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\tornado/process.pyr   r   1   sm    ((** zz/00J' MMEFs    
,,A AAc                  :   S[         R                  ;  a  g SS Kn  [        [	        [
        R                  " S5      5      S5      nU R                  U5        g ! [         a:    [        [        R                  " 5       S-  5      [
        R                  " 5       -  n NUf = f)Nrandomr      i  )sysmodulesr   intr   r   urandomr   timegetpidseed)r   r&   s     r   _reseed_randomr'   A   sq    s{{"
572::b>*B/ KK  5499;%&45s   )A ABBnum_processesmax_restartsc                 |  ^	 [         R                  S:X  a  [        S5      eUc  Sn[        b   eU b  U S::  a
  [	        5       n [
        R                  " SU 5        0 m	S[        S[        [           4U	4S	 jjn[        U 5       H  nU" U5      nUc  M  Us  $    SnT	(       Ga  [        R                  " 5       u  pgUT	;  a  M'  T	R                  U5      n[        R                  " U5      (       a.  [
        R                  " S
UU[        R                  " U5      5        Oa[        R                   " U5      S:w  a.  [
        R                  " SUU[        R                   " U5      5        O[
        R                  " SXF5        M  US-  nXQ:  a  [#        S5      eU" U5      nUb  U$ T	(       a  GM  [         R$                  " S5        g)a!  Starts multiple worker processes.

If ``num_processes`` is None or <= 0, we detect the number of cores
available on this machine and fork that number of child
processes. If ``num_processes`` is given and > 0, we fork that
specific number of sub-processes.

Since we use processes and not threads, there is no shared memory
between any server code.

Note that multiple processes are not compatible with the autoreload
module (or the ``autoreload=True`` option to `tornado.web.Application`
which defaults to True when ``debug=True``).
When using multiple processes, no IOLoops can be created or
referenced until after the call to ``fork_processes``.

In each child process, ``fork_processes`` returns its *task id*, a
number between 0 and ``num_processes``.  Processes that exit
abnormally (due to a signal or non-zero exit status) are restarted
with the same id (up to ``max_restarts`` times).  In the parent
process, ``fork_processes`` calls ``sys.exit(0)`` after all child
processes have exited normally.

max_restarts defaults to 100.

Availability: Unix
win32zfork not available on windowsNd   r   zStarting %d processesir   c                 b   > [         R                  " 5       nUS:X  a  [        5         U qU $ U TU'   g Nr   )r   forkr'   _task_id)r-   pidchildrens     r   start_child#fork_processes.<locals>.start_child   s1    ggi!8HHHSMr   z1child %d (pid %d) killed by signal %d, restartingz3child %d (pid %d) exited with status %d, restartingz!child %d (pid %d) exited normallyr   z"Too many child restarts, giving up)r    platform	Exceptionr1   r   r	   infor"   r
   ranger   waitpopWIFSIGNALEDwarningWTERMSIGWEXITSTATUSRuntimeErrorexit)
r(   r)   r4   r-   idnum_restartsr2   statusnew_idr3   s
            @r   fork_processesrF   S   s   < ||w 788  2!LL(-8H
s 
x} 
 =!^>I " L
ggih\\#>>&!!OOCF#	 ^^F#q(OOEv&	 LL<bF&CDDRM7 (@ HHQKr   c                      [         $ )zhReturns the current task id, if any.

Returns None if this process was not created by `fork_processes`.
)r1   r   r   r   task_idrH      s	     Or   c                       \ rS rSrSr\" 5       rSr0 rS\	S\	SS4S jr
S	\\/S4   SS4S
 jrSS\SS4S jjr\SS j5       r\SS j5       r\SS j5       r\S\SS4S j5       rS\SS4S jrSrg)
Subprocess   a  Wraps ``subprocess.Popen`` with IOStream support.

The constructor is the same as ``subprocess.Popen`` with the following
additions:

* ``stdin``, ``stdout``, and ``stderr`` may have the value
  ``tornado.process.Subprocess.STREAM``, which will make the corresponding
  attribute of the resulting Subprocess a `.PipeIOStream`. If this option
  is used, the caller is responsible for closing the streams when done
  with them.

The ``Subprocess.STREAM`` option and the ``set_exit_callback`` and
``wait_for_exit`` methods do not work on Windows. There is
therefore no reason to use this class instead of
``subprocess.Popen`` on that platform.

.. versionchanged:: 5.0
   The ``io_loop`` argument (deprecated since version 4.1) has been removed.

Fargskwargsr   Nc           	      z   [         R                  R                  5       U l        / n/ nUR	                  S5      [
        R                  L aN  [        R                  " 5       u  pVXRS'   UR                  XV45        UR                  U5        [        U5      U l        UR	                  S5      [
        R                  L aN  [        R                  " 5       u  pxXS'   UR                  Xx45        UR                  U5        [        U5      U l        UR	                  S5      [
        R                  L aN  [        R                  " 5       u  pXS'   UR                  X45        UR                  U
5        [        U	5      U l         [        R                   " U0 UD6U l        U H  n[        R$                  " U5        M     U R"                  R&                  U l        S H5  n[)        X5      (       a  M  [+        X[-        U R"                  U5      5        M7     S U l        S U l        g !   U H  n[        R$                  " U5        M     e = f)Nstdinstdoutstderr)rO   rP   rQ   )r   IOLoopcurrentio_loopgetrJ   STREAMr   pipeextendappendr   rO   rP   rQ   
subprocessPopenproccloser2   hasattrsetattrgetattr_exit_callback
returncode)selfrL   rM   pipe_fdsto_closein_rin_wout_rout_werr_rerr_wfdattrs                r   __init__Subprocess.__init__   s   }},,. ::g*"3"33JD"7OOOTL)OOD!%d+DJ::h:#4#44779LE$8OOUN+OOE"&u-DK::h:#4#44779LE$8OOUN+OOE"&u-DK	"(($9&9DI
 BHHRL 99==1D4&&GDIIt$<= 2 #	 s   9H "H:callbackc                     Xl         [        R                  5         U [        R                  U R                  '   [        R                  U R                  5        g)aN  Runs ``callback`` when this process exits.

The callback takes one argument, the return code of the process.

This method uses a ``SIGCHLD`` handler, which is a global setting
and may conflict if you have other libraries trying to handle the
same signal.  If you are using more than one ``IOLoop`` it may
be necessary to call `Subprocess.initialize` first to designate
one ``IOLoop`` to run the signal handlers.

In many cases a close callback on the stdout or stderr streams
can be used as an alternative to an exit callback if the
signal handler is causing a problem.

Availability: Unix
N)ra   rJ   
initialize_waitingr2   _try_cleanup_process)rc   rp   s     r   set_exit_callbackSubprocess.set_exit_callback   s<    " '(,
DHH%''1r   raise_errorzFuture[int]c                 b   ^^ [        5       mS[        SS4UU4S jjnU R                  U5        T$ )a  Returns a `.Future` which resolves when the process exits.

Usage::

    ret = yield proc.wait_for_exit()

This is a coroutine-friendly alternative to `set_exit_callback`
(and a replacement for the blocking `subprocess.Popen.wait`).

By default, raises `subprocess.CalledProcessError` if the process
has a non-zero exit status. Use ``wait_for_exit(raise_error=False)``
to suppress this behavior and return the exit status without raising.

.. versionadded:: 4.2

Availability: Unix
retr   Nc                 f   > U S:w  a  T(       a  [        T[        U S5      5        g [        TU 5        g )Nr   unknown)r   CalledProcessErrorr   )ry   futurerw   s    r   rp   *Subprocess.wait_for_exit.<locals>.callback(  s-    axK5.sI> 363?r   )r   r"   ru   )rc   rw   rp   r}   s    ` @r   wait_for_exitSubprocess.wait_for_exit  s=    $ 	@# 	@$ 	@ 	@ 	x(r   c                     U R                   (       a  g[        R                  " 5       nUR                  [        R
                  U R                  5        SU l         g)a  Initializes the ``SIGCHLD`` handler.

The signal handler is run on an `.IOLoop` to avoid locking issues.
Note that the `.IOLoop` used for signal handling need not be the
same one used by individual Subprocess objects (as long as the
``IOLoops`` are each running in separate threads).

.. versionchanged:: 5.0
   The ``io_loop`` argument (deprecated since version 4.1) has been
   removed.

Availability: Unix
NT)_initializedasyncioget_event_loopadd_signal_handlersignalSIGCHLD_cleanupclsloops     r   rr   Subprocess.initialize4  s>     %%'=r   c                     U R                   (       d  g[        R                  " 5       nUR                  [        R
                  5        SU l         g)z Removes the ``SIGCHLD`` handler.NF)r   r   r   remove_signal_handlerr   r   r   s     r   uninitializeSubprocess.uninitializeI  s8     %%'""6>>2 r   c                 z    [        U R                  R                  5       5       H  nU R                  U5        M     g N)listrs   keysrt   )r   r2   s     r   r   Subprocess._cleanupR  s,    ))+,C$$S) -r   r2   c                     [         R                  " U[         R                  5      u  p#US:X  a  g X!:X  d   eU R                  R                  U5      nUR                  R                  UR                  U5        g ! [         a     g f = fr/   )	r   waitpidWNOHANGChildProcessErrorrs   r;   rT   add_callback_set_returncode)r   r2   ret_pidrD   subprocs        r   rt   Subprocess._try_cleanup_processW  su    	 jjbjj9OG a<~~,,""3'$$W%<%<fE ! 		s   'A9 9
BBrD   c                    [         R                  S:X  a  SU l        Op[        R                  " U5      (       a  [        R
                  " U5      * U l        O8[        R                  " U5      (       d   e[        R                  " U5      U l        U R                  U R                  l        U R                  (       a&  U R                  nS U l	        U" U R                  5        g g )Nr+   )
r    r6   rb   r   r<   r>   	WIFEXITEDr?   r\   ra   )rc   rD   rp   s      r   r   Subprocess._set_returncodec  s    <<7" DO~~f%%#%;;v#6"6||F++++"$.."8  $		**H"&DT__% r   )ra   rT   r2   r\   rb   rQ   rO   rP   )Tr   N)__name__
__module____qualname____firstlineno____doc__objectrV   r   rs   r   rn   r   r"   ru   boolr   classmethodrr   r   r   rt   r   __static_attributes__r   r   r   rJ   rJ      s    * XFLH%c %S %T %N2(C5$;*? 2D 2,  @    ( ! ! * * 	Fs 	Ft 	F 	F&c &d &r   rJ   r   r   )"r   r   r   r   r   rZ   r    r$   binasciir   tornado.concurrentr   r   r   tornador   tornado.iostreamr   tornado.logr	   typingr
   r   r   TYPE_CHECKINGr   r|   r"   r   r'   r1   rF   rH   rJ   r   r   r   <module>r      s      	    
   
  )   * *	  22 3    AE]C=]08]]@# w& w&r   