o
    -wiR                     @  s   d dl m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mZ er4d dlmZ G d	d
 d
eej ZG dd dZdS )    )annotationsN)TYPE_CHECKING)override)wandb_server_pb2   )HandleAbandonedErrorMailboxHandle)	interfacec                   @  s   e Zd ZdZd&ddZd'd
dZed(ddZed)ddZd)ddZ	ed*ddZ
ed+ddZed+ddZd,d!d"Zd-d#d$Zd%S ).MailboxResponseHandlez(A general handle for any ServerResponse.addressstrreturnNonec                 C  s2   || _ t | _t | _d| _d | _t | _	d S )NF)
_address	threadingLock_lockEvent_event
_abandoned	_responsedict_asyncio_events)selfr    r   ^/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/wandb/sdk/mailbox/response_handle.py__init__   s   

zMailboxResponseHandle.__init__responsespb.ServerResponsec                 C  sj   | j ( | jr	 W d   dS | jrtd| j d|| _|   W d   dS 1 s.w   Y  dS )zDeliver the response.

        This may only be called once. It is an error to respond to the same
        request more than once. It is a no-op if the handle has been abandoned.
        Nz)A response has already been delivered to .)r   r   r   
ValueErrorr   _signal_done)r   r   r   r   r   deliver    s   
"zMailboxResponseHandle.deliverifaceinterface.InterfaceBasec                 C  s   | | j |   d S N)publish_cancelr   abandon)r   r#   r   r   r   cancel2   s   zMailboxResponseHandle.cancelc                 C  s:   | j  d| _|   W d    d S 1 sw   Y  d S )NT)r   r   r!   r   r   r   r   r'   7   s   
"zMailboxResponseHandle.abandonc                 C  s0   | j   | j D ]}|  q
| j  dS )zlIndicate that the handle either got a response or became abandoned.

        The lock must be held.
        N)r   setr   valuesset_threadsafeclear)r   asyncio_eventr   r   r   r!   =   s   

z"MailboxResponseHandle._signal_donespb.ServerResponse | Nonec                 C  s0   | j  | jW  d    S 1 sw   Y  d S r%   )r   r   r)   r   r   r   checkJ   s   $zMailboxResponseHandle.checktimeoutfloat | Nonec                C  s~   |d urt |std| jj|dstd| j | j | jr-| jW  d    S | j	s2J t
 1 s8w   Y  d S NzTimeout must be finite or None.)r1   z"Timed out waiting for response on )mathisfiniter    r   waitTimeoutErrorr   r   r   r   r   )r   r1   r   r   r   wait_orO   s   

zMailboxResponseHandle.wait_orc                  s`  |d urt |stdt }| t | zztj| |dI d H  W nI tj	t	fyr } z:| j
) | jrQ| jW  d    W  Y d }~W | | S | jrWt t	d| j |1 scw   Y  W Y d }~n4d }~ww | j
 | jr| jW  d    W | | S | jsJ t 1 sw   Y  W | | d S W | | d S | | w r3   )r4   r5   r    asyncior   _add_asyncio_eventget_event_loopwait_forr6   r7   r   r   _forget_asyncio_eventr   r   r   )r   r1   evter   r   r   
wait_async`   sD   

z MailboxResponseHandle.wait_asyncloopasyncio.AbstractEventLoopeventasyncio.Eventc                 C  sf   t ||}| j! | js| jr|  n|| j|< W d   dS W d   dS 1 s,w   Y  dS )zAdd an event to signal when a response is received.

        If a response already exists, this notifies the event loop immediately.
        N)_AsyncioEventr   r   r   r,   r   )r   rA   rC   r.   r   r   r   r:      s   
	
"z(MailboxResponseHandle._add_asyncio_eventc                 C  s:   | j  | j|d W d   dS 1 sw   Y  dS )z7Cancel signalling an event when a response is received.N)r   r   pop)r   rC   r   r   r   r=      s   "z+MailboxResponseHandle._forget_asyncio_eventN)r   r   r   r   )r   r   r   r   )r#   r$   r   r   r   r   )r   r/   )r1   r2   r   r   )rA   rB   rC   rD   r   r   )rC   rD   r   r   )__name__
__module____qualname____doc__r   r"   r   r(   r'   r!   r0   r8   r@   r:   r=   r   r   r   r   r
      s"    




 r
   c                   @  s    e Zd ZdddZdd	d
ZdS )rE   rA   rB   rC   rD   c                 C  s   || _ || _d S r%   )_loopr   )r   rA   rC   r   r   r   r      s   
z_AsyncioEvent.__init__r   r   c                 C  s   | j | jj dS )z&Set the asyncio event in its own loop.N)rL   call_soon_threadsafer   r*   r)   r   r   r   r,      s   z_AsyncioEvent.set_threadsafeN)rA   rB   rC   rD   rG   )rH   rI   rJ   r   r,   r   r   r   r   rE      s    
rE   )
__future__r   r9   r4   r   typingr   typing_extensionsr   wandb.protor   spbmailbox_handler   r   wandb.sdk.interfacer	   ServerResponser
   rE   r   r   r   r   <module>   s     