§
    ‹Ÿj˜1  ã                   óD  — 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m	Z	 ddl
mZmZ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mZmZ ej        rdd	lmZ ej        Zd
efd„Zdd„Zda	 ddee         dee         d
efd„Zd
ee         fd„Z  G d„ d¦  «        Z!dS )z„Utilities 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                  óà   — t           €dS 	 t          j        ¦   «         S # t          $ r Y nw xY w	 t          j        d¦  «        S # t
          t          f$ r Y nw xY wt          j        d¦  «         dS )z1Returns the number of processors on this machine.Né   ÚSC_NPROCESSORS_CONFz1Could not detect number of processors; assuming 1)	ÚmultiprocessingÚ	cpu_countÚNotImplementedErrorÚosÚsysconfÚAttributeErrorÚ
ValueErrorr	   Úerror© ó    ú\/var/www/finuniver-perm.ru/html/student/venv/lib/python3.11/site-packages/tornado/process.pyr   r   1   s’   € åÐØˆqðÝÔ(Ñ*Ô*Ð*øÝð ð ð ØˆðøøøðÝŒzÐ/Ñ0Ô0Ð0øÝ�JÐ'ð ð ð Øˆðøøøå„MÐEÑFÔFÐFØˆ1s   ‹ ž
+ª+¯A ÁAÁAc                  óH  — dt           j        vrd S dd l} 	 t          t	          t          j        d¦  «        ¦  «        d¦  «        }nG# t          $ r: t          t          j        ¦   «         dz  ¦  «        t          j	        ¦   «         z  }Y nw xY w|  
                    |¦  «         d S )NÚrandomr   é   iè  )ÚsysÚmodulesr   Úintr   r   Úurandomr   ÚtimeÚgetpidÚseed)r   r&   s     r   Ú_reseed_randomr'   A   s�   € Ø•s”{Ð"Ð"ØˆØ€M€M€Mð
5Ý•7�2œ: b™>œ>Ñ*Ô*¨BÑ/Ô/ˆˆøÝð 5ð 5ð 5Ý•4”9‘;”; Ñ%Ñ&Ô&­¬©¬Ñ4ˆˆˆð5øøøà
‡K‚K�ÑÔÐÐÐs   –/A ÁAB
Â	B
Únum_processesÚmax_restartsc                 óR  ‡	— t           j        dk    rt          d¦  «        ‚|€d}t          �J ‚| �| dk    rt	          ¦   «         } t          j        d| ¦  «         i Š	dt          dt          t                   fˆ	fd	„}t          | ¦  «        D ]} ||¦  «        }|�|c S Œd}‰	ròt          j        ¦   «         \  }}|‰	vrŒ‰	                     |¦  «        }t          j        |¦  «        r*t          j        d
||t          j        |¦  «        ¦  «         nYt          j        |¦  «        dk    r*t          j        d||t          j        |¦  «        ¦  «         nt          j        d||¦  «         ŒÉ|dz  }||k    rt#          d¦  «        ‚ ||¦  «        }|�|S ‰	°òt          j        d¦  «         dS )aq  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 windowsNéd   r   zStarting %d processesÚir   c                 óh   •— t          j        ¦   «         }|dk    rt          ¦   «          | a| S | ‰|<   d S ©Nr   )r   Úforkr'   Ú_task_id)r-   ÚpidÚchildrens     €r   Ústart_childz#fork_processes.<locals>.start_child   s;   ø€ ÝŒg‰iŒiˆØ�!Š8ˆ8åÑÔÐàˆHØˆHàˆH�S‰MØ�4r   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_processesrE   S   s  ø€ õ< „|�wÒÐõ Ð7Ñ8Ô8Ð8ØÐØˆõ ÐÐÐØÐ °Ò 2Ð 2Ý!™œˆÝ„LÐ(¨-Ñ8Ô8Ð8Ø€Hð
•sð 
�x­œ}ð 
ð 
ð 
ð 
ð 
ð 
õ �=Ñ!Ô!ð ð ˆØˆ[˜‰^Œ^ˆØˆ>ØˆIˆIˆIð à€LØ
ð Ý”g‘i”i‰ˆˆVØ�hÐÐØØ�\Š\˜#ÑÔˆÝŒ>˜&Ñ!Ô!ð 	ÝŒOØCØØÝ”˜FÑ#Ô#ñ	ô ð ð õ Œ^˜FÑ#Ô# qÒ(Ð(ÝŒOØEØØÝ”˜vÑ&Ô&ñ	ô ð ð õ ŒLÐ<¸bÀ#ÑFÔFÐFØØ˜ÑˆØ˜,Ò&Ð&ÝÐCÑDÔDÐDØ�˜R‘”ˆØÐØˆMð7 ð õ@ „HˆQ�K„K€K€K€Kr   c                  ó   — t           S )zpReturns the current task id, if any.

    Returns None if this process was not created by `fork_processes`.
    )r1   r   r   r   Útask_idrG   ³   s	   € õ €Or   c                   óî   — e Zd ZdZ e¦   «         ZdZi Zdededdfd„Z	de
