o
    ~Zh{%                     @  s  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% 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Z/de.d< e"dZ0de.d< dddd/d&d'Z1d0d)d*Z2d1d,d-Z3e4d.kre3  dS dS )2    )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)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limiterfunc&Callable[[Unpack[PosArgsT]], T_Retval]argsUnpack[PosArgsT]r!   boolr"   CapacityLimiter | Nonereturnc                  sZ  d fdd}t  I dH  tjd| |ftjd	}z
t t }W n tyE   t t	 }t t| t
  Y nw |pJt 4 I dH O |r| \}jdu rttjtttj t }g }	|r||d
 d  tk r~n| \}
}|
  |
 |	|
 |sstdd |	D ]	}| I dH  qW d   n1 sw   Y  n |sStjddtg}t |t!j"t!j"dI dH zTttjtttj t#d  $dI dH }W d   n1 sw   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 n! t%t) fy0     t*yE } z	  t%d|d}~ww + t| d: z)tt,||I dH W v rj|t f W  d   W  d  I dH  S v r|t f w w 1 sw   Y  W d  I dH  dS 1 I dH sw   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.

    pickled_cmdbytesr)   objectc                   s  z/ | I d H   ddI d H }|d\}}|dvr%td| t|I d H }W nG tyw } z; z"  t	dd 
 I d H  W d    n1 sYw   Y  W n	 tyh   Y nw t|t rp t|d }~ww t|}|dkrt|tsJ ||S )	N   
2       )   RETURN	   EXCEPTION-Worker process returned unexpected response: T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bufferedprocessstdinworkers G/var/www/html/lang_env/lib/python3.10/site-packages/anyio/to_process.pysend_raw_command=   s>   

z"run_sync.<locals>.send_raw_commandNrun)protocolr   r	   Tr3   z-uz-m)rI   stdout         READY
r2   __main____file__initz*Error during worker process initialization)r*   r+   r)   r,   )-r   r?   dumpsHIGHEST_PROTOCOLr   getr   LookupErrorsetr   r   Z#setup_process_pool_exit_at_shutdowncurrent_default_process_limiterpop
returncoder   r   rI   r   r   rP   r
   WORKER_MAX_IDLE_TIMEpopleftr;   removeappendr   r<   sys
executable__name__r   
subprocessPIPEr   Zreceiver   getattrmodulespathr   r9   addr   )r#   r!   r"   r%   rM   requestZidle_workersZ
idle_sincenowZkilled_processesZprocess_to_killZkilled_processcommandmessagemain_module_pathpickledrD   rK   rF   rL   run_sync&   s   !





	








F0rr   r   c                  C  s<   zt  W S  ty   tt pd} t |  |  Y S w )z
    Return the capacity limiter that is used by default to limit the number of worker
    processes.

    :return: a capacity limiter object

       )r    rY   rZ   r   os	cpu_countr[   )r"   rK   rK   rL   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
y2   Y d S  tyE } z|}W Y d }~nmd }~ww |dkri|\}}z|| }W nY tyh } z|}W Y d }~nJd }~ww |dkr|\t _}t jd= |rtj|rztd|}	|	r|	jrt|	}
|	j|
 |
t jd< W n ty } z|}W Y d }~nd }~ww z|d urd}t|tj}n	d	}t|tj}W n ty } z|}d}t|tj}W Y d }~nd }~ww |jd
|t|f  |j| t|tr|q)NwrS   TrN   rV   rT   __mp_main__r1   r0   s   %s %d
)rc   rI   rP   openrt   devnullbufferwriter?   loadEOFErrorr9   rj   ri   isfiler   loaderr   exec_modulerW   rX   lenr>   
SystemExit)rI   rP   rE   	exceptionrn   r%   rD   r#   rp   specmainrB   rq   rK   rK   rL   process_worker   sp   




r   rT   )
r#   r$   r%   r&   r!   r'   r"   r(   r)   r   )r)   r   )r)   rv   )5
__future__r   rt   r?   rf   rc   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    rr   r\   r   re   rK   rK   rK   rL   <module>   sJ    
 

;
