Format: application/x-python-code
⬇ Download File (6.0 KB)
   1  o
   2  
   3  ;'h�@s\UddlmZddlZddlZddlZddlZddlmZddlm	Z	ddl
   4  mZddlm
   5  Z
   6  mZmZddlmZmZdd	lmZdd
   7  lmZddlmZejdkr[dd
   8  lmZmZndd
   9  lmZmZdZded<dZded<dZ ded<dZ!ded<dZ"ed�Z#ed�Z$eedd�Z%eed�Z&Gdd�d�Z'd.d d!�Z(dd"�d/d)d*�Z)d0d,d-�Z*dS)1�)�annotationsN)�deque)�Callable)�dedent)�Any�Final�TypeVar�)�current_time�	to_thread)�BrokenWorkerIntepreter)�CapacityLimiter)�RunVar)��)�TypeVarTuple�Unpack�r�UNBOUND�
  10  FMT_UNPICKLED�FMT_PICKLED��DEFAULT_CPU_COUNT��T_Retval�PosArgsT�WorkerZ_available_workers�_default_interpreter_limiterc@sneZdZUeed�dd�ZdZded<dZded	<d
  11  ed<d
  12  ed<d dd�Z	d dd�Z
  13  d!dd�Zd"dd�ZdS)#ra�
  14          import _interpqueues as queues
  15          import _interpreters as interpreters
  16          from pickle import loads, dumps, HIGHEST_PROTOCOL
  17  
  18          item = queues.get(queue_id)[0]
  19          try:
  20              func, args = loads(item)
  21              retval = func(*args)
  22          except BaseException as exc:
  23              is_exception = True
  24              retval = exc
  25          else:
  26              is_exception = False
  27  
  28          try:
  29              queues.put(queue_id, (retval, is_exception), FMT_UNPICKLED, UNBOUND)
  30          except interpreters.NotShareableError:
  31              retval = dumps(retval, HIGHEST_PROTOCOL)
  32              queues.put(queue_id, (retval, is_exception), FMT_PICKLED, UNBOUND)
  33          z<string>�execr�float�	last_usedF�bool�_initialized�int�_interpreter_id�	_queue_id�return�NonecCsNddl}ddl}|��|_|�dtt�|_d|_|�|j|jt	ttd��dS)NrrT)Zqueue_idrrr)
  34  �
  35  _interpqueues�
  36  _interpreters�creater$rrr%r"Zset___main___attrsr��self�queues�interpreters�r/�JC:\Users\shubh\Videos\agent\nova\Lib\site-packages\anyio/to_interpreter.py�
  37  initializeFs
  38  ��zWorker.initializecCs6ddl}ddl}|jr|�|j�|�|j�dSdS)Nr)r(r)r"�destroyr$r%r+r/r/r0r2Ws�zWorker.destroy�func�Callable[..., T_Retval]�args�
  39  tuple[Any]�tuple[Any, bool]c
  40  Cs�ddl}ddl}|js|��t�||ftj�}|�|j|t	t
  41  �|�|j|j
  42  �}r/t|��|�|j�dd�\\}}}	|	t	krFt�|�}||fS)Nrr)r(r)r"r1�pickle�dumps�HIGHEST_PROTOCOL�putr%rrrr$�	_run_funcr�get�loads)
  43  r,r3r5r-r.�payload�exc_info�res�is_exception�fmtr/r/r0�_call_s
  44  zWorker._call�limiterr
  45  rc�s,�tj|j|||d�IdH\}}|r|�|S)N�rE)r�run_syncrD)r,r3r5rE�resultrBr/r/r0�callxs��zWorker.callN)r&r')r3r4r5r6r&r7)r3r4r5r6rEr
  46  r&r)
  47  �__name__�
  48  __module__�__qualname__�compilerr<r �__annotations__r"r1r2rDrIr/r/r/r0r%s
  49  �
  50  
  51  
  52  �workers�
  53  deque[Worker]r&r'cCs|D]}|��q|��dS)N)r2�clear)rO�workerr/r/r0�
  54  _stop_workers�s
  55  rSrFr3�&Callable[[Unpack[PosArgsT]], T_Retval]r5�Unpack[PosArgsT]rE�CapacityLimiter | Nonec
  56  �sr�tjdkr
  57  td��|durt�}zt��}Wnty.t�}t�|�t	�
  58  t|�Ynw|4IdH�z|��}Wnt
  59  yHt�}YnwWd�IdHn1IdHsYwYz2|�|||�IdHWt�}|r�||djtkrxntj|��j|d�IdH|snt�|_|�|�St�}|r�||djtkr�ntj|��j|d�IdH|s�t�|_|�|�w)aF
  60      Call the given function with the given arguments in a subinterpreter.
  61  
  62      If the ``cancellable`` option is enabled and the task waiting for its completion is
  63      cancelled, the call will still run its course but its return value (or any raised
  64      exception) will be ignored.
  65  
  66      .. warning:: This feature is **experimental**. The upstream interpreter API has not
  67          yet been finalized or thoroughly tested, so don't rely on this for anything
  68          mission critical.
  69  
  70      :param func: a callable
  71      :param args: positional arguments for the callable
  72      :param limiter: capacity limiter to use to limit the total amount of subinterpreters
  73          running (if omitted, the default limiter is used)
  74      :return: the result of the call
  75      :raises BrokenWorkerIntepreter: if there's an internal error in a subinterpreter
  76  
  77      )r�
  78  z,subinterpreters require at least Python 3.13NrrF)�sys�version_info�RuntimeError�#current_default_interpreter_limiter�
  79  _idle_workersr=�LookupErrorr�set�atexit�registerrS�pop�
  80  IndexErrorrrIr
  81  r �MAX_WORKER_IDLE_TIMErrG�popleftr2�append)r3rEr5Zidle_workersrR�nowr/r/r0rG�sL�
  82  
  83  �
  84  ��(����rGr
  85  cCs<zt��WStytt��pt�}t�|�|YSw)z�
  86      Return the capacity limiter that is used by default to limit the number of
  87      concurrently running subinterpreters.
  88  
  89      Defaults to the number of CPU cores.
  90  
  91      :return: a capacity limiter object
  92  
  93      )rr=r]r
  94  �os�	cpu_countrr^rFr/r/r0r[�s
  95  
  96  
  97  �r[)rOrPr&r')r3rTr5rUrErVr&r)r&r
  98  )+�
  99  __future__rr_rgr8rX�collectionsr�collections.abcr�textwrapr�typingrrr�r
 100  rZ_core._exceptionsrZ_core._synchronizationr
 101  ZlowlevelrrYrrZtyping_extensionsrrNrrrrcrrr\rrrSrGr[r/r/r/r0�<module>s>
 102  �
 103  e
 104  �: