o
    xi                     @  s   d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
mZ d dl
mZ d dlmZ dd	lmZ dd
lmZ eeZG dd deZG dd dZdS )    )annotationsN)	Awaitable)Callable)wandb_internal_pb2)wandb_server_pb2)asyncio_manager   )MailboxHandle)MailboxResponseHandlec                   @  s   e Zd ZdZdS )MailboxClosedErrorz/The mailbox has been closed and cannot be used.N)__name__
__module____qualname____doc__ r   r   M/home/ubuntu/.local/lib/python3.10/site-packages/wandb/sdk/mailbox/mailbox.pyr      s    r   c                   @  sB   e Zd ZdZddd	ZdddZdddZdddZdddZdS )Mailboxa9  Matches service responses to requests.

    The mailbox can set an address on a server request and create a handle for
    waiting for a response to that record. Responses are delivered by calling
    `deliver()`. The `close()` method abandons all handles in case the
    service process becomes unreachable.
    asyncerasyncio_manager.AsyncioManagercancel Callable[[str], Awaitable[None]]returnNonec                 C  s&   || _ || _i | _t | _d| _dS )zCreate a mailbox.

        Args:
            asyncer: Asyncio runner for scheduling async operations.
            cancel: A callback that can be used to cancel a request by ID.
        FN)_asyncer_cancel_handles	threadingLock_handles_lock_closed)selfr   r   r   r   r   __init__!   s
   

zMailbox.__init__requestspb.ServerRequest | pb.Record!MailboxHandle[spb.ServerResponse]c                 C  s   t |tjr6|j }s|jjj }rtd| d|  }||_|	dr+||jj_|	dr5||j
j_n|jj }rDtd| d|  }||j_| j | jrVt t|| j| jd}|| j|< W d   |S 1 sow   Y  |S )a  Set a response address on a request.

        Args:
            request: The request on which to set a request ID or mailbox slot.
                This is mutated. An address must not already be set.

        Returns:
            A handle for waiting for the response to the request.

        Raises:
            MailboxClosedError: If the mailbox has been closed, in which case
                no new responses are expected to be delivered and new handles
                cannot be created.
        z Request already has an address ()record_publishrecord_communicate)r   r   N)
isinstancespbServerRequest
request_idr&   controlmailbox_slot
ValueError_new_addressHasFieldr'   r   r   r   r
   r   r   r   )r    r"   addresshandler   r   r   require_response3   s:   





zMailbox.require_responsestrc                 C  s,   dd }| }|| j v r| }|| j v s|S )z[Returns an unused address for a request.

        Assumes `_handles_lock` is held.
        c                   S  s   d dd tdD S )N c                 s  s"    | ]}t tjtj V  qd S )N)secretschoicestringascii_lowercasedigits).0_r   r   r   	<genexpr>l   s
    
z9Mailbox._new_address.<locals>.generate.<locals>.<genexpr>   )joinranger   r   r   r   generatek   s   
z&Mailbox._new_address.<locals>.generate)r   )r    rA   r1   r   r   r   r/   e   s   

zMailbox._new_addressresponsespb.ServerResponsec                   s   |j }|s$|d}|dkr|jd}d| }td|  dS | j | j|d}W d   n1 s9w   Y  |rJ||I dH  dS dS )zDeliver a response from the service.

        If the response address is invalid, this does nothing.
        It is a no-op if the mailbox has been closed.
        server_response_typeresult_communicateresult_typezresult_communicate.z(Received response with no mailbox slot: N)	r+   
WhichOneofrE   _loggererrorr   r   popdeliver)r    rB   r1   kindrF   r2   r   r   r   rK   y   s   

zMailbox.deliverc                 C  sl   | j ) d| _tdt| j d | j D ]}|  q| j  W d   dS 1 s/w   Y  dS )zXIndicate no further responses will be delivered.

        Abandons all handles.
        TzClosing mailbox, abandoning z	 handles.N)	r   r   rH   infolenr   valuesabandonclear)r    r2   r   r   r   close   s   
"zMailbox.closeN)r   r   r   r   r   r   )r"   r#   r   r$   )r   r4   )rB   rC   r   r   )r   r   )	r   r   r   r   r!   r3   r/   rK   rR   r   r   r   r   r      s    


2
r   )
__future__r   loggingr6   r8   r   collections.abcr   typingr   wandb.protor   pbr   r)   wandb.sdk.libr   mailbox_handler	   response_handler
   	getLoggerr   rH   	Exceptionr   r   r   r   r   r   <module>   s    