egdf         ddfd	„Zddeddfd„Zedd„¦   «         Zedd„¦   «         Zedd„¦   «         Zededdfd„¦   «         Zdeddfd„ZdS )Ú
Subprocessa   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           	      óŒ  — t           j                             ¦   «         | _        g }g }|                     d¦  «        t
          j        u r[t          j        ¦   «         \  }}||d<   | 	                    ||f¦  «         | 
                    |¦  «         t          |¦  «        | _        |                     d¦  «        t
          j        u r[t          j        ¦   «         \  }}||d<   | 	                    ||f¦  «         | 
                    |¦  «         t          |¦  «        | _        |                     d¦  «        t
          j        u r[t          j        ¦   «         \  }	}
|
|d<   | 	                    |	|
f¦  «         | 
                    |
¦  «         t          |	¦  «        | _        	 t          j        |i |¤Ž| _        n#  |D ]}t          j        |¦  «         Œ‚ xY w|D ]}t          j        |¦  «         Œ| j        j        | _        dD ]6}t)          | |¦  «        s$t+          | |t-          | j        |¦  «        ¦  «         Œ7d | _        d | _        d S )NÚstdinÚstdoutÚstderr)rM   rN   rO   )r   ÚIOLoopÚcurrentÚio_loopÚgetrI   ÚSTREAMr   ÚpipeÚextendÚappendr   rM   rN   rO   Ú
subprocessÚPopenÚprocÚcloser2   ÚhasattrÚsetattrÚgetattrÚ_exit_callbackÚ
returncode)ÚselfrJ   rK   Úpipe_fdsÚto_closeÚin_rÚin_wÚout_rÚout_wÚerr_rÚerr_wÚfdÚattrs                r   Ú__init__zSubprocess.__init__×   s$  € Ý”}×,Ò,Ñ.Ô.ˆŒð ˆØˆØ�:Š:�gÑÔ¥*Ô"3Ð3Ð3Ýœ™œ‰JˆD�$Ø"ˆF�7‰OØ�OŠO˜T 4˜LÑ)Ô)Ð)Ø�OŠO˜DÑ!Ô!Ð!Ý% dÑ+Ô+ˆDŒJØ�:Š:�hÑÔ¥:Ô#4Ð4Ð4Ýœ7™9œ9‰LˆE�5Ø$ˆF�8ÑØ�OŠO˜U E˜NÑ+Ô+Ð+Ø�OŠO˜EÑ"Ô"Ð"Ý& uÑ-Ô-ˆDŒKØ�:Š:�hÑÔ¥:Ô#4Ð4Ð4Ýœ7™9œ9‰LˆE�5Ø$ˆF�8ÑØ�OŠO˜U E˜NÑ+Ô+Ð+Ø�OŠO˜EÑ"Ô"Ð"Ý& uÑ-Ô-ˆDŒKð	Ý"Ô(¨$Ð9°&Ð9Ð9ˆDŒIˆIøð	Øð ð �Ý”˜‘”��ØøøøØð 	ð 	ˆBÝŒH�R‰LŒLˆLˆLØ”9”=ˆŒØ1ð 	>ð 	>ˆDÝ˜4 Ñ&Ô&ð >Ý˜˜d¥G¨D¬I°tÑ$<Ô$<Ñ=Ô=Ð=øØ"ˆÔØˆŒˆˆs   ÆF4 Æ4GÚcallbackc                 ó¬   — || _         t                               ¦   «          | t          j        | j        <   t                               | j        ¦  «         dS )a¦  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)r_   rI   Ú
initializeÚ_waitingr2   Ú_try_cleanup_process)ra   rm   s     r   Úset_exit_callbackzSubprocess.set_exit_callbackþ   sI   € ð" 'ˆÔÝ×ÒÑÔÐØ(,�
Ô˜DœHÑ%Ý×'Ò'¨¬Ñ1Ô1Ð1Ð1Ð1r   TÚraise_errorzFuture[int]c                 óp   ‡‡— t          ¦   «         Šdt          ddfˆˆfd„}|                      |¦  «         ‰S )a2  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                 óx   •— | dk    r"‰r t          ‰t          | d¦  «        ¦  «         d S t          ‰| ¦  «         d S )Nr   Úunknown)r   ÚCalledProcessErrorr   )ru   Úfuturers   s    €€r   rm   z*Subprocess.wait_for_exit.<locals>.callback(  sT   ø€ Ø�aŠxˆx˜Kˆxå5ØÕ.¨s°IÑ>Ô>ñô ð ð ð õ 3°6¸3Ñ?Ô?Ð?Ð?Ð?r   )r   r"   rr   )ra   rs   rm   ry   s    ` @r   Úwait_for_exitzSubprocess.wait_for_exit  sd   øø€ õ$ ‘”ˆð	@�#ð 	@ $ð 	@ð 	@ð 	@ð 	@ð 	@ð 	@ð 	@ð 	×Ò˜xÑ(Ô(Ð(Øˆr   c                 ó–   — | j         rdS t          j        ¦   «         }|                     t          j        | j        ¦  «         d| _         dS )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   ro   zSubprocess.initialize4  sJ   € ð Ôð 	ØˆFÝÔ%Ñ'Ô'ˆØ×Ò¥¤°´Ñ=Ô=Ð=ØˆÔÐÐr   c                 óŠ   — | j         sdS t          j        ¦   «         }|                     t          j        ¦  «         d| _         dS )z Removes the ``SIGCHLD`` handler.NF)r|   r}   r~   Úremove_signal_handlerr€   r�   rƒ   s     r   ÚuninitializezSubprocess.uninitializeI  sF   € ð Ôð 	ØˆFÝÔ%Ñ'Ô'ˆØ×"Ò"¥6¤>Ñ2Ô2Ð2Ø ˆÔÐÐr   c                 ó‚   — t          | j                             ¦   «         ¦  «        D ]}|                      |¦  «         Œd S ©N)Úlistrp   Úkeysrq   )r„   r2   s     r   r‚   zSubprocess._cleanupR  sH   € å˜œ×)Ò)Ñ+Ô+Ñ,Ô,ð 	*ð 	*ˆCØ×$Ò$ SÑ)Ô)Ð)Ð)ð	*ð 	*r   r2   c                 ó  — 	 t          j        |t           j        ¦  «        \  }}n# t          $ r Y d S w xY w|dk    rd S ||k    sJ ‚| j                             |¦  «        }|j                             |j        |¦  «         d S r/   )	r   ÚwaitpidÚWNOHANGÚChildProcessErrorrp   r:   rR   Úadd_callbackÚ_set_returncode)r„   r2   Úret_pidrC   Úsubprocs        r   rq   zSubprocess._try_cleanup_processW  s“   € ð	Ý œj¨­b¬jÑ9Ô9‰OˆG�V�VøÝ ð 	ð 	ð 	ØˆFˆFð	øøøà�aŠ<ˆ<ØˆFØ˜#Š~ˆ~ˆ~ˆ~Ø”,×"Ò" 3Ñ'Ô'ˆØŒ×$Ò$ WÔ%<¸fÑEÔEÐEÐEÐEs   ‚"% ¥
3²3rC   c                 ób  — t           j        dk    rd| _        n^t          j        |¦  «        rt          j        |¦  «         | _        n/t          j        |¦  «        sJ ‚t          j        |¦  «        | _        | j        | j        _        | j	        r | j	        }d | _	         || j        ¦  «         d S d S )Nr+   éÿÿÿÿ)
r    r5   r`   r   r;   r=   Ú	WIFEXITEDr>   rZ   r_   )ra   rC   rm   s      r   r’   zSubprocess._set_returncodec  s²   € ÝŒ<˜7Ò"Ð"Ø ˆDŒOˆOåŒ~˜fÑ%Ô%ð 9Ý#%¤;¨vÑ#6Ô#6Ð"6�”�å”| FÑ+Ô+Ð+Ð+Ð+Ý"$¤.°Ñ"8Ô"8�”ð  $œˆŒ	ÔØÔð 	&ØÔ*ˆHØ"&ˆDÔØˆH�T”_Ñ%Ô%Ð%Ð%Ð%ð	&ð 	&r   )T©r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚobjectrT   r|   rp   r   rl   r   r"   rr   Úboolrz   Úclassmethodro   rˆ   r‚   rq   r’   r   r   r   rI   rI   ¼   sj  € € € € € ðð ð* ˆV‰XŒX€Fà€LØ€Hð%˜cð %¨Sð %°Tð %ð %ð %ð %ðN2¨(°C°5¸$°;Ô*?ð 2ÀDð 2ð 2ð 2ð 2ð,ð ¨ð ¸ð ð ð ð ð@ ð ð  ð  ñ „[ð ð( ð!ð !ð !ñ „[ð!ð ð*ð *ð *ñ „[ð*ð ð	F sð 	F¨tð 	Fð 	Fð 	Fñ „[ð	Fð& cð &¨dð &ð &ð &ð &ð &ð &r   rI   r˜   rŠ   )"rœ   r}   r   r   r€   rX   r    r$   Úbinasciir   Útornado.concurrentr   r   r   Útornador   Útornado.iostreamr   Útornado.logr	   Útypingr
   r   r   ÚTYPE_CHECKINGr   rx   r"   r   r'   r1   rE   rG   rI   r   r   r   ú<module>r§      sû  ðð ð ð €€€Ø 	€	€	€	Ø Ð Ð Ð Ø €€€Ø Ð Ð Ð Ø 
€
€
€
Ø €€€à Ð Ð Ð Ð Ð ðð ð ð ð ð ð ð ð ð ð
 Ð Ð Ð Ð Ð Ø )Ð )Ð )Ð )Ð )Ð )Ø Ð Ð Ð Ð Ð à €€€Ø *Ð *Ð *Ð *Ð *Ð *Ð *Ð *Ð *Ð *à	Ôð ØÐÐÐÐÐð  Ô2Ð ð�3ð ð ð ð ð ð ð ð ð €ð AEð]ð ]Ø˜C”=ð]Ø08¸´ð]àð]ð ]ð ]ð ]ð@�˜#”ð ð ð ð ðw&ð w&ð w&ð w&ð w&ñ w&ô w&ð w&ð w&ð w&r   