
    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m	Z	m
Z
mZmZ erd dlmZ ddgZ e j                   e      Zded	ed
ededef
dZ G d d      Zy)    N)ThreadPoolExecutor)Event)DictListOptionalTextIOTYPE_CHECKING)Futuretail_logfileTailLogheaderfiledstfinishedinterval_secc                    t         j                  j                  |      sF|j                         ry t	        j
                  |       t         j                  j                  |      sFt        |d      5 }	 |j                         }|r|j                  |  |        n&|j                         rnt	        j
                  |       O	 d d d        y # 1 sw Y   y xY w)Nreplace)errors)	ospathexistsis_settimesleepopenreadlinewrite)r   r   r   r   r   fplines          e/var/www/html/venv/lib/python3.12/site-packages/torch/distributed/elastic/multiprocessing/tail_log.pyr   r      s     ggnnT"??

<  ggnnT"
 
d9	% -;;=D		VHTF+,??$ JJ|,  - - -s   3ACCc                   h    e Zd ZdZ	 	 ddedeeef   dedeeeef      de	f
dZ
dd
ZddZd	efdZy)r   a  
    Tail the given log files.

    The log files do not have to exist when the ``start()`` method is called. The tail-er will gracefully wait until
    the log files are created by the producer and will tail the contents of the
    log files until the ``stop()`` method is called.

    .. warning:: ``TailLog`` will wait indefinitely for the log file to be created!

    Each log file's line will be suffixed with a header of the form: ``[{name}{idx}]:``,
    where the ``name`` is user-provided and ``idx`` is the index of the log file
    in the ``log_files`` mapping. ``log_line_prefixes`` can be used to override the
    header for each log file.

    Usage:

    ::

     log_files = {0: "/tmp/0_stdout.log", 1: "/tmp/1_stdout.log"}
     tailer = TailLog("trainer", log_files, sys.stdout).start()
     # actually run the trainers to produce 0_stdout.log and 1_stdout.log
     run_trainers()
     tailer.stop()

     # once run_trainers() start writing the ##_stdout.log files
     # the tailer will print to sys.stdout:
     # >>> [trainer0]:log_line1
     # >>> [trainer1]:log_line1
     # >>> [trainer0]:log_line2
     # >>> [trainer0]:log_line3
     # >>> [trainer1]:log_line2

    .. note:: Due to buffering log lines between files may not necessarily
              be printed out in order. You should configure your application's
              logger to suffix each log line with a proper timestamp.

    Nname	log_filesr   log_line_prefixesr   c                 L   t        |      }d | _        |dkD  r+t        || j                  j                   d|       | _        || _        || _        || _        || _        |j                         D ci c]  }|t                c}| _        g | _        || _        d| _        y c c}w )Nr   _)max_workersthread_name_prefixF)len_threadpoolr   	__class____qualname___name_dst
_log_files_log_line_prefixeskeysr   _finished_events_futs_interval_sec_stopped)selfr"   r#   r   r$   r   n
local_ranks           r    __init__zTailLog.__init__Y   s     	Nq51&*nn&A&A%B!D6#J D
 
	#"32;..2B3
$.J3
 $&
)3
s   1B!returnc                    | j                   s| S | j                  j                         D ]  \  }}d| j                   | d}| j                  r|| j                  v r| j                  |   }| j
                  j                  | j                   j                  t        ||| j                  | j                  |   | j                                | S )N[z]:)r   r   r   r   r   )r*   r/   itemsr-   r0   r3   appendsubmitr   r.   r2   r4   )r6   r8   r   r   s       r    startzTailLog.startt   s    K $ 5 5 7 	JZL3F&&:9P9P+P00<JJ  '' !		!22:>!%!3!3 ( 			     c           	         | j                   j                         D ]  }|j                           t        | j                        D ]  \  }}	 |j                           | j                  r| j                  j                  d       d| _        y # t        $ rA}t        j                  d| j                  ||j                  j                  |       Y d }~d }~ww xY w)Nz$error in log tailor for %s%s. %s: %sT)wait)r2   valuesset	enumerater3   result	Exceptionloggererrorr-   r+   r,   r*   shutdownr5   )r6   r   r8   fes        r    stopzTailLog.stop   s    --446 	HLLN	 'tzz2 
	MJ	

	 %%4%0  :JJKK,, s   B	C7CCc                     | j                   S )N)r5   )r6   s    r    stoppedzTailLog.stopped   s    }}rA   )Ng?)r:   r   )r:   N)__name__
__module__r,   __doc__strr   intr   r   floatr9   r@   rN   boolrP    rA   r    r   r   2   sp    $V 7;! S> 	
 $DcN3 6(* rA   )loggingr   r   concurrent.futures.threadr   	threadingr   typingr   r   r   r   r	   concurrent.futures._baser
   __all__	getLoggerrQ   rI   rT   rV   r   r   rX   rA   r    <module>r`      sy     	  8  > > /9
%			8	$---!'-38-HM-0l lrA   