3
I4W^O  ã               @   sP   d dl mZmZmZmZ d dlZd dlZd dlZejdk r>e	Z
G dd„ dƒZdS )é    )Úabsolute_importÚdivisionÚprint_functionÚunicode_literalsNz3.3c               @   s2   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ ZdS )Ú	FileBatonz0A primitive, file-based synchronization utility.çš™™™™™¹?c             C   s   || _ || _d| _dS )zñ
        Creates a new :class:`FileBaton`.

        Args:
            lock_file_path: The path to the file used for locking.
            wait_seconds: The seconds to periorically sleep (spin) when
                calling ``wait()``.
        N)Úlock_file_pathÚwait_secondsÚfd)Úselfr   r	   © r   ú7/tmp/pip-build-e66j97b2/torch/torch/utils/file_baton.pyÚ__init__   s    	zFileBaton.__init__c             C   s6   yt j| jt jt jB ƒ| _dS  tk
r0   dS X dS )z˜
        Tries to atomically create a file under exclusive access.

        Returns:
            True if the file could be created, else False.
        TFN)ÚosÚopenr   ÚO_CREATÚO_EXCLr
   ÚFileExistsError)r   r   r   r   Útry_acquire   s
    zFileBaton.try_acquirec             C   s$   xt jj| jƒrtj| jƒ qW dS )zÆ
        Periodically sleeps for a certain amount until the baton is released.

        The amount of time slept depends on the ``wait_seconds`` parameter
        passed to the constructor.
        N)r   ÚpathÚexistsr   ÚtimeÚsleepr	   )r   r   r   r   Úwait)   s    zFileBaton.waitc             C   s   t j| jƒ t j| jƒ dS )z'Releaes the baton and removes its file.N)r   Úcloser
   Úremover   )r   r   r   r   Úrelease3   s    zFileBaton.releaseN)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r      s
   

r   )Ú
__future__r   r   r   r   r   Úsysr   ÚversionÚOSErrorr   r   r   r   r   r   Ú<module>   s   
