Format: application/x-python-code
⬇ Download File (23.4 KB)
   1  o
   2  
   3  ;'h`O�@s�ddlmZddlZddlmZddlmZddlmZddl	m
   4  Z
   5  ddlmZd	d
   6  l
   7  mZd	dlmZd	dlmZd	d
   8  lmZmZedd�Gdd�d��Zedd�Gdd�d��Zedd�Gdd�d��Zedd�Gdd�d��Zedd�Gdd�d��ZGdd�d�ZGdd�de�ZGdd�d�ZGd d!�d!e�ZGd"d#�d#�ZGd$d%�d%�Z Gd&d'�d'e �Z!Gd(d)�d)�Z"Gd*d+�d+e"�Z#Gd,d-�d-�Z$dS).�)�annotationsN)�deque)�	dataclass)�
   9  TracebackType)�AsyncLibraryNotFoundError�)�
  10  checkpoint�)�get_async_backend)�BusyResourceError)�CancelScope)�TaskInfo�get_current_taskT)�frozenc@�eZdZUdZded<dS)�EventStatisticszR
  11      :ivar int tasks_waiting: number of tasks waiting on :meth:`~.Event.wait`
  12      �int�
  13  tasks_waitingN��__name__�
  14  __module__�__qualname__�__doc__�__annotations__�rr�RC:\Users\shubh\Videos\agent\nova\Lib\site-packages\anyio/_core/_synchronization.pyrs
  15  rc@s2eZdZUdZded<ded<ded<ded<d	S)
  16  �CapacityLimiterStatisticsa�
  17      :ivar int borrowed_tokens: number of tokens currently borrowed by tasks
  18      :ivar float total_tokens: total number of available tokens
  19      :ivar tuple borrowers: tasks or other objects currently holding tokens borrowed from
  20          this limiter
  21      :ivar int tasks_waiting: number of tasks waiting on
  22          :meth:`~.CapacityLimiter.acquire` or
  23          :meth:`~.CapacityLimiter.acquire_on_behalf_of`
  24      r�borrowed_tokens�float�total_tokensztuple[object, ...]�	borrowersrNrrrrrrs
  25  
  26  rc@s*eZdZUdZded<ded<ded<dS)	�LockStatisticsa
  27      :ivar bool locked: flag indicating if this lock is locked or not
  28      :ivar ~anyio.TaskInfo owner: task currently holding the lock (or ``None`` if the
  29          lock is not held by any task)
  30      :ivar int tasks_waiting: number of tasks waiting on :meth:`~.Lock.acquire`
  31      �bool�locked�TaskInfo | None�ownerrrNrrrrrr!,s
  32  
  33  r!c@s"eZdZUdZded<ded<dS)�ConditionStatisticsz�
  34      :ivar int tasks_waiting: number of tasks blocked on :meth:`~.Condition.wait`
  35      :ivar ~anyio.LockStatistics lock_statistics: statistics of the underlying
  36          :class:`~.Lock`
  37      rrr!Zlock_statisticsNrrrrrr&:s
  38  r&c@r)�SemaphoreStatisticszZ
  39      :ivar int tasks_waiting: number of tasks waiting on :meth:`~.Semaphore.acquire`
  40  
  41      rrNrrrrrr'Fs
  42  r'c@s>eZdZddd�Zddd�Zddd	�Zdd
  43  d�Zdd
  44  d�ZdS)�Event�returncCs&zt���WStyt�YSw�N)r
  45  �create_eventr�EventAdapter��clsrrr�__new__Qs
  46  
  47  �z
  48  Event.__new__�NonecC�t�)z&Set the flag, notifying all listeners.��NotImplementedError��selfrrr�setW�z	Event.setr"cCr1)z5Return ``True`` if the flag is set, ``False`` if not.r2r4rrr�is_set[r7zEvent.is_setc���t�)z�
  49          Wait until the flag has been set.
  50  
  51          If the flag has already been set when this method is called, it returns
  52          immediately.
  53  
  54          r2r4rrr�wait_s�z
  55  Event.waitrcCr1)z8Return statistics about the current state of this event.r2r4rrr�
  56  statisticsir7zEvent.statisticsN�r)r(�r)r0�r)r"�r)r)rrrr/r6r8r:r;rrrrr(Ps
  57  
  58  
  59  
  60  
  61  r(c@sfeZdZUdZded<dZded<ddd	�Zeddd��Zddd�Z	ddd�Z
  62  ddd�Zddd�ZdS)r,NzEvent | None�_internal_eventFr"�_is_setr)cC�
  63  t�|�Sr*��objectr/r-rrrr/r�
  64  zEventAdapter.__new__r(cCs,|jdurt���|_|jr|j��|jSr*)r@r
  65  r+rAr6r4rrr�_eventus
  66  
  67  
  68  zEventAdapter._eventr0cCs"|jdur
  69  d|_dS|j��dS�NT)r@rArFr6r4rrrr6~s
  70  
  71  zEventAdapter.setcCs|jdur|jS|j��Sr*)r@rAr8r4rrrr8�s
  72  
  73  zEventAdapter.is_setc���|j��IdHdSr*)rFr:r4rrrr:���zEventAdapter.waitrcCs|jdur
  74  tdd�S|j��S�Nr)r)r@rr;r4rrrr;��
  75  
  76  
  77  zEventAdapter.statistics)r)r,r<r=r>r?)
  78  rrrr@rrAr/�propertyrFr6r8r:r;rrrrr,ns
  79  
  80  
  81  
  82  
  83  r,c@sbeZdZdd�ddd�Zd d	d
  84  �Zd!dd�Zd dd�Zd dd�Zd dd�Zd"dd�Z	d#dd�Z
  85  dS)$�LockF��fast_acquirerOr"r)cCs.zt�j|d�WStyt|d�YSw�NrN)r
  86  �create_lockr�LockAdapter�r.rOrrrr/�s
  87  �zLock.__new__r0c���|��IdHdSr*��acquirer4rrr�
  88  __aenter__���zLock.__aenter__�exc_type�type[BaseException] | None�exc_val�BaseException | None�exc_tb�TracebackType | Nonec���|��dSr*��release�r5rYr[r]rrr�	__aexit__���zLock.__aexit__c�r9)�Acquire the lock.r2r4rrrrV���zLock.acquirecCr1)�w
  89          Acquire the lock, without blocking.
  90  
  91          :raises ~anyio.WouldBlock: if the operation would block
  92  
  93          r2r4rrr�acquire_nowait��zLock.acquire_nowaitcCr1)�Release the lock.r2r4rrrra�r7zLock.releasecCr1�z*Return True if the lock is currently held.r2r4rrrr#�r7zLock.lockedr!cCr1)zh
  94          Return statistics about the current state of this lock.
  95  
  96          .. versionadded:: 3.0
  97          r2r4rrrr;��zLock.statisticsN)rOr"r)rMr=�rYrZr[r\r]r^r)r0r>�r)r!)rrrr/rWrcrVrhrar#r;rrrrrM�s
  98  
  99  
 100  
 101  
 102  	
 103  rMc@s�eZdZUdZded<dd�d&d	d
 104  �Zdd�d'dd�Zed(dd��Zd)dd�Z	d*dd�Z
 105  d)dd�Zd)dd�Zd)dd �Z
 106  d+d!d"�Zd,d$d%�ZdS)-rRN�Lock | None�_internal_lockFrNrOr"r)cCrBr*rCrSrrrr/�rEzLockAdapter.__new__cC�
 107  ||_dSr*)�
 108  _fast_acquire)r5rOrrr�__init__�rEzLockAdapter.__init__rMcCs"|jdurt�j|jd�|_|jSrP)rpr
 109  rQrrr4rrr�_lock�s
 110  
 111  �zLockAdapter._lockr0c�rHr*�rtrVr4rrrrW�rIzLockAdapter.__aenter__rYrZr[r\r]r^c�s�|jdur
 112  |j��dSdSr*)rprarbrrrrc�s�
 113  �zLockAdapter.__aexit__c�s�|j��IdHdS)reNrur4rrrrV�s�zLockAdapter.acquirecC�|j��dS)rgN)rtrhr4rrrrh�szLockAdapter.acquire_nowaitcCrv)rjN�rtrar4rrrra��zLockAdapter.releasecC�
 114  |j��Srk�rtr#r4rrrr#��
 115  zLockAdapter.lockedr!cCs |jdurtddd�S|j��S)zi
 116          Return statistics about the current state of this lock.
 117  
 118          .. versionadded:: 3.0
 119  
 120          NFr)rpr!r;r4rrrr;�s
 121  
 122  zLockAdapter.statistics)rOr"r)rR)rOr")r)rMr=rmr>rn)rrrrprr/rsrLrtrWrcrVrhrar#r;rrrrrR�s
 123  
 124  
 125  
 126  	
 127  
 128  	
 129  rRc@s�eZdZUdZded<d+d,dd�Zd-d
 130  d�Zd.dd�Zd-dd�Zd-dd�Z	d-dd�Z
 131  d-dd�Zd/dd�Zd0d1d"d#�Z
 132  d-d$d%�Zd-d&d'�Zd2d)d*�ZdS)3�	ConditionNr$�_owner_task�lockrocCs|pt�|_t�|_dSr*)rMrtr�_waiters)r5r~rrrrsszCondition.__init__r)r0c�rTr*rUr4rrrrWrXzCondition.__aenter__rYrZr[r\r]r^c�r_r*r`rbrrrrcrdzCondition.__aexit__cCs|jt�kr
 133  td��dS)Nz3The current task is not holding the underlying lock)r}r�RuntimeErrorr4rrr�_check_acquireds�zCondition._check_acquiredc�s�|j��IdHt�|_dS)zAcquire the underlying lock.N)rtrVrr}r4rrrrVs�zCondition.acquirecCs|j��t�|_dS)��
 134          Acquire the underlying lock, without blocking.
 135  
 136          :raises ~anyio.WouldBlock: if the operation would block
 137  
 138          N)rtrhrr}r4rrrrhs
 139  zCondition.acquire_nowaitcCrv)zRelease the underlying lock.Nrwr4rrrra)rxzCondition.releaser"cCry)zReturn True if the lock is set.rzr4rrrr#-r{zCondition.lockedr	�nrc	CsH|��t|�D]}z|j��}WntyYdSw|��qdS)zNotify exactly n listeners.N)r��ranger�popleft�
 140  IndexErrorr6)r5r��_�eventrrr�notify1s�
 141  �zCondition.notifycCs*|��|jD]}|��q|j��dS)zNotify all the listeners.N)r�rr6�clear�r5r�rrr�
 142  notify_all<s
 143  
 144  zCondition.notify_allc�s��t�IdHt�}|j�|�|��z<z	|��IdHWnty0|��s/|j�|��wWt	dd��|�
 145  �IdHWd�dS1sJwYdSt	dd��|�
 146  �IdHWd�w1shwYw)zWait for a notification.NT)�shield)rr(r�appendrar:�
 147  BaseExceptionr8�removerrVr�rrrr:Ds$���.��zCondition.waitr&cCstt|j�|j���S)zm
 148          Return statistics about the current state of this condition.
 149  
 150          .. versionadded:: 3.0
 151          )r&�lenrrtr;r4rrrr;UszCondition.statisticsr*)r~ror=rmr>)r	)r�rr)r0)r)r&)rrrr}rrsrWrcr�rVrhrar#r�r�r:r;rrrrr|s
 152  
 153  
 154  
 155  
 156  
 157  
 158  
 159  
 160  
 161  
 162  r|c@s�eZdZddd�d'dd�Zddd�d(d
 163  d�Zd)dd�Zd*dd�Zd+dd�Zd+dd�Zd+dd�Z	e
 164  d,d d!��Ze
 165  d-d"d#��Zd.d%d&�Z
 166  dS)/�	SemaphoreNF��	max_valuerO�
 167  initial_valuerr��
 168  int | NonerOr"r)cCs4z
 169  t�j|||d�WStyt||d�YSw)Nr��r�)r
 170  �create_semaphorer�SemaphoreAdapter�r.r�r�rOrrrr/_s��zSemaphore.__new__cCsVt|t�s	td��|dkrtd��|dur&t|t�std��||kr&td��||_dS)Nz initial_value must be an integerrzinitial_value must be >= 0z$max_value must be an integer or Nonez7max_value must be equal to or higher than initial_value)�
 171  isinstancer�	TypeError�
 172  ValueErrorrr�r5r�r�rOrrrrsms
 173  
 174  �
 175  zSemaphore.__init__c�s�|��IdH|Sr*rUr4rrrrW�s�zSemaphore.__aenter__rYrZr[r\r]r^r0c�r_r*r`rbrrrrc�rdzSemaphore.__aexit__c�r9)z5Decrement the semaphore value, blocking if necessary.r2r4rrrrV�rfzSemaphore.acquirecCr1)r�r2r4rrrrh�rizSemaphore.acquire_nowaitcCr1)zIncrement the semaphore value.r2r4rrrra�r7zSemaphore.releasecCr1)z#The current value of the semaphore.r2r4rrr�value��zSemaphore.valuecCr1)z#The maximum value of the semaphore.r2r4rrrr��r�zSemaphore.max_valuer'cCr1)zm
 176          Return statistics about the current state of this semaphore.
 177  
 178          .. versionadded:: 3.0
 179          r2r4rrrr;�rlzSemaphore.statistics)r�rr�r�rOr"r)r�)r�rr�r�rOr"�r)r�rmr=�r)r�r)r��r)r')rrrr/rsrWrcrVrhrarLr�r�r;rrrrr�^s"��
 180  
 181  
 182  
 183  
 184  	r�cs�eZdZUdZded<ddd�d"d
 185  d�Zddd�d#�fdd�Zed$dd��Zd%dd�Z	d%dd�Z
 186  d%dd�Zed&dd��Zed'dd��Z
 187  d(d d!�Z�ZS))r�NzSemaphore | None�_internal_semaphoreFr�r�rr�r�rOr"r)cCrBr*rCr�rrrr/�s
 188  zSemaphoreAdapter.__new__r0cs"t�j|||d�||_||_dS)Nr�)�superrs�_initial_value�
 189  _max_valuer���	__class__rrrs�s
 190  zSemaphoreAdapter.__init__r�cCs&|jdurt�j|j|jd�|_|jS)Nr�)r�r
 191  r�r�r�r4rrr�
 192  _semaphore�s
 193  
 194  �zSemaphoreAdapter._semaphorec�rHr*)r�rVr4rrrrV�rIzSemaphoreAdapter.acquirecC�|j��dSr*)r�rhr4rrrrh��zSemaphoreAdapter.acquire_nowaitcCr�r*)r�rar4rrrra�r�zSemaphoreAdapter.releasecCs|jdur|jS|jjSr*)r�r�r�r�r4rrrr���
 195  zSemaphoreAdapter.valuecCs|jSr*)r�r4rrrr��szSemaphoreAdapter.max_valuer'cCs|jdur
 196  tdd�S|j��SrJ)r�r'r�r;r4rrrr;�rKzSemaphoreAdapter.statistics)r�rr�r�rOr"r)r�)r�rr�r�rOr"r)r0r�r=r�r�r�)rrrr�rr/rsrLr�rVrhrar�r�r;�
 197  __classcell__rrr�rr��s$
 198  �
 199  �
 200  
 201  
 202  r�c@s�eZdZd-dd�Zd.dd�Zd/dd�Zed0dd��Zejd1dd��Zed2dd��Z	ed0dd��Z
 203  d.dd�Zd3dd �Zd.d!d"�Z
 204  d3d#d$�Zd.d%d&�Zd3d'd(�Zd4d*d+�Zd,S)5�CapacityLimiterrrr)cCs*zt��|�WStyt|�YSwr*)r
 205  �create_capacity_limiterr�CapacityLimiterAdapter�r.rrrrr/�s
 206  �zCapacityLimiter.__new__r0c�r9r*r2r4rrrrW�s�zCapacityLimiter.__aenter__rYrZr[r\r]r^�bool | Nonec�r9r*r2rbrrrrc���zCapacityLimiter.__aexit__cCr1)aM
 207          The total number of tokens available for borrowing.
 208  
 209          This is a read-write property. If the total number of tokens is increased, the
 210          proportionate number of tasks waiting on this limiter will be granted their
 211          tokens.
 212  
 213          .. versionchanged:: 3.0
 214              The property is now writable.
 215  
 216          r2r4rrrr�s
 217  zCapacityLimiter.total_tokensr�cCr1r*r2�r5r�rrrrr7rcCr1)z7The number of tokens that have currently been borrowed.r2r4rrrrr�zCapacityLimiter.borrowed_tokenscCr1)z7The number of tokens currently available to be borrowedr2r4rrr�available_tokensr�z CapacityLimiter.available_tokenscCr1)z�
 218          Acquire a token for the current task without waiting for one to become
 219          available.
 220  
 221          :raises ~anyio.WouldBlock: if there are no tokens available for borrowing
 222  
 223          r2r4rrrrh�zCapacityLimiter.acquire_nowait�borrowerrDcCr1)z�
 224          Acquire a token without waiting for one to become available.
 225  
 226          :param borrower: the entity borrowing a token
 227          :raises ~anyio.WouldBlock: if there are no tokens available for borrowing
 228  
 229          r2�r5r�rrr�acquire_on_behalf_of_nowait&r�z+CapacityLimiter.acquire_on_behalf_of_nowaitc�r9)zr
 230          Acquire a token for the current task, waiting if necessary for one to become
 231          available.
 232  
 233          r2r4rrrrV0r�zCapacityLimiter.acquirec�r9)z�
 234          Acquire a token, waiting if necessary for one to become available.
 235  
 236          :param borrower: the entity borrowing a token
 237  
 238          r2r�rrr�acquire_on_behalf_of8s�z$CapacityLimiter.acquire_on_behalf_ofcCr1)z�
 239          Release the token held by the current task.
 240  
 241          :raises RuntimeError: if the current task has not borrowed a token from this
 242              limiter.
 243  
 244          r2r4rrrraAr�zCapacityLimiter.releasecCr1)z�
 245          Release the token held by the given borrower.
 246  
 247          :raises RuntimeError: if the borrower has not borrowed a token from this
 248              limiter.
 249  
 250          r2r�rrr�release_on_behalf_ofKr�z$CapacityLimiter.release_on_behalf_ofrcCr1)zl
 251          Return statistics about the current state of this limiter.
 252  
 253          .. versionadded:: 3.0
 254  
 255          r2r4rrrr;UrizCapacityLimiter.statisticsN)rrr)r�r=�rYrZr[r\r]r^r)r��r)r�r�rr)r0r��r�rDr)r0�r)r)rrrr/rWrcrLr�setterrr�rhr�rVr�rar�r;rrrrr��s&
 256  
 257  
 258  
 259  
 260  
 261  
 262  
 263  
 264  
 265  	
 266  
 267  
 268  r�c@s�eZdZUdZded<d4dd�Zd5d
 269  d�Zed6d
 270  d��Zd7dd�Z	d8dd�Z
 271  ed9dd��Zejd:dd��Zed;dd ��Z
 272  ed9d!d"��Zd7d#d$�Zd<d'd(�Zd7d)d*�Zd<d+d,�Zd7d-d.�Zd<d/d0�Zd=d2d3�ZdS)>r�NzCapacityLimiter | None�_internal_limiterrrr)cCrBr*rCr�rrrr/brEzCapacityLimiterAdapter.__new__r0cCrqr*)r)r5rrrrrserEzCapacityLimiterAdapter.__init__r�cCs |jdur
 273  t��|j�|_|jSr*)r�r
 274  r��
 275  _total_tokensr4rrr�_limiterhs
 276  
 277  �zCapacityLimiterAdapter._limiterc�rHr*)r�rWr4rrrrWqrIz!CapacityLimiterAdapter.__aenter__rYrZr[r\r]r^r�c�s�|j�|||�IdHSr*)r�rcrbrrrrcts�z CapacityLimiterAdapter.__aexit__cC�|jdur|jS|jjSr*)r�r�rr4rrrr|r�z#CapacityLimiterAdapter.total_tokensr�cCsLt|t�s|tjurtd��|dkrtd��|jdur ||_dS||j_	dS)Nz'total_tokens must be an int or math.infr	ztotal_tokens must be >= 1)
 278  r�r�math�infr�r�r�r�r�rr�rrrr�s
 279  rcCs|jdurdS|jjS)Nr)r�rr4rrrr�s
 280  z&CapacityLimiterAdapter.borrowed_tokenscCr�r*)r�r�r�r4rrrr��r�z'CapacityLimiterAdapter.available_tokenscCr�r*)r�rhr4rrrrh�r�z%CapacityLimiterAdapter.acquire_nowaitr�rDcC�|j�|�dSr*)r�r�r�rrrr���z2CapacityLimiterAdapter.acquire_on_behalf_of_nowaitc�rHr*)r�rVr4rrrrV�rIzCapacityLimiterAdapter.acquirec�s�|j�|�IdHdSr*)r�r�r�rrrr��s�z+CapacityLimiterAdapter.acquire_on_behalf_ofcCr�r*)r�rar4rrrra�r�zCapacityLimiterAdapter.releasecCr�r*)r�r�r�rrrr��r�z+CapacityLimiterAdapter.release_on_behalf_ofrcCs&|jdurtd|jddd�S|j��S)Nrr)rrr r)r�rrr;r4rrrr;�s
 281  �
 282  z!CapacityLimiterAdapter.statistics)rrr)r�)rrr)r0)r)r�r=r�r�r�r�r�r�)rrrr�rr/rsrLr�rWrcrr�rr�rhr�rVr�rar�r;rrrrr�_s.
 283  
 284  
 285  
 286  
 287  
 288  
 289  
 290  
 291  
 292  
 293  r�c@s4eZdZdZdZdddd�Zdd
 294  d�Zddd�ZdS)�
 295  ResourceGuarda�
 296      A context manager for ensuring that a resource is only used by a single task at a
 297      time.
 298  
 299      Entering this context manager while the previous has not exited it yet will trigger
 300      :exc:`BusyResourceError`.
 301  
 302      :param action: the action to guard against (visible in the :exc:`BusyResourceError`
 303          when triggered, e.g. "Another task is already {action} this resource")
 304  
 305      .. versionadded:: 4.1
 306      ��action�_guarded�usingr��strcCs||_d|_dS�NFr�)r5r�rrrrs�s
 307  zResourceGuard.__init__r)r0cCs|jrt|j��d|_dSrG)r�rr�r4rrr�	__enter__�s
 308  
 309  zResourceGuard.__enter__rYrZr[r\r]r^cCs
 310  d|_dSr�)r�rbrrr�__exit__�s
 311  zResourceGuard.__exit__N)r�)r�r�r=rm)rrrr�	__slots__rsr�r�rrrrr��s
 312  
 313  r�)%�
 314  __future__rr��collectionsr�dataclassesr�typesrZsniffiorZlowlevelrZ
 315  _eventloopr
 316  �_exceptionsr�_tasksrZ_testingr
 317  rrrr!r&r'r(r,rMrRr|r�r�r�r�r�rrrr�<module>s>
 318  	&0@ZT;r]