
    sg!                         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Zd Zd Zdej                  fdZy)    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	is_serverserver_addrserver_port
world_sizetimeoutwait_for_workers	use_libuvc           	         |t         j                  d       t        j                  j	                  dd      dk(  }|dk(  r|dkD  rt        d| d|       |dk7  rt         j                  d|       |dk(  r|nd}	 |dk7  r|}	n
t               }	t         j                  d	||	|| ||       	 t        j                  ||	|| t        j                  |
      ||      }
|rt        |
||       t         j                  d       |
S # t        $ rS}t        |      t        k(  r6||k  rt         j                  d|	||       |dz  }nt        d| d|	 d      | Y d }~nd }~ww xY w)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.	USE_LIBUV1   zCserver_port must be specified when world_size > 1, got server_port=z, world_size=z+sever_port: %s, specified, ignoring retriesziCreating c10d store on %s:%s
  world_size  : %s
  is_server   : %s
  timeout(sec): %s
  use_libuv   : %s
)seconds)	host_nameportr   	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   distTCPStoredatetime	timedelta_check_full_rankRuntimeErrorstr_ADDRESS_IN_USE)r
   r   r   r   r   r   retriesr   attemptr   storees               ^/var/www/html/venv/lib/python3.12/site-packages/torch/distributed/elastic/utils/distributed.pyr   r      s    9	
 

{C0C7IbZ!^QR]Q^^klvkwx
 	
 bA;O %*gG
"D ?D#
 	
#	MM%%# **7;!1#E   
GDKK9:L 	 1v(W$NNC	 qLG&k](4&H  	G s   )AD   	E	A	EEc                     	 t        | |t        |       y # t        $ r(}t        |      t        k(  rt        d| d      | d }~ww xY w)N)
key_prefixbarrier_timeoutztimed out waiting for all z members to join)r   _TCP_STORE_INITr&   r'   _SOCKET_TIMEOUTTimeoutError)r+   r   r   r,   s       r-   r%   r%   v   sS    zowW q6_$,ZL8HI s    	A#AAc                  ~    t               } t        |       5  | j                         d   cddd       S # 1 sw Y   yxY w)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)socks    r-   r   r      s9    &  !D	 %!!$% % %s   3<returnc                     t        j                  ddt         j                  t         j                        } | D ]G  }|\  }}}}}t        j                   |||      }	 |j	                  d       |j                  d       |c S  t        d      # t        $ r1}|j                          t        j                  d|       Y d}~d}~ww xY w)	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)r9   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addrr;   r<   proto_sr,   s           r-   r	   r	      s    " tF,<,<6CUCUE  	J$(!eQMM&$.	JFF#$HHQKH	J 2
33  	JGGINN<qNII	Js   #B	C'CC)r   r   iX  T   N)r#   r   r>   
contextlibr   typingr   torch.distributeddistributedr!   'torch.distributed.elastic.utils.loggingr   %torch.distributed.elastic.utils.storer   __all____name__r   r(   r2   r1   boolr'   intfloatr   r%   r   r	        r-   <module>rY      s     	      > 9 I	H	*"* ! $UUU U 	U
 U U ~Up	%04fmm 4rX   