a
    õ£Beõ1  ã                   @   s  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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ed
œdd„Zdd
œdd„Zdadee ee edœdd„Zee d
œdd„ZG dd„ de ƒ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                	   C   s\   t du rdS z
t  ¡ W S  ty(   Y n0 zt d¡W S  ttfyL   Y n0 t 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© r   r   úR/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/tornado/process.pyr   0   s    

r   c                  C   sh   dt jvrd S dd l} zttt d¡ƒdƒ}W n* tyX   tt ¡ d ƒt 	¡ A }Y n0 |  
|¡ d S )NÚrandomr   é   iè  )ÚsysÚmodulesr   Úintr   r   Úurandomr   ÚtimeÚgetpidÚseed)r   r#   r   r   r   Ú_reseed_random@   s    
r$   )Únum_processesÚmax_restartsr   c           	         sP  t jdkrtdƒ‚|du rd}tdu s*J ‚| du s:| dkr@tƒ } t d| ¡ i ‰ ttt dœ‡ fdd	„}t	| ƒD ]}||ƒ}|durp|  S qpd}ˆ �rBt
 ¡ \}}|ˆ vr®q’ˆ  |¡}t
 |¡rÚt d
||t
 |¡¡ n8t
 |¡dk�rt d||t
 |¡¡ nt d||¡ q’|d7 }||k�r,tdƒ‚||ƒ}|dur’|S q’t  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                    s.   t  ¡ }|dkrtƒ  | a| S | ˆ |< d S d S ©Nr   )r   Úforkr$   Ú_task_id)r)   Úpid©Úchildrenr   r   Ústart_child~   s    z#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 normallyr   z"Too many child restarts, giving up)r   ÚplatformÚ	Exceptionr,   r   r   Úinfor   r	   Úranger   ÚwaitÚpopÚWIFSIGNALEDÚwarningÚWTERMSIGÚWEXITSTATUSÚRuntimeErrorÚexit)	r%   r&   r0   r)   ÚidZnum_restartsr-   ÚstatusZnew_idr   r.   r   Úfork_processesR   sV    



üü
r?   c                   C   s   t S )zpReturns the current task id, if any.

    Returns None if this process was not created by `fork_processes`.
    )r,   r   r   r   r   Útask_id²   s    r@   c                   @   sº   e Zd ZdZeƒ ZdZi ZdZe	e	ddœdd„Z
eegdf ddœdd	„Zdeddœdd„Zeddœdd„ƒZeddœdd„ƒZeddœdd„ƒZeeddœdd„ƒZeddœd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.

    FN)ÚargsÚkwargsr   c                 O   s~  t j ¡ | _g }g }| d¡tju rZt ¡ \}}||d< | 	||f¡ | 
|¡ t|ƒ| _| d¡tju r t ¡ \}}||d< | 	||f¡ | 
|¡ t|ƒ| _| d¡tju ræt ¡ \}	}
|
|d< | 	|	|
f¡ | 
|
¡ t|	ƒ| _ztj|i |¤Ž| _W n$   |D ]}t |¡ �q‚ Y n0 |D ]}t |¡ �q&| jj| _dD ]&}t| |ƒ�sFt| |t| j|ƒƒ �qFd | _d | _d S )NÚstdinÚstdoutÚstderr)rD   rE   rF   )r   ÚIOLoopÚcurrentÚio_loopÚgetrA   ÚSTREAMr   ÚpipeÚextendÚappendr   rD   rE   rF   Ú
subprocessÚPopenÚprocÚcloser-   ÚhasattrÚsetattrÚgetattrÚ_exit_callbackÚ
returncode)ÚselfrB   rC   Zpipe_fdsÚto_closeZin_rZin_wZout_rZout_wZerr_rZerr_wÚfdÚattrr   r   r   Ú__init__×   sF    






zSubprocess.__init__)Úcallbackr   c                 C   s*   || _ 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)rV   rA   Ú
initializeÚ_waitingr-   Ú_try_cleanup_process)rX   r]   r   r   r   Úset_exit_callbackþ   s    zSubprocess.set_exit_callbackTzFuture[int])Úraise_errorr   c                    s*   t ƒ ‰ tddœ‡ ‡fd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
        N)Úretr   c                    s,   | dkrˆrt ˆ t| dƒƒ n
tˆ | ƒ d S )Nr   Úunknown)r   ÚCalledProcessErrorr   )rc   ©Úfuturerb   r   r   r]   (  s
    
ÿz*Subprocess.wait_for_exit.<locals>.callback)r   r   ra   )rX   rb   r]   r   rf   r   Úwait_for_exit  s    	
zSubprocess.wait_for_exitr   c                    s8   ˆ j r
dS tj ¡ ‰t tj‡ ‡fdd„¡ˆ _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
        Nc                    s   ˆ  ˆ j¡S ©N)Úadd_callback_from_signalÚ_cleanup)ÚsigÚframe©ÚclsrI   r   r   Ú<lambda>H  ó    z'Subprocess.initialize.<locals>.<lambda>T)Ú_initializedr   rG   rH   ÚsignalÚSIGCHLDÚ_old_sigchld©ro   r   rn   r   r^   4  s    
þzSubprocess.initializec                 C   s$   | j s
dS t tj| j¡ d| _ dS )z Removes the ``SIGCHLD`` handler.NF)rr   rs   rt   ru   rv   r   r   r   ÚuninitializeL  s    zSubprocess.uninitializec                 C   s"   t | j ¡ ƒD ]}|  |¡ qd S ri   )Úlistr_   Úkeysr`   )ro   r-   r   r   r   rk   T  s    zSubprocess._cleanup)r-   r   c                 C   sd   zt  |t j¡\}}W n ty*   Y d S 0 |dkr8d S ||ksDJ ‚| j |¡}|j |j|¡ d S r*   )	r   ÚwaitpidÚWNOHANGÚChildProcessErrorr_   r6   rI   rj   Ú_set_returncode)ro   r-   Zret_pidr>   Zsubprocr   r   r   r`   Y  s    zSubprocess._try_cleanup_process)r>   r   c                 C   sp   t jdkrd| _n4t |¡r,t |¡ | _nt |¡s:J ‚t |¡| _| j| j_| j	rl| j	}d | _	|| jƒ d S )Nr'   éÿÿÿÿ)
r   r1   rW   r   r7   r9   Ú	WIFEXITEDr:   rQ   rV   )rX   r>   r]   r   r   r   r}   e  s    


zSubprocess._set_returncode)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚobjectrK   rr   r_   ru   r
   r\   r   r   ra   Úboolrh   Úclassmethodr^   rw   rk   r`   r}   r   r   r   r   rA   »   s"   ' rA   )N)"rƒ   r   r   rs   rO   r   r!   Úbinasciir   Ztornado.concurrentr   r   r   Útornador   Ztornado.iostreamr   Ztornado.logr   Útypingr	   r
   r   ÚTYPE_CHECKINGr   re   r   r   r$   r,   r?   r@   r„   rA   r   r   r   r   Ú<module>   s4    ÿþ`	