a
    ø£Bey$  ã                   @  sh  U d dl m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	 d dl
mZmZ d dlmZ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mZmZ ddlmZmZm Z  ddl!m"Z"m#Z# ddl$m%Z% dZ&edƒZ'e"dƒZ(de)d< e"dƒZ*de)d< e"dƒZ+de)d< dddœddddddœdd „Z,d!d"œd#d$„Z-d%d"œd&d'„Z.e/d(k�rde.ƒ  dS ))é    )ÚannotationsN)Údeque)ÚCallable)Úmodule_from_specÚspec_from_file_location)ÚTypeVarÚcasté   )Úcurrent_timeÚget_async_backendÚget_cancelled_exc_class)ÚBrokenWorkerProcess)Úopen_process)ÚCapacityLimiter)ÚCancelScopeÚ
fail_after)ÚByteReceiveStreamÚByteSendStreamÚProcess)ÚRunVarÚcheckpoint_if_cancelled)ÚBufferedByteReceiveStreami,  ÚT_RetvalÚ_process_pool_workerszRunVar[set[Process]]Ú_process_pool_idle_workersz$RunVar[deque[tuple[Process, float]]]Ú_default_process_limiterzRunVar[CapacityLimiter]F)ÚcancellableÚlimiterzCallable[..., T_Retval]ÚobjectÚboolzCapacityLimiter | None)ÚfuncÚargsr   r   Úreturnc                ‡  sŠ  dddœ‡ ‡‡‡fdd„}t ƒ I dH  tjd| |ftjd�}zt ¡ ‰t ¡ }W n> tyŽ   tƒ ‰t	ƒ }t ˆ¡ t |¡ t
ƒ  ˆ¡ Y n0 |p˜tƒ 4 I dH �šÊ |�rŠ| ¡ \‰}ˆjdu �r~ttˆjƒ‰tttˆjƒƒ‰ tƒ }g }	|�r4||d	 d
  tk �r
�q4| ¡ \}
}|
 ¡  ˆ |
¡ |	 |
¡ qêtdd��* |	D ]}| ¡ I dH  �qDW d  ƒ n1 �sp0    Y  �q¸ˆ ˆ¡ q¦tjddtg}t |t!j"t!j"d�I dH ‰zªttˆjƒ‰tttˆjƒƒ‰ t#dƒ�  ˆ  $d¡I dH }W d  ƒ n1 �sþ0    Y  |dk�r t%d|›�ƒ‚t&tj'd ddƒ}tjdtj(|ftjd�}||ƒI dH  W nR t%t)ƒ f�yv   ‚ Y n8 t*�y¬ } zˆ ¡  t%dƒ|‚W Y d}~n
d}~0 0 ˆ +ˆ¡ t| d��‚ zTtt,||ƒI dH ƒW ˆˆv �rö| ˆtƒ f¡ W  d  ƒ W  d  ƒI dH  S nˆˆv �r6| ˆtƒ f¡ 0 W d  ƒ n1 �sN0    Y  W d  ƒI dH  �q†1 I dH �s|0    Y  dS )a¸  
    Call the given function with the given arguments in a worker process.

    If the ``cancellable`` option is enabled and the task waiting for its completion is
    cancelled, the worker process running it will be abruptly terminated using SIGKILL
    (or ``terminateProcess()`` on Windows).

    :param func: a callable
    :param args: positional arguments for the callable
    :param cancellable: ``True`` to allow cancellation of the operation while it's
        running
    :param limiter: capacity limiter to use to limit the total amount of processes
        running (if omitted, the default limiter is used)
    :return: an awaitable that yields the return value of the function.

    Úbytesr   )Úpickled_cmdr"   c                 “  s.  z^ˆ  | ¡I d H  ˆ  dd¡I d H }| d¡\}}|dvrHtd|›�ƒ‚ˆ  t|ƒ¡I d H }W nœ tyú } z„ˆ ˆ¡ zDˆ ¡  t	dd�� ˆ 
