o
    Þ­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œd0d'd(„Z2d1d*d+„Z3d2d-d.„Z4e5d/krÅe4ƒ  dS dS )3é    )Ú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Ú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 r¶ttˆjƒ‰tttˆjƒƒ‰ tƒ }g }	|r”||d
 d  tk r~n| ¡ \}
}|
 ¡  ˆ |
¡ |	 |
¡ |sstdd�� |	D ]	}| ¡ I dH  qœW d  ƒ n1 s°w   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 söw   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 �s�w   Y  W d  ƒI dH  dS 1 I dH �s¦w   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.

    Ú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rŠt|tƒsˆJ ‚|‚|S )	Nó   
é2   ó    )ó   RETURNó	   EXCEPTIONú-Worker process returned unexpected response: T©Úshieldr4   )ÚsendÚreceive_untilÚsplitÚRuntimeErrorÚreceive_exactlyÚintÚBaseExceptionÚdiscardÚkillr   ÚacloseÚProcessLookupErrorÚ
isinstancer   r   ÚpickleÚloads)r-   ÚresponseÚstatusÚlengthÚpickled_responseÚexcÚretval©ÚbufferedÚprocessÚstdinÚworkers© úM/var/www/html/CropPilot/venv/lib/python3.10/site-packages/anyio/to_process.pyÚsend_raw_commandE   s>   €ÿ
ÿ€ÿ€ô
z"run_sync.<locals>.send_raw_commandNÚrun)Úprotocolr   r   Tr6   z-uz-m)rO   Ústdouté   é   ó   READY
r5   Ú__main__Ú__file__Úinitz*Error during worker process initialization)r-   r.   r,   r/   )-r   rD   ÚdumpsÚHIGHEST_PROTOCOLr!   Úgetr"   ÚLookupErrorÚsetr   r   Ú#setup_process_pool_exit_at_shutdownr   ÚpopÚ
returncoder   r   rO   r   r   rV   r   ÚWORKER_MAX_IDLE_TIMEÚpopleftr@   ÚremoveÚappendr   rA   ÚsysÚ
executableÚ__name__r   Ú
subprocessÚPIPEr   Úreceiver   ÚgetattrÚmodulesÚpathr   r>   Úaddr   )r&   r$   r%   r(   rS   ÚrequestÚidle_workersÚ
idle_sinceÚnowÚkilled_processesÚprocess_to_killÚkilled_processÚcommandÚmessageÚmain_module_pathÚpickledrJ   rQ   rL   rR   r   ,   s¬   €!

û

ÿ

ù	ÿÿ
å
ÿ
ÿ
ÿ
ÿ
þÿþ€þ

û¾
Fÿü0¾r   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#   r_   r`   r   ÚosÚ	cpu_countra   )r%   rQ   rQ   rR   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= |r®tj |¡r®ztd|ƒ}	|	r˜|	jr˜t|	ƒ}
|	j |
¡ |
t jd< W n ty­ } z|}W Y d }~nd }~ww z|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 }~ww |j d
|t|ƒf ¡ |j |¡ t|tƒrþ|‚q)NÚwrY   TrT   r\   rZ   Ú__mp_main__r4   r3   s   %s %d
)ri   rO   rV   Úopenr   ÚdevnullÚbufferÚwriterD   ÚloadÚEOFErrorr>   rq   rp   Úisfiler	   Úloaderr   Úexec_moduler]   r^   ÚlenrC   Ú
SystemExit)rO   rV   rK   Ú	exceptionrz   r(   rJ   r&   r|   ÚspecÚmainrG   r}   rQ   rQ   rR   r   Î   sp   €ÿ€ÿ



€€ÿ€€ý
Ñr   rZ   )
r&   r'   r(   r)   r$   r*   r%   r+   r,   r   )r,   r   )r,   r�   )6Ú
__future__r   Ú__all__r   rD   rl   ri   Úcollectionsr   Úcollections.abcr   Úimportlib.utilr   r	   Útypingr
   r   Ú_core._eventloopr   r   r   Ú_core._exceptionsr   Ú_core._subprocessesr   Ú_core._synchronizationr   Ú_core._tasksr   r   Úabcr   r   r   Úlowlevelr   r   Ústreams.bufferedr   Úversion_infor   r   Útyping_extensionsre   r   r    r!   Ú__annotations__r"   r#   r   r   r   rk   rQ   rQ   rQ   rR   Ú<module>   sL    
ÿü 

;
ÿ