o
    -wi"                     @  s   d dl m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 d d
lmZ d dlmZ d dlmZmZ d dlmZmZmZ ddlmZmZ G dd deZ dddZ!dddZ"G dd dZ#dS )    )annotationsN)Callable)wandb_internal_pb2)wandb_server_pb2)wandb_settings_pb2)wandb_settings)InterfaceBase)InterfaceSock)MessageSockRouter)	ExitHooks)
SockClientSockClientClosedError)HandleAbandonedErrorMailboxMailboxClosedError   )service_processservice_tokenc                   @  s   e Zd ZdZdS )WandbAttachFailedErrorzFailed to attach to a run.N)__name__
__module____qualname____doc__ r   r   e/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/wandb/sdk/lib/service/service_connection.pyr      s    r   settingswandb_settings.SettingsreturnServiceConnectionc                 C  s$   t  }|rt| ddS t| S )z=Connect to the service process, starting one up if necessary.N)clientproc)r   from_envr   connect_start_and_connect_service)r   tokenr   r   r   connect_to_service   s   r%   c                   s^   t | }|j }|j  t    fddt||fddd t	  S )zStart a service process and returns a connection to it.

    An atexit hook is registered to tear down the service process and wait for
    it to complete. The hook does not run in processes started using the
    multiprocessing module.
    c                     s     j d S N)teardown	exit_coder   )connhooksr   r   teardown_atexit5   s   z3_start_and_connect_service.<locals>.teardown_atexitc                     s
   t  S r&   )atexit
unregisterr   )r+   r   r   <lambda>;   s   
 z,_start_and_connect_service.<locals>.<lambda>)r   r    cleanup)
r   startr$   r"   save_to_envr   hookr   r,   register)r   r    r   r   )r)   r*   r+   r   r#   $   s   
	



r#   c                   @  sd   e Zd ZdZ	d'd(d	d
Zd)ddZd*ddZd+ddZd,ddZd-ddZ	d+d d!Z
d.d%d&ZdS )/r   z1A connection to the W&B internal service process.Nr   r   r    %service_process.ServiceProcess | Noner/   Callable[[], None] | Nonec                 C  s4   || _ || _d| _|| _t | _t| j | j| _dS )a  Returns a new ServiceConnection.

        Args:
            mailbox: The mailbox to use for all communication over the socket.
            router: A handle to the thread that reads from the socket and
                updates the mailbox.
            client: A socket that's connected to the service.
            proc: The service process if we own it, or None otherwise.
            cleanup: A callback to run on teardown before doing anything.
        FN)_client_proc
_torn_down_cleanupr   _mailboxr
   _router)selfr   r    r/   r   r   r   __init__F   s   zServiceConnection.__init__	stream_idstrr   r   c                 C  s   t | j| j|dS )z8Returns an interface for communicating with the service.)r>   )r	   r6   r:   )r<   r>   r   r   r   make_interface^   s   z ServiceConnection.make_interfacerecord	pb.RecordNonec                 C  s   | j | dS )zSend data to the service.N)r6   send_record_publish)r<   rA   r   r   r   send_recordb   s   zServiceConnection.send_recordr   wandb_settings_pb2.Settingsrun_idc                 C  4   t  }|j| ||j_| jt j|d dS )z$Send an init request to the service.)inform_initN)	spbServerInformInitRequestr   CopyFrom_infor>   r6   send_server_requestServerRequestr<   r   rG   requestr   r   r   rI   f      zServiceConnection.inform_initc                 C  s(   t  }||j_| jt j|d dS )z&Send an finish request to the service.)inform_finishN)rJ   ServerInformFinishRequestrM   r>   r6   rN   rO   )r<   rG   rQ   r   r   r   rS   q   s   zServiceConnection.inform_finish	attach_idc              
   C  sv   t  }||jj_z| j|}| j| |j	dd}W |jjS  t
ttfy/   tdd ty:   tddw )zvSend an attach request to the service.

        Raises a WandbAttachFailedError if attaching is not possible.
        
   )timeoutz5Failed to attach: the service process is not running.NzFailed to attach because the run does not belong to the current service process, or because the service process is busy (unlikely).)rJ   rO   inform_attachrM   r>   r:   require_responser6   rN   wait_orr   r   r   r   TimeoutErrorinform_attach_responser   )r<   rU   rQ   handleresponser   r   r   rX   w   s(   
zServiceConnection.inform_attachc                 C  rH   )z$Send a start request to the service.)inform_startN)	rJ   ServerInformStartRequestr   rL   rM   r>   r6   rN   rO   rP   r   r   r   r_      rR   zServiceConnection.inform_startr(   int
int | Nonec                 C  sd   | j rtdd| _ | jr|   | j  | jsdS t  | j	t
jt
j|dd | j S )ax  Close the connection.

        Stop reading responses on the connection, and if this connection owns
        the service process, send a teardown message and wait for it to shut
        down.

        This may only be called once.

        Returns:
            The exit code of the service process, or None if the process was
            not owned by this connection.
        zAlready torn down.TN)r(   )inform_teardown)r8   AssertionErrorr9   r;   joinr7   r   clear_service_in_envr6   rN   rJ   rO   ServerInformTeardownRequest)r<   r(   r   r   r   r'      s"   

zServiceConnection.teardownr&   )r   r   r    r4   r/   r5   )r>   r?   r   r   )rA   rB   r   rC   )r   rF   rG   r?   r   rC   )rG   r?   r   rC   )rU   r?   r   rF   )r(   ra   r   rb   )r   r   r   r   r=   r@   rE   rI   rS   rX   r_   r'   r   r   r   r   r   C   s    





)r   r   r   r   )$
__future__r   r,   typingr   wandb.protor   pbr   rJ   r   	wandb.sdkr   wandb.sdk.interface.interfacer   "wandb.sdk.interface.interface_sockr	   wandb.sdk.interface.router_sockr
   wandb.sdk.lib.exit_hooksr   wandb.sdk.lib.sock_clientr   r   wandb.sdk.mailboxr   r   r    r   r   	Exceptionr   r%   r#   r   r   r   r   r   <module>   s$    

