o
    niB                     @  s  d dl m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 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 G dd deZG dd deZG dd deZ d!dd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)service_token)	ExitHooks)Mailbox)
SockClientSockClientTimeoutError)servicec                   @     e Zd ZdZdS )WandbServiceNotOwnedErrorzARaised when the current process does not own the service process.N__name__
__module____qualname____doc__ r   r   ^/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/wandb/sdk/lib/service_connection.pyr          r   c                   @  r   )WandbServiceConnectionErrorz4Raised on failure to connect to the service process.Nr   r   r   r   r   r      r   r   c                   @  r   )WandbAttachFailedErrorz#Raised if attaching to a run fails.Nr   r   r   r   r   r      r   r   settingswandb_settings.SettingsreturnServiceConnectionc                 C  s   t  }|r|S t| S )z>Connects to the service process, starting one up if necessary.) _try_connect_to_existing_service_start_and_connect_service)r   connr   r   r   connect_to_service    s   r#   ServiceConnection | Nonec               
   C  sd   t  } | sdS | jdksJ t }z|| j W n ty+ } ztd|d}~ww t|ddS )z2Attemps to connect to an existing service process.N	localhostz&Failed to connect to internal service.)clientproc)	r
   get_service_tokenhostr   connectport	Exceptionr   r   )tokenr&   er   r   r   r    +   s    r    c                   s   t | }|  |j}|sJ t }|| tjt	 dd|d t
    fddt||fddd t  S )	zStarts 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.
    tcpr%   )
parent_pid	transportr)   r+   c                     s     j d S N)teardown	exit_coder   )r"   hooksr   r   teardown_atexit\   s   z3_start_and_connect_service.<locals>.teardown_atexitc                     s
   t  S r2   )atexit
unregisterr   )r6   r   r   <lambda>b   s   
 z,_start_and_connect_service.<locals>.<lambda>)r&   r'   cleanup)r   _Servicestart	sock_portr   r*   r
   set_service_tokenosgetpidr   hookr   r7   register)r   r'   r+   r&   r   )r"   r5   r6   r   r!   A   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._Service | Noner:   Callable[[], None] | Nonec                 C  s   || _ || _d| _|| _dS )a  Returns a new ServiceConnection.

        Args:
            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_cleanup)selfr&   r'   r:   r   r   r   __init__m   s   
zServiceConnection.__init__mailboxr   r   r   c                 C  s   t | j|S )z8Returns an interface for communicating with the service.)r	   rE   )rI   rK   r   r   r   make_interface   s   z ServiceConnection.make_interfacerecord	pb.RecordNonec                 C  s   | j | dS )zSends data to the service.N)rE   send_record_publish)rI   rM   r   r   r   send_record   s   zServiceConnection.send_recordr   wandb_settings_pb2.Settingsrun_idstrc                 C  .   t  }|j| ||j_| jj|d dS )z%Sends an init request to the service.)inform_initN)spbServerInformInitRequestr   CopyFrom_info	stream_idrE   sendrI   r   rS   requestr   r   r   rV         zServiceConnection.inform_initc                 C  s"   t  }||j_| jj|d dS )z'Sends an finish request to the service.)inform_finishN)rW   ServerInformFinishRequestrZ   r[   rE   r\   )rI   rS   r^   r   r   r   r`      s   zServiceConnection.inform_finish	attach_idc                 C  s@   t  }||j_z| jj|d}|jjW S  ty   t	dw )zwSends an attach request to the service.

        Raises a WandbAttachFailedError if attaching is not possible.
        )inform_attachzCould not attach because the run does not belong to the current service process, or because the service process is busy (unlikely).)
rW   ServerInformAttachRequestrZ   r[   rE   send_and_recvinform_attach_responser   r   r   )rI   rb   r^   responser   r   r   rc      s   
zServiceConnection.inform_attachc                 C  rU   )z%Sends a start request to the service.)inform_startN)rW   ServerInformStartRequestr   rY   rZ   r[   rE   r\   r]   r   r   r   rh      r_   zServiceConnection.inform_startr4   intc                 C  sT   | j std| jrJ d| _| jr|   t  | jjtj	|dd | j 
 S )a/  Shuts down the service process and returns its exit code.

        This may only be called once.

        Returns:
            The exit code of the service process.

        Raises:
            WandbServiceNotOwnedError: If the current process did not start
                the service process.
        z5Cannot tear down service started by different processT)r4   )inform_teardown)rF   r   rG   rH   r
   clear_service_tokenrE   r\   rW   ServerInformTeardownRequestjoin)rI   r4   r   r   r   r3      s   

zServiceConnection.teardownr2   )r&   r   r'   rC   r:   rD   )rK   r   r   r   )rM   rN   r   rO   )r   rR   rS   rT   r   rO   )rS   rT   r   rO   )rb   rT   r   rR   )r4   rj   r   rj   )r   r   r   r   rJ   rL   rQ   rV   r`   rc   rh   r3   r   r   r   r   r   j   s    





)r   r   r   r   )r   r$   )%
__future__r   r7   r?   typingr   wandb.protor   pbr   rW   r   	wandb.sdkr   wandb.sdk.interface.interfacer   "wandb.sdk.interface.interface_sockr	   wandb.sdk.libr
   wandb.sdk.lib.exit_hooksr   wandb.sdk.lib.mailboxr   wandb.sdk.lib.sock_clientr   r   wandb.sdk.servicer   r,   r   r   r   r#   r    r!   r   r   r   r   r   <module>   s,    


)