o
    Zh#                     @   s   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 g dZeeZdZdZd	Z	
					ddededededededee fddZdd Zdd ZdejfddZdS )     N)closing)Optional)
get_logger)barrier)create_c10d_storeget_free_portget_socket_with_portzAddress already in usezSocket Timeoutz_tcp_store/num_members   X  T   	is_serverserver_addrserver_port
world_sizetimeoutwait_for_workers	use_libuvc              
   C   sJ  |d ur	t d tjdddk}|dkr$|dkr$td| d| |dkr.t d| |dkr4|nd}	 |dkr>|}	nt }	t d
||	|| || z!tj	||	|| t
j|d||d}
|rft|
||d t d |
W S  ty } z*t|tkr||k rt d|	|| |d7 }ntd| d|	 d| W Y d }~nd }~ww q7)Nzargument use_libuv is deprecated and ignored. Set USE_LIBUV environment variable to "0" to disable libuv, or "1" to enable it. If the env var is not set, libuv will be used by default.Z	USE_LIBUV1r	   r
   zCserver_port must be specified when world_size > 1, got server_port=z, world_size=z+sever_port: %s, specified, ignoring retriesTziCreating c10d store on %s:%s
  world_size  : %s
  is_server   : %s
  timeout(sec): %s
  use_libuv   : %s
)seconds)Z	host_nameportr   Z	is_masterr   r   r   )r   zSuccessfully created c10d storez)port: %s already in use, attempt: [%s/%s]zon z, port: z already in use)loggerwarningosenvironget
ValueErrorinfor   distZTCPStoredatetime	timedelta_check_full_rankRuntimeErrorstr_ADDRESS_IN_USE)r   r   r   r   r   r   retriesr   attemptr   storee r)   Z/var/www/auris/lib/python3.10/site-packages/torch/distributed/elastic/utils/distributed.pyr      sv   




r   c              
   C   sP   zt | |t|d W d S  ty' } zt|tkr"td| d| d }~ww )N)Z
key_prefixZbarrier_timeoutztimed out waiting for all z members to join)r   _TCP_STORE_INITr"   r#   _SOCKET_TIMEOUTTimeoutError)r'   r   r   r(   r)   r)   r*   r!   v   s   
r!   c                  C   s>   t  } t|  |  d W  d   S 1 sw   Y  dS )a  
    Returns an unused port on localhost.

    This function finds an unused port on localhost by opening to socket to bind
    to a port and then closing it.

    Returns:
        int: an unused port on localhost

    Example:
        >>> # xdoctest: +SKIP("Nondeterministic")
        >>> get_free_port()
        63976

    .. note::
        The port returned by :func:`get_free_port` is not reserved and may be
        taken by another process after this function returns.
    r
   N)r   r   getsockname)sockr)   r)   r*   r      s   

$r   returnc                  C   s   t jddt jt jd} | D ]<}|\}}}}}t  |||}z|d |d |W   S  tyI } z|  tj	d|d W Y d}~qd}~ww t
d)	a  
    Returns a free port on localhost that is "reserved" by binding a temporary
    socket on it. Close the socket before passing the port to the entity
    that requires it. Usage example

    ::

    sock = _get_socket_with_port()
    with closing(sock):
        port = sock.getsockname()[1]
        sock.close()
        # there is still a race-condition that some other process
        # may grab this port before func() runs
        func(port)
    	localhostN)hostr   familytype)r1   r   r   zSocket creation attempt failed.)exc_infozFailed to create a socket)socketgetaddrinfo	AF_UNSPECSOCK_STREAMbindlistenOSErrorcloser   r   r"   )addrsaddrr3   r4   proto_sr(   r)   r)   r*   r      s    


r   )r	   r
   r   Tr   N)r   r   r6   
contextlibr   typingr   Ztorch.distributeddistributedr   Z'torch.distributed.elastic.utils.loggingr   Z%torch.distributed.elastic.utils.storer   __all____name__r   r$   r,   r+   boolr#   intfloatr   r!   r   r   r)   r)   r)   r*   <module>   sJ   
X