a
    !f$                     @  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	m
Z
 d dlmZ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r\e-  dS )(    )annotationsN)deque)module_from_specspec_from_file_location)CallableTypeVarcast   )current_timeget_asynclib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 |pt 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| \}   |	 qtdd* |	D ] I dH  qDW d   n1 sp0    Y  q qtjddtg}
t |
t!j"t!j"dI dH zttjtttj t#d   $dI dH }W d   n1 s0    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  q1 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 s0    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 Q/var/www/html/python-backend/venv/lib/python3.9/site-packages/anyio/to_process.pysend_raw_command4   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_processes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 }~nd }~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   __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   )/
__future__r   rl   r7   r^   r[   collectionsr   importlib.utilr   r   typingr   r   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<    <
