Python wait for file

15 Python code examples are found related to " wait for file". You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: backend.py    From ncluster with MIT License 9 votes vote down vote up
def wait_for_file(self, fn: str, max_wait_sec: int = 3600 * 24 * 365,
                    check_interval: float = 0.02) -> bool:
    """
    Waits for file maximum of max_wait_sec. Returns True if file was detected within specified max_wait_sec
    Args:
      fn: filename on task machine
      max_wait_sec: how long to wait in seconds
      check_interval: how often to check in seconds
    Returns:
      False if waiting was was cut short by max_wait_sec limit, True otherwise
    """
    print("Waiting for file", fn)
    start_time = time.time()
    while True:
      if time.time() - start_time > max_wait_sec:
        util.log(f"Timeout exceeded ({max_wait_sec} sec) for {fn}")
        return False
      if not self.exists(fn):
        time.sleep(check_interval)
        continue
      else:
        break
    return True 
Example 2
Source File: binlogmove_privileges.py    From mysql-utilities with GNU General Public License v2.0 8 votes vote down vote up
def wait_for_file(source_dir, filename, timeout=10):
        """Wait for the creation of the specific file.

        This method checks if the specified file exists and waits for it to
        appear during the specified amount of time (by default 10 seconds).

        source_dir[in]  Source directory where the file is located.
        filename[in]    Name of the file to wait for.
        timeout[in]     Time to wait in seconds (by default 10 seconds).
        """
        file_path = os.path.normpath(os.path.join(source_dir, filename))
        attempts = 0
        while attempts < timeout:
            # Check if the file exists.
            if os.path.isfile(file_path):
                return
            # Wait 1 second before trying again.
            time.sleep(1)
            attempts += 1 
Example 3
Source File: fsclient.py    From a2ml with Apache License 2.0 6 votes vote down vote up
def wait_for_file(path, if_wait_for_file, num_tries=30, interval_sec=1):
    if if_wait_for_file:
        nTry = 0
        while nTry <= num_tries:
            if is_file_exists(path):
                break

            if path != "/mnt/ready.txt":
                wait_for_fs_ready()

            if is_file_exists(path):
                break

            logging.info("File %s does not exist. Wait for %s sec" %
                         (path, interval_sec))

            nTry = nTry + 1
            time.sleep(interval_sec)

        return is_file_exists(path)

    return True 
Example 4
Source File: file_util.py    From kicad-automation-scripts with Apache License 2.0 6 votes vote down vote up
def wait_for_file_created_by_process(pid, file, timeout=5):
    process = psutil.Process(pid)

    DELAY = 0.01
    for i in range(int(timeout/DELAY)):
        open_files = process.open_files()
        logger.debug(open_files)
        if os.path.isfile(file):
            file_open = False
            for open_file in open_files:
                if open_file.path == file:
                    file_open = True
            
            if file_open:
                logger.debug('Waiting for process to close file')
            else:
                return
        else:
            logger.debug('Waiting for process to create file')
        time.sleep(DELAY)

    raise RuntimeError('Timed out waiting for creation of %s' % file) 
Example 5
Source File: hockey.py    From Trusty-cogs with MIT License 6 votes vote down vote up
def wait_for_file(self, ctx):
        """
            Waits for the author to upload a file
        """
        msg = None
        while msg is None:

            def check(m):
                return m.author == ctx.message.author and m.attachments != []

            try:
                msg = await self.bot.wait_for("message", check=check, timeout=60)
            except asyncio.TimeoutError:
                await ctx.send(_("Emoji changing cancelled"))
                break
            if msg.content.lower().strip() == "exit":
                await ctx.send(_("Emoji changing cancelled"))
            break
        return msg 
Example 6
Source File: utils.py    From parsl with Apache License 2.0 6 votes vote down vote up
def wait_for_file(path, seconds=10):
    for i in range(0, int(seconds * 100)):
        time.sleep(seconds / 100.)
        if os.path.exists(path):
            break
    yield 
Example 7
Source File: bus_finder.py    From rpisurv with GNU General Public License v2.0 5 votes vote down vote up
def wait_for_file(self):
        if self.path:
            self.wait_for_path_to_exist()
        else:
            self.find_address_file()
        self.wait_for_dbus_address_to_be_written_to_file()


