o
    ni!                     @   s  d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZmZmZ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 ddlmZ ddlmZmZ d	d
lmZm Z  erjddl!m"Z" G dd deZ#G dd deZ$G dd deZ%G dd dZ&dS )zReliably launch and connect to backend server process (wandb service).

Backend server process can be connected to using tcp sockets transport.
    N)TYPE_CHECKINGAnyDictOptional)_sentrytermlog)
core_debugerror_reporting_enabledis_require_legacy_service)ErrorWandbCoreNotAvailableError)url_registry)get_core_path
get_module   )_startup_debug	port_file)Settingsc                   @      e Zd ZdZdS )ServiceStartProcessErrorz>Raised when a known error occurs when launching wandb service.N__name__
__module____qualname____doc__ r   r   W/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/wandb/sdk/service/service.pyr          r   c                   @   r   )ServiceStartTimeoutErrorz$Raised when service start times out.Nr   r   r   r   r   r   !   r   r   c                   @   r   )ServiceStartPortErrorz/Raised when service start fails to find a port.Nr   r   r   r   r   r   %   r   r   c                   @   s   e Zd ZU ded< ee ed< eej ed< eed< 				dd	d
Z	de
ddfddZ	dde
deej ddfddZdddZdddZedee fddZdefddZdS )_Servicer   	_settings
_sock_port_internal_proc_startup_debug_enabledsettingsreturnNc                 C   s8   || _ d | _d | _d | _t | _tjt	|dd d S )Nservice)tagsprocess_context)
r!   _stubr"   r#   r   
is_enabledr$   r   configure_scopedict)selfr%   r   r   r   __init__/   s   
z_Service.__init__messagec                 C   s   | j sd S t| d S N)r$   r   print_message)r.   r0   r   r   r   _startup_debug_print;   s   z_Service._startup_debug_printfnameprocc              
   C   s  t  | jj }t  |k rz|r?| r?t|jtjt	
d|jr%|j nd|jr.|j ndd}td|j d| |dtj|sKt d qzt }|| |js_t d W q|j| _W d
S  tyy } ztd| d	d
}~ww td| jj d)a  Wait for the service to write the port file and then read it.

        Args:
            fname: The path to the port file.
            proc: The process to wait for.

        Raises:
            ServiceStartTimeoutError: If the service takes too long to start.
            ServiceStartPortError: If the service writes an invalid port file or unable to read it.
            ServiceStartProcessError: If the service process exits unexpectedly.

        python3 )commandsys_executablewhich_pythonproc_outproc_errz&The wandb service process exited with z. Ensure that `sys.executable` is a valid python interpreter. You can override it with the `_executable` setting or with the `WANDB_X_EXECUTABLE` environment variable.
)contextg?z+Failed to allocate port for wandb service: .Nz3Timed out waiting for wandb service to start after zF seconds. Try increasing the timeout with the `_service_wait` setting.)time	monotonicr!   x_service_waitpollr-   argssys
executableshutilwhichstdoutreadstderrr   
returncodeospathisfilesleepr   PortFileis_valid	sock_portr"   	Exceptionr   r   )r.   r4   r5   time_maxr=   pfer   r   r   _wait_for_ports@   sL   





z_Service._wait_for_portsc                 C   s  |  d tdd}t dkr|jtjd n|jdd tt	 }t
 }tj|d| d}| jj}|d	g}g }t szt }W n ty^ }	 zt|	 W Y d
}	~	nd
}	~	ww ||g t sm|d tddrw|d g }tdtd ddd n|g d |d|d|g7 }tjddkrtddd}
tj  !d}d| d}t"#t$j%j%j%j%d d }|d	dd d!|g}t||d"< td#|  td$| d% ztj&|| fd&tji|}W n t'y }	 zt|	 W Y d
}	~	nd
}	~	ww |  d' z	| j(||d( W n t'y+ }	 zt|	 W Y d
}	~	nd
}	~	ww |  d) || _)W d
   n	1 s?w   Y  |  d* d
S )+zLaunch server and set ports.launchT)	close_fdsWindows)creationflags)start_new_sessionzport-z.txtz-mNz--no-observabilityFalse)default--debugz6Using wandb-core as the SDK backend.  Please refer to z
wandb-corez for more information.F)repeat)wandbr'   r_   z--port-filenamez--pidWANDB_SERVICE_PROFILEmemrayzQwandb service memory profiling requires memray, install with `pip install memray`)requiredz%Y%m%d%H%M%Szwandb_service.memray.z.bintoolszcli.pyrunz-or   z5wandb service memory profiling enabled, output file: z9Convert to flamegraph with: `python -m memray flamegraph `env
wait_ports)r5   wait_ports_donelaunch_done)*r3   r-   platformsystemupdate
subprocessCREATE_NEW_PROCESS_GROUPstrrL   getpidtempfileTemporaryDirectoryrM   joinr!   x_executabler
   r   r   r   reraiseextendr	   appendr   r   r   urlenvirongetr   datetimenowstrftimepathlibPath__file__parentPopenrS   rW   r#   )r.   kwargspidtmpdirr4   rE   exec_cmd_listservice_args	core_pathrV   _time_tagoutput_filecli_executableinternal_procr   r   r   _launch_server   s   









Vz_Service._launch_serverc                 C   s   |    d S r1   )r   r.   r   r   r   start   s   z_Service.startc                 C   s   | j S r1   )r"   r   r   r   r   rR      s   z_Service.sock_portc                 C   s   d}| j r
| j  }|S )Nr   )r#   wait)r.   retr   r   r   ru      s   
z_Service.join)r%   r   r&   Nr1   )r&   N)r   r   r   __annotations__r   intro   r   boolr/   rq   r3   rW   r   r   propertyrR   ru   r   r   r   r   r    )   s0   
 


?
hr    )'r   r}   rL   r   rl   rF   ro   rD   rs   r?   typingr   r   r   r   ra   r   r   	wandb.envr   r	   r
   wandb.errorsr   r   wandb.errors.linksr   
wandb.utilr   r   r7   r   r   wandb.sdk.wandb_settingsr   r   r   r   r    r   r   r   r   <module>   s.    