a
    ”˜ëjF&  ã                   @  s   U d dl mZ d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& ej'dkrþd dlm(Z(m)Z) nd dl*m(Z(m)Z) dZ+edƒZ,e(dƒZ-e#dƒZ.de/d< e#dƒZ0de/d< e#dƒZ1de/d< dddœddd d!dd"œd#d$„Z2d%d&œd'd(„Z3d)d&œd*d+„Z4e5d,k�rœe4ƒ  dS )-é    )Úannotations)Úcurrent_default_process_limiterÚprocess_workerÚrun_syncN)Ú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)ÚBufferedByteReceiveStream)é   é   )ÚTypeVarTupleÚUnpacki,  ÚT_RetvalÚPosArgsTÚ_process_pool_workerszRunVar[set[Process]]Ú_process_pool_idle_workersz$RunVar[deque[tuple[Process, float]]]Ú_default_process_limiterzRunVar[CapacityLimiter]F)ÚcancellableÚlimiterz&Callable[[Unpack[PosArgsT]], T_Retval]zUnpack[PosArgsT]Ú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)
    :raises NoEventLoopError: if no supported asynchronous event loop is running in the
        current thread
    :return: an awaitable that yields the return value of the function.

    ÚbytesÚobject)Ú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©Zshieldr1   )Ú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©ZbufferedÚprocessÚstdinÚworkers© úO/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/anyio/to_process.pyÚsend_raw_commandE   s2    ÿ
0

z"run_sync.<locals>.send_raw_commandNÚrun)Úprotocolr   r   Tr3   z-uz-m)rG   Ústdouté   é   ó   READY
r2   Ú__main__Ú__file__Úinitz*Error during worker process initialization)-r   r>   ÚdumpsÚHIGHEST_PROTOCOLr!   Úgetr"   ÚLookupErrorÚsetr   r   Z#setup_process_pool_exit_at_shutdownr   ÚpopÚ
returncoder   r   rG   r   r   rN   r   ÚWORKER_MAX_IDLE_TIMEÚpopleftr:   ÚremoveÚappendr   r;   ÚsysÚ
executableÚ__name__r   Ú
subprocessÚPIPEr   Zreceiver   ÚgetattrÚmodulesÚpathr   r8   Úaddr   )r'   r$   r%   r(   rK   ÚrequestZidle_workersZ
idle_sinceÚnowZkilled_processesZprocess_to_killZkilled_processÚcommandÚmessageÚmain_module_pathÚpickledrC   rI   rE   rJ   r   ,   s–    !


ÿ
2
ÿ
ÿ
0
ÿ
þÿþ

ý$ ÿ
r   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#   rW   rX   r   ÚosÚ	cpu_countrY   )r%   rI   rI   rJ   r   ¾   s    

r   Ú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�rh|\t _}t jd= |�rhtj |¡�rhz:td|ƒ}	|	�r:|	j�r:t|	ƒ}
|	j |
¡ |
t jd< W n* t�yf } z|}W Y d }~n
d }~0 0 z4|d u�rˆd}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ÚwrQ   rL   rT   rR   Ú__mp_main__r1   r0   s   %s %d
)r`   rG   rN   Úopenrp   ÚdevnullÚbufferÚwriter>   ÚloadÚEOFErrorr8   rg   rf   Úisfiler	   Úloaderr   Úexec_modulerU   rV   Úlenr=   Ú
SystemExit)rG   rN   rD   Ú	exceptionrk   r(   rC   r'   rm   ÚspecÚmainrA   rn   rI   rI   rJ   r   Î   sX    



$
r   rR   )6Ú
__future__r   Ú__all__rp   r>   rc   r`   Úcollectionsr   Úcollections.abcr   Ú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   Úversion_infor   r   Ztyping_extensionsr\   r   r    r!   Ú__annotations__r"   r#   r   r   r   rb   rI   rI   rI   rJ   Ú<module>   sH   
ÿü ;
