Format: application/x-python-code
⬇ Download File (9.8 KB)
   1  o
   2  
   3  ;'h@)�@sTddlmZddlZddlmZmZddlmZmZddl	m
   4  Z
   5  ddlmZm
   6  Z
   7  mZddlmZmZmZmZdd	lmZmZdd
   8  lmZmZmZddlmZed�Zed
   9  dd�Zeddd�ZGdd�de
  10  �Z edd�Gdd�dee��Z!edd�Gdd�dee��Z"edd�Gdd�deeee��Z#edd�Gdd�deeee��Z$dS)�)�annotationsN)�OrderedDict�deque)�	dataclass�field)�
  11  TracebackType)�Generic�
  12  NamedTuple�TypeVar�)�BrokenResourceError�ClosedResourceError�EndOfStream�
  13  WouldBlock)�TaskInfo�get_current_task)�Event�ObjectReceiveStream�ObjectSendStream)�
  14  checkpoint�T_Item�T_coT)�	covariant�T_contra)�
  15  contravariantc@s>eZdZUded<ded<ded<ded<ded<ded<d	S)
  16  �MemoryObjectStreamStatistics�intZcurrent_buffer_used�float�max_buffer_sizeZopen_send_streamsZopen_receive_streamsZtasks_waiting_sendZtasks_waiting_receiveN)�__name__�
  17  __module__�__qualname__�__annotations__�r#r#�JC:\Users\shubh\Videos\agent\nova\Lib\site-packages\anyio/streams/memory.pyrs
  18  rF)�eqc@s>eZdZUeded�Zded<edd�Zded<d
  19  d
  20  d�ZdS)�MemoryObjectItemReceiverF��init�default_factoryr�	task_info)r(r�item�return�strcCs(t|dd�}|jj�d|j�d|�d�S)Nr+z(task_info=z, item=�))�getattr�	__class__rr*)�selfr+r#r#r$�__repr__)sz!MemoryObjectItemReceiver.__repr__N)r,r-)	rr r!rrr*r"r+r2r#r#r#r$r&$s
  21  r&c@s�eZdZUe�Zded<eded�Zded<eddd�Zd	ed
  22  <eddd�Z	d	ed<ede
  23  d�Zded
  24  <ede
  25  d�Zded<ddd�Z
  26  dS)�MemoryObjectStreamStaterrFr'z
  27  deque[T_Item]�bufferr�r(�defaultr�open_send_channels�open_receive_channelsz4OrderedDict[Event, MemoryObjectItemReceiver[T_Item]]�waiting_receiverszOrderedDict[Event, T_Item]�waiting_sendersr,rcCs*tt|j�|j|j|jt|j�t|j��S�N)r�lenr4rr7r8r:r9�r1r#r#r$�
  28  statistics=s�z"MemoryObjectStreamState.statisticsN�r,r)rr r!rrr"rr4r7r8rr9r:r>r#r#r#r$r30s
  29  ��r3c@s�eZdZUded<eddd�Zded<d'd	d
  30  �Zd(dd
  31  �Zd(dd�Zd)dd�Z	d'dd�Z
  32  d'dd�Zd*dd�Zd)dd�Z
  33  d+d"d#�Zd'd$d%�Zd&S),�MemoryObjectReceiveStreamzMemoryObjectStreamState[T_co]�_stateFr5�bool�_closedr,�NonecC�|jjd7_dS�N�)rAr8r=r#r#r$�
  34  __post_init__M�z'MemoryObjectReceiveStream.__post_init__rcCs`|jrt�|jjr|jjjdd�\}}|jj�|�|��|jjr(|jj��S|jj	s.t
  35  �t�)a�
  36          Receive the next item if it can be done without waiting.
  37  
  38          :return: the received item
  39          :raises ~anyio.ClosedResourceError: if this send stream has been closed
  40          :raises ~anyio.EndOfStream: if the buffer is empty and this stream has been
  41              closed from the sending end
  42          :raises ~anyio.WouldBlock: if there are no items in the buffer and no tasks
  43              waiting to send
  44  
  45          F��last)rCr
  46  rAr:�popitemr4�append�set�popleftr7rr)r1�
  47  send_eventr+r#r#r$�receive_nowaitPsz(MemoryObjectReceiveStream.receive_nowaitc
  48  �s��t�IdHz|��WStyMt�}tt�}||jj|<z|��IdHW|jj�	|d�n	|jj�	|d�wz|j
  49  WYStyLtd�wwr;)
  50  rrQrrr&rrAr9�wait�popr+�AttributeErrorr)r1�
  51  receive_event�receiverr#r#r$�receivels"�
  52  
  53  $��z!MemoryObjectReceiveStream.receive�MemoryObjectReceiveStream[T_co]cC�|jrt�t|jd�S)a
  54          Create a clone of this receive stream.
  55  
  56          Each clone can be closed separately. Only when all clones have been closed will
  57          the receiving end of the memory stream be considered closed by the sending ends.
  58  
  59          :return: the cloned stream
  60  
  61          �rA)rCr
  62  r@rAr=r#r#r$�clone��
  63  zMemoryObjectReceiveStream.clonecCsV|js%d|_|jjd8_|jjdkr't|jj���}|D]
  64  }|��qdSdSdS�z�
  65          Close the stream.
  66  
  67          This works the exact same way as :meth:`aclose`, but is provided as a special
  68          case for the benefit of synchronous callbacks.
  69  
  70          TrGrN)rCrAr8�listr:�keysrN)r1Zsend_events�eventr#r#r$�close�s
  71  �zMemoryObjectReceiveStream.closec���|��dSr;�rar=r#r#r$�aclose���z MemoryObjectReceiveStream.aclosercC�
  72  |j��S�zj
  73          Return statistics about the current state of this stream.
  74  
  75          .. versionadded:: 3.0
  76          �rAr>r=r#r#r$r>��
  77  z$MemoryObjectReceiveStream.statisticscC�|Sr;r#r=r#r#r$�	__enter__��z#MemoryObjectReceiveStream.__enter__�exc_type�type[BaseException] | None�exc_val�BaseException | None�exc_tb�TracebackType | NonecC�|��dSr;rc�r1rmrorqr#r#r$�__exit__��z"MemoryObjectReceiveStream.__exit__cC�6|jstjd|jj�dt|�d�d�t|d�dSdS�Nz
  78  Unclosed <z at �x�>)�source�rC�warnings�warnr0r�id�ResourceWarningr=r#r#r$�__del__��
  79  ��z!MemoryObjectReceiveStream.__del__N�r,rD)r,r)r,rXr?�rmrnrorprqrrr,rD)rr r!r"rrCrHrQrWr[rardr>rkrur�r#r#r#r$r@Hs
  80  
  81  
  82  
  83  
  84  
  85  
  86  
  87  
  88  
  89  r@c@s�eZdZUded<eddd�Zded<d(d	d
  90  �Zd)d
  91  d�Zd)dd�Zd*dd�Z	d(dd�Z
  92  d(dd�Zd+dd�Zd*dd�Z
  93  d,d#d$�Zd(d%d&�Zd'S)-�MemoryObjectSendStreamz!MemoryObjectStreamState[T_contra]rAFr5rBrCr,rDcCrErF)rAr7r=r#r#r$rH�rIz$MemoryObjectSendStream.__post_init__r+rcCs�|jrt�|jjst�|jjr+|jjjdd�\}}|j��s'||_	|�
  94  �dS|jjst|jj�|jj
  95  kr>|jj�|�dSt�)a�
  96          Send an item immediately if it can be done without waiting.
  97  
  98          :param item: the item to send
  99          :raises ~anyio.ClosedResourceError: if this send stream has been closed
 100          :raises ~anyio.BrokenResourceError: if the stream has been closed from the
 101              receiving end
 102          :raises ~anyio.WouldBlock: if the buffer is full and there are no tasks waiting
 103              to receive
 104  
 105          FrJN)rCr
 106  rAr8rr9rLr*Zhas_pending_cancellationr+rNr<r4rrMr)r1r+rUrVr#r#r$�send_nowait�s
 107  �z"MemoryObjectSendStream.send_nowaitc�s��t�IdHz|�|�WdStyJt�}||jj|<z	|��IdHWnty8|jj�|d��w||jjvrG|jj|=t	d�YdSw)a�
 108          Send an item to the stream.
 109  
 110          If the buffer is full, this method blocks until there is again room in the
 111          buffer or the item can be sent directly to a receiver.
 112  
 113          :param item: the item to send
 114          :raises ~anyio.ClosedResourceError: if this send stream has been closed
 115          :raises ~anyio.BrokenResourceError: if the stream has been closed from the
 116              receiving end
 117  
 118          N)
 119  rr�rrrAr:rR�
 120  BaseExceptionrSr)r1r+rPr#r#r$�send�s$�
 121  �
 122  ��zMemoryObjectSendStream.send� MemoryObjectSendStream[T_contra]cCrY)a
 123          Create a clone of this send stream.
 124  
 125          Each clone can be closed separately. Only when all clones have been closed will
 126          the sending end of the memory stream be considered closed by the receiving ends.
 127  
 128          :return: the cloned stream
 129  
 130          rZ)rCr
 131  r�rAr=r#r#r$r[r\zMemoryObjectSendStream.clonecCsb|js+d|_|jjd8_|jjdkr-t|jj���}|jj��|D]
 132  }|��q$dSdSdSr])rCrAr7r^r9r_�clearrN)r1Zreceive_eventsr`r#r#r$ras
 133  �zMemoryObjectSendStream.closec�rbr;rcr=r#r#r$rd!rezMemoryObjectSendStream.aclosercCrfrgrhr=r#r#r$r>$riz!MemoryObjectSendStream.statisticscCrjr;r#r=r#r#r$rk,rlz MemoryObjectSendStream.__enter__rmrnrorprqrrcCrsr;rcrtr#r#r$ru/rvzMemoryObjectSendStream.__exit__cCrwrxr|r=r#r#r$r�7r�zMemoryObjectSendStream.__del__Nr�)r+rr,rD)r,r�r?r�)rr r!r"rrCrHr�r�r[rardr>rkrur�r#r#r#r$r��s
 134  
 135  
 136  
 137  
 138  
 139  
 140  
 141  
 142  
 143  r�)%�
 144  __future__rr}�collectionsrr�dataclassesrr�typesr�typingrr	r
 145  �rr
 146  rrZ_core._testingrr�abcrrrZlowlevelrrrrrr&r3r@r�r#r#r#r$�<module>s,u