HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/env/lib/python3.10/site-packages/tornado/__pycache__/process.cpython-310.pyc
o

we�1�@sdZddlZddlZddlZddlZddlZddlZddlZddlm	Z	ddl
mZmZm
Z
ddlmZddlmZddlmZddlZddlmZmZmZejrWdd	lmZejZd
efdd�Zdd
d�Zda	ddeedeed
efdd�Zd
eefdd�Z Gdd�de!�Z"dS)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	Cs\tdurdSzt��WStyYnwzt�d�WSttfy&Ynwt�d�dS)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�rr�H/home/arjun/projects/env/lib/python3.10/site-packages/tornado/process.pyr1s
��
rcCshdtjvrdSddl}zttt�d��d�}Wnty,tt��d�t�	�A}Ynw|�
|�dS)N�randomr�i�)�sys�modulesr�intrr�urandomr�time�getpid�seed)rr"rrr�_reseed_randomAs
�r#�
num_processes�max_restartsc	sNtjdkr	td��|durd}tdusJ�|dus|dkr t�}t�d|�i�dtdttf�fd	d
�}t	|�D]}||�}|durG|Sq9d}�r�t
��\}}|�vrWqJ��|�}t
�
|�rmt�d||t
�|��nt
�|�dkr�t�d||t
�|��nt�d
||�qJ|d7}||kr�td��||�}|dur�|S�sLt�d�dS)aqStarts 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
    �win32zfork not available on windowsN�drzStarting %d processes�ir
cs*t��}|dkrt�|a|S|�|<dS�Nr)r�forkr#�_task_id)r(�pid��childrenrr�start_childsz#fork_processes.<locals>.start_childz1child %d (pid %d) killed by signal %d, restartingz3child %d (pid %d) exited with status %d, restartingz!child %d (pid %d) exited normallyrz"Too many child restarts, giving up)r�platform�	Exceptionr+rr�inforr	�ranger�wait�pop�WIFSIGNALED�warning�WTERMSIG�WEXITSTATUS�RuntimeError�exit)	r$r%r/r(�id�num_restartsr,�status�new_idrr-r�fork_processesSsZ
�

��� r@cCstS)zpReturns the current task id, if any.

    Returns None if this process was not created by `fork_processes`.
    )r+rrrr�task_id�srAc@s�eZdZdZe�ZdZiZdededdfdd�Z	d	e
egdfddfd
d�Zdd
e
ddfdd�Zeddd��Zeddd��Zeddd��Zededdfdd��Zdeddfdd�ZdS)�
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
Osptj��|_g}g}|�d�tjur-t��\}}||d<|�	||f�|�
|�t|�|_|�d�tjurPt��\}}||d<|�	||f�|�
|�t|�|_
|�d�tjurst��\}	}
|
|d<|�	|	|
f�|�
|
�t|	�|_ztj|i|��|_Wn|D]}t�|�q��|D]}t�|�q�|jj|_dD]}t||�s�t||t|j|��q�d|_d|_dS)N�stdin�stdout�stderr)rErFrG)r�IOLoop�current�io_loop�getrB�STREAMr�pipe�extend�appendrrErFrG�
subprocess�Popen�proc�closer,�hasattr�setattr�getattr�_exit_callback�
returncode)
�selfrCrD�pipe_fds�to_close�in_r�in_w�out_r�out_w�err_r�err_w�fd�attrrrr�__init__�sH







�
zSubprocess.__init__�callbackcCs*||_t��|tj|j<t�|j�dS)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)rWrB�
initialize�_waitingr,�_try_cleanup_process)rYrerrr�set_exit_callback�szSubprocess.set_exit_callbackT�raise_errorzFuture[int]cs,t��dtddf��fdd�}|�|��S)a2Returns 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
Ncs.|dkr�rt�t|d��dSt�|�dS)Nr�unknown)r�CalledProcessErrorr)rk��futurerjrrre(s

�z*Subprocess.wait_for_exit.<locals>.callback)rrri)rYrjrerrnr�
wait_for_exits
	zSubprocess.wait_for_exitcCs,|jrdSt��}|�tj|j�d|_dS)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�looprrrrf4s

zSubprocess.initializecCs(|jsdSt��}|�tj�d|_dS)z Removes the ``SIGCHLD`` handler.NF)rqrrrs�remove_signal_handlerrurvrxrrr�uninitializeIs

zSubprocess.uninitializecCs"t|j���D]}|�|�qdS�N)�listrg�keysrh)ryr,rrrrwRs�zSubprocess._cleanupr,cCsdzt�|tj�\}}Wn
tyYdSw|dkrdS||ks"J�|j�|�}|j�|j|�dSr))	r�waitpid�WNOHANG�ChildProcessErrorrgr5rJ�add_callback�_set_returncode)ryr,�ret_pidr>�subprocrrrrhWs�zSubprocess._try_cleanup_processr>cCsttjdkr	d|_nt�|�rt�|�|_n
t�|�sJ�t�|�|_|j|j_|j	r8|j	}d|_	||j�dSdS)Nr&���)
rr0rXrr6r8�	WIFEXITEDr9rRrW)rYr>rerrrr�cs


�zSubprocess._set_returncode)T�r
N)�__name__�
__module__�__qualname__�__doc__�objectrLrqrgr
rdrrri�boolrp�classmethodrfr|rwrhr�rrrrrB�s"' rBr�r})#r�rrrrrurPrr �binasciir�tornado.concurrentrrr�tornador�tornado.iostreamr�tornado.logr�typingr	r
r�
TYPE_CHECKINGrrmrrr#r+r@rAr�rBrrrr�<module>s>
���
�`