¡ I d H  W d   ƒ n1 s°0    Y  W n tyÎ   Y n0 t|tƒ ƒrà‚ nt|‚W Y d }~n
d }~0 0 t |¡}|dk�r&t|tƒ�s J ‚|‚n|S d S )	Nó   
é2   ó    )ó   RETURNó	   EXCEPTIONú-Worker process returned unexpected response: T©Úshieldr)   )ÚsendZreceive_untilÚsplitÚRuntimeErrorZreceive_exactlyÚintÚBaseExceptionÚdiscardÚkillr   ÚacloseÚProcessLookupErrorÚ
isinstancer   r   ÚpickleÚloads)r$   ÚresponseÚstatusÚlengthZpickled_responseÚexcÚretval©ÚbufferedÚprocessÚstdinÚworkers© úS/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/anyio/to_process.pyÚsend_raw_command6   s2    ÿ
0

z"run_sync.<locals>.send_raw_commandNÚrun)Úprotocolr   r	   Tr+   z-uz-m)rA   Ústdouté   é   ó   READY
r*   Ú__main__Ú__file__Úinitz*Error during worker process initialization)-r   r7   ÚdumpsÚHIGHEST_PROTOCOLr   Úgetr   ÚLookupErrorÚsetr   r   Z#setup_process_pool_exit_at_shutdownÚcurrent_default_process_limiterÚpopÚ
returncoder   r   rA   r   r   rH   r
   ÚWORKER_MAX_IDLE_TIMEÚpopleftr3   ÚremoveÚappendr   r4   ÚsysÚ
executableÚ__name__r   Ú
subprocessÚPIPEr   Zreceiver   ÚgetattrÚmodulesÚpathr   r1   Úaddr   )r    r   r   r!   rE   ÚrequestZidle_workersZ
idle_sinceÚnowZkilled_processesZprocess_to_killZkilled_processÚcommandÚmessageÚmain_module_pathÚpickledr<   rC   r>   rD   Úrun_sync   s–    !


ÿ
2
ÿ
ÿ
0
ÿ
þÿþ

ý$ ÿ
rj   r   )r"   c                  C  s@   z
t  ¡ W S  ty:   tt ¡ p$dƒ} t  | ¡ |  Y S 0 dS )z“
    Return the capacity limiter that is used by default to limit the number of worker
    processes.

    :return: a capacity limiter object

    é   N)r   rQ   rR   r   ÚosÚ	cpu_countrS   )r   rC   rC   rD   rT   ¯   s    

rT   ÚNonec               
   C  s  t j} t j}ttjƒt _ttjdƒt _|j d¡ d  }}zt 	| j¡^}}W n: t
yb   Y d S  tyˆ } z|}W Y d }~nÚd }~0 0 |dkrÒ|\}}z||Ž }W n( tyÎ } z|}W Y d }~n
d }~0 0 nˆ|dk�rZ|\t _}t jd= |�rZz:td|ƒ}	|	�r,|	j�r,t|	ƒ}
|	j |
¡ |
t jd< W n* t�yX } z|}W Y d }~n
d }~0 0 z4|d u�rzd}t |tj¡}nd}t |tj¡}W n< t�yÊ } z"|}d}t |tj¡}W Y d }~n
d }~0 0 |j d	|t|ƒf ¡ |j |¡ t|tƒr2|‚q2d S )
NÚwrK   rF   rN   rL   Z__mp_main__r)   r(   s   %s %d
)r[   rA   rH   Úopenrl   ÚdevnullÚbufferÚwriter7   ÚloadÚEOFErrorr1   rb   ra   r   Úloaderr   Úexec_modulerO   rP   Úlenr6   Ú
SystemExit)rA   rH   r=   Ú	exceptionrf   r!   r<   r    rh   ÚspecÚmainr:   ri   rC   rC   rD   Úprocess_worker¿   sX    



$
r}   rL   )0Ú
__future__r   rl   r7   r^   r[   Úcollectionsr   Úcollections.abcr   Zimportlib.utilr   r   Útypingr   r   Z_core._eventloopr
   r   r   Z_core._exceptionsr   Z_core._subprocessesr   Z_core._synchronizationr   Z_core._tasksr   r   Úabcr   r   r   Zlowlevelr   r   Zstreams.bufferedr   rW   r   r   Ú__annotations__r   r   rj   rT   r}   r]   rC   rC   rC   rD   Ú<module>   s>   ÿü <
