B
    ®»ˆd¥"  ã               @   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mZ ddlm	Z	 ddl
mZ G dd„ deƒZedƒZG dd	„ d	eƒZG d
d„ deƒZG dd„ dejƒZdS )z"Writes events to disk in a logdir.é    N)Útf)Ú	event_pb2)ÚRecordWriterc               @   s   e Zd Zdd„ Zdd„ ZdS )ÚAtomicCounterc             C   s   || _ t ¡ | _d S )N)Ú_valueÚ	threadingÚLockÚ_lock)ÚselfÚinitial_value© r   ú^/var/www/html/venv/lib/python3.7/site-packages/tensorboard/summary/writer/event_file_writer.pyÚ__init__   s    zAtomicCounter.__init__c          
   C   s.   | j  z| jS |  jd7  _X W d Q R X d S )Né   )r	   r   )r
   r   r   r   Úget"   s    zAtomicCounter.getN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r      s   r   c               @   s:   e Zd ZdZddd„Zdd„ Zd	d
„ Zdd„ Zdd„ ZdS )ÚEventFileWritera&  Writes `Event` protocol buffers to an event file.

    The `EventFileWriter` class creates an event file in the specified
    directory, and asynchronously writes Event protocol buffers to the
    file. The Event file is encoded using the tfrecord format, which is
    similar to RecordIO.
    é
   éx   Ú c          	   C   s”   || _ tjj |¡ tj |dt ¡ t	 
¡ t ¡ t ¡ f ¡| | _tjj | jd¡| _tt| jƒ||ƒ| _tjt ¡ dd}|  |¡ |  ¡  dS )aœ  Creates a `EventFileWriter` and an event file to write to.

        On construction the summary writer creates a new event file in `logdir`.
        This event file will contain `Event` protocol buffers, which are written to
        disk via the add_event method.
        The other arguments to the constructor control the asynchronous writes to
        the event file:

        Args:
          logdir: A string. Directory where event file will be written.
          max_queue_size: Integer. Size of the queue for pending events and summaries.
          flush_secs: Number. How often, in seconds, to flush the
            pending events and summaries to disk.
        z"events.out.tfevents.%010d.%s.%s.%sÚwbzbrain.Event:2)Z	wall_timeZfile_versionN)Ú_logdirr   ÚioZgfileÚmakedirsÚosÚpathÚjoinÚtimeÚsocketÚgethostnameÚgetpidÚ_global_uidr   Z
_file_nameZGFileZ_general_file_writerÚ_AsyncWriterr   Ú_async_writerr   ÚEventÚ	add_eventÚflush)r
   ZlogdirÚmax_queue_sizeÚ
flush_secsZfilename_suffixZ_eventr   r   r   r   6   s"    
zEventFileWriter.__init__c             C   s   | j S )z7Returns the directory where event file will be written.)r   )r
   r   r   r   Ú
get_logdirb   s    zEventFileWriter.get_logdirc             C   s0   t |tjƒstdt|ƒ ƒ‚| j | ¡ ¡ dS )zeAdds an event to the event file.

        Args:
          event: An `Event` protocol buffer.
        z.Expected an event_pb2.Event proto,  but got %sN)Ú
isinstancer   r&   Ú	TypeErrorÚtyper%   ÚwriteZSerializeToString)r
   Úeventr   r   r   r'   f   s
    zEventFileWriter.add_eventc             C   s   | j  ¡  dS )zŠFlushes the event file to disk.

        Call this method to make sure that all pending events have been
        written to disk.
        N)r%   r(   )r
   r   r   r   r(   s   s    zEventFileWriter.flushc             C   s   | j  ¡  dS )zÇPerforms a final flush of the event file to disk, stops the
        write/flush worker and closes the file.

        Call this method when you do not need the summary writer
        anymore.
        N)r%   Úclose)r
   r   r   r   r1   {   s    zEventFileWriter.closeN)r   r   r   )	r   r   r   Ú__doc__r   r+   r'   r(   r1   r   r   r   r   r   -   s   
