Format: application/x-python-code
⬇ Download File (6.4 KB)
   1  o
   2  
   3  ;'h{%�@s�UddlmZddlZddlZddlZddlZddlmZddlm	Z	ddl
   4  mZmZddl
   5  mZmZddlmZmZmZdd	lmZdd
   6  lmZddlmZddlmZmZdd
   7  lmZmZm Z ddl!m"Z"m#Z#ddl$m%Z%ej&dkr}ddl
   8  m'Z'm(Z(nddl)m'Z'm(Z(dZ*ed�Z+e'd�Z,e"d�Z-de.d<e"d�Z/de.d<e"d�Z0de.d<ddd�d/d&d'�Z1d0d)d*�Z2d1d,d-�Z3e4d.kr�e3�dSdS)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�
   9  fail_after)�ByteReceiveStream�ByteSendStream�Process)�RunVar�checkpoint_if_cancelled)�BufferedByteReceiveStream)��)�TypeVarTuple�Unpacki,�T_Retval�PosArgsT�_process_pool_workerszRunVar[set[Process]]�_process_pool_idle_workersz$RunVar[deque[tuple[Process, float]]]�_default_process_limiterzRunVar[CapacityLimiter]F)�cancellable�limiter�func�&Callable[[Unpack[PosArgsT]], T_Retval]�args�Unpack[PosArgsT]r!�boolr"�CapacityLimiter | None�returnc�sZ�d����fdd�}t�IdHtjd||ftjd	�}z
  10  t���t��}WntyEt��t	�}t���t�|�t
  11  ����Ynw|pJt�4IdH��O|r�|�
  12  �\�}�jdur�tt�j��ttt�j���t�}g}	|r�||d
  13  dtkr~n|��\}
  14  }|
  15  ����|
  16  �|	�|
  17  �|sstdd
  18  ��|	D]	}|��IdHq�Wd�n1s�wYn�����|sStjddtg}t |t!j"t!j"d�IdH�zTtt�j��ttt�j���t#d����$d�IdH}
  19  Wd�n1s�wY|
  20  dk�rt%d|
  21  ����t&tj'ddd�}tjdtj(|ftjd	�}||�IdHWn!t%t)�f�y0�t*�yE}z	���t%d�|�d}~ww��+��t|d
  22  ��:z)tt,||�IdH�W��v�rj|��t�f�Wd�Wd�IdHS��v�r�|��t�f�ww1�s�wYWd�IdHdS1IdH�s�wYdS)a�
  23      Call the given function with the given arguments in a worker process.
  24  
  25      If the ``cancellable`` option is enabled and the task waiting for its completion is
  26      cancelled, the worker process running it will be abruptly terminated using SIGKILL
  27      (or ``terminateProcess()`` on Windows).
  28  
  29      :param func: a callable
  30      :param args: positional arguments for the callable
  31      :param cancellable: ``True`` to allow cancellation of the operation while it's
  32          running
  33      :param limiter: capacity limiter to use to limit the total amount of processes
  34          running (if omitted, the default limiter is used)
  35      :return: an awaitable that yields the return value of the function.
  36  
  37      �pickled_cmd�bytesr)�objectc�s�z/��|�IdH��dd�IdH}|�d�\}}|dvr%td|������t|��IdH}WnGtyw}z;����z"���t	dd����
  38  �IdHWd�n1sYwYWn	tyhYnwt|t
  39  ��rp�t|�d}~wwt�|�}|dkr�t|t�s�J�|�|S)	N�
  40  �2� )�RETURN�	EXCEPTION�-Worker process returned unexpected response: T��shieldr1)�sendZ
  41  receive_until�split�RuntimeErrorZreceive_exactly�int�
  42  BaseException�discard�killr�aclose�ProcessLookupError�
  43  isinstancerr
  44  �pickle�loads)r*�response�status�lengthZpickled_response�exc�retval��buffered�process�stdin�workers��FC:\Users\shubh\Videos\agent\nova\Lib\site-packages\anyio/to_process.py�send_raw_command=s>��
  45  �����
  46  z"run_sync.<locals>.send_raw_commandN�run)�protocolrr	Tr3z-uz-m)rI�stdout���READY
  47  r2�__main__�__file__�initz*Error during worker process initialization)r*r+r)r,)-rr?�dumps�HIGHEST_PROTOCOLr�getr�LookupError�setrrZ#setup_process_pool_exit_at_shutdown�current_default_process_limiter�pop�
  48  returncoderrrIrrrPr
  49  �WORKER_MAX_IDLE_TIME�popleftr;�remove�appendrr<�sys�
  50  executable�__name__r�
  51  subprocess�PIPErZreceiver
  52  �getattr�modules�pathrr9�addr)r#r!r"r%rM�requestZidle_workersZ
  53  idle_since�nowZkilled_processesZprocess_to_killZkilled_process�command�message�main_module_path�pickledrDrKrFrL�run_sync&s��!
  54  
  55  �
  56  
  57  �
  58  
  59  �	��
  60  �
  61  �
  62  �
  63  �
  64  �
  65  �����
  66  
  67  ��
  68  F��0�rrrcCs<zt��WStytt��pd�}t�|�|YSw)z�
  69      Return the capacity limiter that is used by default to limit the number of worker
  70      processes.
  71  
  72      :return: a capacity limiter object
  73  
  74      �)r rYrZr�os�	cpu_countr[)r"rKrKrLr\�s
  75  
  76  �r\�Nonec
  77  
  78  Cs�tj}tj}ttj�t_ttjd�t_|j�d�	d}}z
  79  t�	|j�^}}Wnt
  80  y2YdStyE}z|}WYd}~nmd}~ww|dkri|\}}z||�}WnYtyh}z|}WYd}~nJd}~ww|dkr�|\t_}tj
  81  d=|r�tj�|�r�ztd|�}	|	r�|	jr�t|	�}
  82  |	j�|
  83  �|
  84  tj
  85  d<Wnty�}z|}WYd}~nd}~wwz|dur�d}t�|tj�}n	d	}t�|tj�}Wnty�}z|}d}t�|tj�}WYd}~nd}~ww|j�d
  86  |t|�f�|j�|�t|t�r�|�q)N�wrSTrNrVrT�__mp_main__r1r0s%s %d
  87  )rcrIrP�openrt�devnull�buffer�writer?�load�EOFErrorr9rjri�isfiler�loaderr�exec_modulerWrX�lenr>�
  88  SystemExit)
  89  rIrPrE�	exceptionrnr%rDr#rp�spec�mainrBrqrKrKrL�process_worker�sp����
  90  
  91  
  92  
  93  ������
  94  �r�rT)
  95  r#r$r%r&r!r'r"r(r)r)r)r)r)rv)5�
  96  __future__rrtr?rfrc�collectionsr�collections.abcr�importlib.utilrr�typingrrZ_core._eventloopr
  97  rrZ_core._exceptionsr
  98  Z_core._subprocessesrZ_core._synchronizationrZ_core._tasksrr�abcrrrZlowlevelrrZstreams.bufferedr�version_inforrZtyping_extensionsr_rrr�__annotations__rr rrr\r�rerKrKrKrL�<module>sJ
  99  ��
 100  
 101  ;
 102  