U
    yh7                     @   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m	Z	m
Z
mZmZ er\d dlmZ ddgZe eZeeeeedd	dZG d
d dZdS )    N)ThreadPoolExecutorEvent)DictListOptionalTextIOTYPE_CHECKING)Futuretail_logfileTailLogheaderfiledstfinishedinterval_secc              	   C   sx   t j|s$| rd S t| q t|dd>}| }|rR||  |  q2| r^qjq2t| q2W 5 Q R X d S )Nreplace)errors)	ospathexistsis_settimesleepopenreadlinewrite)r   r   r   r   r   fpline r    d/var/www/html/venv/lib/python3.8/site-packages/torch/distributed/elastic/multiprocessing/tail_log.pyr      s    c                   @   sf   e Zd ZdZdeeeef eeeeef  e	dddZ
d ddd	Zddd
dZedddZdS )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                 C   st   t |}d | _|dkr2t|| jj d| d| _|| _|| _|| _|| _dd |	 D | _
g | _|| _d| _d S )Nr   _)max_workersZthread_name_prefixc                 S   s   i | ]}|t  qS r    r   ).0
local_rankr    r    r!   
<dictcomp>m   s     z$TailLog.__init__.<locals>.<dictcomp>F)len_threadpoolr   	__class____qualname___name_dst
_log_files_log_line_prefixeskeys_finished_events_futs_interval_sec_stopped)selfr#   r$   r   r%   r   nr    r    r!   __init__Y   s"    zTailLog.__init__)returnc                 C   sx   | j s
| S | j D ]^\}}d| j | d}| jrH|| jkrH| j| }| j| j jt||| j	| j
| | jd q| S )N[z]:r   )r,   r1   itemsr/   r2   r5   appendZsubmitr   r0   r4   r6   )r8   r)   r   r   r    r    r!   startt   s"    

zTailLog.startc                 C   s   | j  D ]}|  q
t| jD ]P\}}z|  W q" tk
rp } ztd| j	||j
j| W 5 d }~X Y q"X q"| jr| jjdd d| _d S )Nz$error in log tailor for %s%s. %s: %sT)wait)r4   valuesset	enumerater5   result	Exceptionloggererrorr/   r-   r.   r,   shutdownr7   )r8   r   r)   fer    r    r!   stop   s     
  zTailLog.stopc                 C   s   | j S )N)r7   )r8   r    r    r!   stopped   s    zTailLog.stopped)Nr"   )__name__
__module__r.   __doc__strr   intr   r   floatr:   r?   rK   boolrL   r    r    r    r!   r   2   s   +  
)loggingr   r   Zconcurrent.futures.threadr   	threadingr   typingr   r   r   r   r	   Zconcurrent.futures._baser
   __all__	getLoggerrM   rF   rP   rR   r   r   r    r    r    r!   <module>
   s    
    