+r   c               @   s2   e Zd ZdZddd„Zdd„ Zdd	„ Zd
d„ ZdS )r$   zWrites bytes to a file.é   r   c             C   sB   || _ d| _t |¡| _t| j| j |ƒ| _t ¡ | _	| j 
¡  dS )a@  Writes bytes to a file asynchronously. An instance of this class
        holds a queue to keep the incoming data temporarily. Data passed to the
        `write` function will be put to the queue and the function returns
        immediately. This class also maintains a thread to write data in the
        queue to disk. The first initialization parameter is an instance of
        `tensorboard.summary.record_writer` which computes the CRC checksum and
        then write the combined result to the disk. So we use an async approach
        to improve performance.

        Args:
            record_writer: A RecordWriter instance
            max_queue_size: Integer. Size of the queue for pending bytestrings.
            flush_secs: Number. How often, in seconds, to flush the
                pending bytestrings to disk.
        FN)Ú_writerÚ_closedÚqueueÚQueueÚ_byte_queueÚ_AsyncWriterThreadÚ_workerr   r   r	   Ústart)r
   Úrecord_writerr)   r*   r   r   r   r   ˆ   s    
z_AsyncWriter.__init__c          	   C   s0   | j   | jrtdƒ‚| j |¡ W dQ R X dS )z4Enqueue the given bytes to be written asychronously.zWriter is closedN)r	   r5   ÚIOErrorr8   Úput)r
   Ú
bytestringr   r   r   r/   ¡   s    z_AsyncWriter.writec          	   C   s8   | j ( | jrtdƒ‚| j ¡  | j ¡  W dQ R X dS )z…Write all the enqueued bytestring before this flush call to disk.

        Block until all the above bytestring are written.
        zWriter is closedN)r	   r5   r=   r8   r   r4   r(   )r
   r   r   r   r(   ¨   s
    
z_AsyncWriter.flushc          	   C   sF   | j sB| j0 | j s8d| _ | j ¡  | j ¡  | j ¡  W dQ R X dS )z@Closes the underlying writer, flushing any pending writes first.TN)r5   r	   r:   Ústopr4   r(   r1   )r
   r   r   r   r1   ³   s    

z_AsyncWriter.closeN)r3   r   )r   r   r   r2   r   r/   r(   r1   r   r   r   r   r$   …   s
   
r$   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	r9   z;Thread that processes asynchronous writes for _AsyncWriter.c             C   s<   t j | ¡ d| _|| _|| _|| _d| _d| _t	ƒ | _
dS )a  Creates an _AsyncWriterThread.

        Args:
          queue: A Queue from which to dequeue data.
          record_writer: An instance of record_writer writer.
          flush_secs: How often, in seconds, to flush the
            pending file to disk.
        Tr   FN)r   ÚThreadr   ÚdaemonÚ_queueÚ_record_writerÚ_flush_secsÚ_next_flush_timeÚ_has_pending_dataÚobjectÚ_shutdown_signal)r
   r6   r<   r*   r   r   r   r   Á   s    	z_AsyncWriterThread.__init__c             C   s   | j  | j¡ |  ¡  d S )N)rC   r>   rI   r   )r
   r   r   r   r@   Ô   s    z_AsyncWriterThread.stopc             C   sÊ   xÄt   ¡ }| j| }d }zdyH|dkr4| j d|¡}n| j d¡}|| jkrNd S | j |¡ d| _W n tj	k
rx   Y nX W d |rŒ| j 
¡  X t   ¡ }|| jkr| jr¶| j ¡  d| _|| j | _qW d S )Nr   TF)r   rF   rC   r   rI   rD   r/   rG   r6   ÚEmptyÚ	task_doner(   rE   )r
   ÚnowZqueue_wait_durationÚdatar   r   r   ÚrunØ   s,    





z_AsyncWriterThread.runN)r   r   r   r2   r   r@   rN   r   r   r   r   r9   ¾   s   r9   )r2   r   r6   r    r   r   Ztensorboard.compatr   Ztensorboard.compat.protor   Z(tensorboard.summary.writer.record_writerr   rH   r   r#   r   r$   rA   r9   r   r   r   r   Ú<module>   s   X9