#busfinder = BusFinder()
#print busfinder.get_address() 
Example 8
Source File: of_capture.py    From r_e_c_u_r with GNU General Public License v3.0 5 votes vote down vote up
def wait_for_raw_file(self):
        if os.path.exists(self.video_dir + 'raw.h264'):
            mp4box_process, recording_name = self.convert_raw_recording()
                   
            self.root.after(0, self.wait_for_recording_to_save, mp4box_process, recording_name)
            self.update_capture_settings()
        elif os.path.exists(self.video_dir + 'raw.mp4') and self.of_recording_finished:
            recording_path , recording_name = self.generate_recording_path()
            os.rename(self.video_dir + 'raw.mp4', recording_path )
            self.is_recording = False
            self.data.create_new_slot_mapping_in_first_open(recording_name)
            self.update_capture_settings()
        else:
            self.root.after(1000, self.wait_for_raw_file) 
Example 9
Source File: bus_finder.py    From python-omxplayer-wrapper with GNU Lesser General Public License v3.0 5 votes vote down vote up
def wait_for_file(self):
        if self.path:
            self.wait_for_path_to_exist()
        else:
            self.find_address_file()
        self.wait_for_dbus_address_to_be_written_to_file() 
Example 10
Source File: qtconsole.py    From flask-unchained with MIT License 5 votes vote down vote up
def wait_for_connection_file(connection_file):
    for _ in range(100):
        try:
            st = os.stat(connection_file)
        except OSError as exc:
            if exc.errno != errno.ENOENT:
                raise
        else:
            if st.st_size > 0:
                break
        time.sleep(0.1) 
Example 11
Source File: dyode.py    From dyode with GNU General Public License v3.0 5 votes vote down vote up
def wait_for_file(params):
    log.debug('Waiting for file ...')
    log.debug(datetime.datetime.now())
    # YOLO
    log.debug(params)
    # Use a dedicated name for each process to prevent race conditions
    process_name = multiprocessing.current_process().name
    manifest_filename = 'manifest_' + process_name + '.cfg'
    receive_file(manifest_filename, params['port'])
    files = parse_manifest(manifest_filename)
    if len(files) == 0:
        log.error('No file detected')
        return 0
    log.debug('Manifest content : %s' % files)
    for f in files:
        filename = os.path.basename(f)
        temp_file = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(12))
        receive_file(temp_file, params['port'])
        log.info('File ' + f + ' received')
	log.debug(datetime.datetime.now())
	hash_file(temp_file)
        if hash_file(temp_file) != files[f]:
            log.error('Invalid checksum for file ' + f)
            os.remove(f)
            log.error('Calculating next file hash...')
            continue
        else:
            log.info('Hashes match !')
	    shutil.move(temp_file, params['out'] + '/' + filename)
	    log.info('File ' + filename + ' available at ' + params['out'])
    os.remove(manifest_filename)



################### Send specific functions ####################################

# Send a file using udpcast 
Example 12
Source File: colab_kernel_init.py    From agents with Apache License 2.0 5 votes vote down vote up
def WaitForFilePath(path_pattern, timeout_sec):
  start = time.time()
  result = []
  while not result:
    if time.time() - start > timeout_sec:
      return result
    result = glob.glob(path_pattern)
    time.sleep(0.1)
  return result 
Example 13
Source File: _base_service.py    From treadmill with Apache License 2.0 5 votes vote down vote up
def wait_for_file(filename, timeout=None):
    """Wait at least ``timeout`` seconds for a file to appear or be modified.

    :param ``int`` timeout:
        Minimum amount of seconds to wait for the file.
    :returns ``bool``:
        ``True`` if there was an event, ``False`` otherwise (timeout).
    """
    if timeout is None:
        timeout = DEFAULT_TIMEOUT

    elif timeout == 0:
        return os.path.exists(filename)

    filedir = os.path.dirname(filename)
    # TODO: Fine tune the watcher mask for efficiency.
    watcher = dirwatch.DirWatcher(filedir)

    now = time.time()
    end_time = now + timeout
    while not os.path.exists(filename):
        if watcher.wait_for_events(timeout=max(0, end_time - now)):
            watcher.process_events()

        now = time.time()
        if now > end_time:
            return False

    return True 
Example 14
Source File: util.py    From multisensory with Apache License 2.0 5 votes vote down vote up
def wait_for_file(fname, timeout_sec):
  start = now_sec()
  while not os.path.exists(fname):
    if now_sec() - start > timeout_sec:
      fail('File does not exist after %f seconds: %s' % (timeout_sec, fname))
    else:
      time.sleep(1) 
Example 15
Source File: nginx.py    From canvas with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def wait_for_file(filename):
    for x in range(100):
        # Wait for the file to have actually been written to, avoiding a race condition.
        if os.path.exists(filename) and len(file(filename).read()):
            break
        time.sleep(0.05)
    else:
        raise Exception("Timed out waiting for %s" % filename)