
    &`i@                     F    d dl Z d dlmZmZ d dlmZ  G d de          ZdS )    N)AnyOptional)ChannelInterfacec                       e Zd ZdZ	 	 ddedee         dee         fdZddZ	dd	Z
d
 ZdefdZddedee         fdZddee         defdZddZdS )CachedChannela  
    CachedChannel wraps an inner channel and caches the data read from it until
    `num_reads` reads have completed. If inner channel is None, the data
    is written to serialization context and retrieved from there. This is useful
    when passing data within the same actor and a shared memory channel can be
    avoided.

    Args:
        num_reads: The number of reads from this channel that must happen before
            writing again. Readers must be methods of the same actor.
        inner_channel: The inner channel to cache data from. If None, the data is
            read from the serialization context.
        _channel_id: The unique ID for the channel. If None, a new ID is generated.
    N	num_readsinner_channel_channel_idc                     |dk    s
J d            || _         || _        || _        | j        't          t	          j                              | _        d S d S )Nr   z!num_reads must be greater than 0.)
_num_reads_inner_channelr
   struuiduuid4)selfr   r	   r
   s       {/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/experimental/channel/cached_channel.py__init__zCachedChannel.__init__   s^     1}}}A}}}#+ '#"4:<<00D $#    returnc                 J    | j         | j                                          d S d S N)r   ensure_registered_as_writerr   s    r   r   z)CachedChannel.ensure_registered_as_writer&   .    *;;===== +*r   c                 J    | j         | j                                          d S d S r   )r   ensure_registered_as_readerr   s    r   r   z)CachedChannel.ensure_registered_as_reader*   r   r   c                 8    t           | j        | j        | j        ffS r   )r   r   r   r
   r   s    r   
__reduce__zCachedChannel.__reduce__.   s%    O
 
 	
r   c                 8    d| j          d| j         d| j         dS )NzCachedChannel(channel_id=z, num_reads=z), inner_channel=))r
   r   r   r   s    r   __str__zCachedChannel.__str__5   s?    4(8 4 44 4!04 4 4	
r   valuetimeoutc                     |                                   ddlm} | j        | j                            ||           d S |                                j        }|                    | j        || j	                   d S Nr   ChannelContext)
r   ray.experimental.channelr'   r   writeget_currentserialization_contextset_datar
   r   )r   r"   r#   r'   ctxs        r   r)   zCachedChannel.write<   s    ((***;;;;;;*%%eW555F ((**@T%udo>>>>>r   c                    |                                   ddlm} |                                j        }|                    | j                  r|                    | j                  S | j        
J d            | j        	                    |          }|
                    | j        || j                   |                    | j                  S )Nr   r&   zGCannot read from the serialization context while inner channel is None.)r   r(   r'   r*   r+   has_datar
   get_datar   readr,   r   )r   r#   r'   r-   r"   s        r   r1   zCachedChannel.readM   s    ((***;;;;;;((**@<<()) 	2<< 0111 ++T ,++#((11T%udo>>> ||D,---r   c                     ddl m} | j        | j                                         |                                j        }|                    | j                   d S r%   )r(   r'   r   closer*   r+   
reset_datar
   )r   r'   r-   s      r   r3   zCachedChannel.closei   sa    ;;;;;;*%%'''((**@t'(((((r   )NN)r   Nr   )__name__
__module____qualname____doc__intr   r   r   r   r   r   r   r!   r   floatr)   r1   r3    r   r   r   r      s!        $ 59%)	1 11   011 c]	1 1 1 1> > > >> > > >
 
 

 
 
 
 
? ?3 ?% ? ? ? ?". .HUO .s . . . .8) ) ) ) ) )r   r   )r   typingr   r   ray.experimental.channel.commonr   r   r;   r   r   <module>r>      sw                     < < < < < <h) h) h) h) h)$ h) h) h) h) h)r   