<commit_before>import os
import random
import time

class Watcher():
  def __init__(self, files=None, callback=None, basespeed=0.1):
    self._files = files
    self._callback = callback
    self._started = False
    self._running = False
    self._basespeed = basespeed
    self._waitlimit = 2048

  def _terminate(self):
    self._running = False

  def is_active(self):
    return self._running

  def start(self, asynchronous=False):
    if self._started:
      return
    self._started = True
    self._running = True
    try:
      while True:
        nextfile = next(self._files)
        backoff = self._basespeed
        total_wait = 0
        while not os.path.exists(nextfile):
          if backoff >= self._waitlimit * 2:
            if self._callback:
              self._callback(file=nextfile, success=False, wait=total_wait)
            self._terminate()
            return
          if backoff > self._waitlimit:
            waittime = self._waitlimit
          else:
            waittime = random.uniform(self._basespeed, backoff)
          time.sleep(waittime)
          total_wait += waittime
          backoff *= 2

        if self._callback:
          self._callback(file=nextfile, success=True, wait=total_wait)
    except StopIteration:
      self._terminate()
      return<commit_msg>treat first file differently and wait for shorter periods<commit_after>import os
import random
import time

class Watcher():
  def __init__(self, files=None, callback=None, basespeed=0.1):
    self._files = files
    self._callback = callback
    self._started = False
    self._running = False
    self._basespeed = basespeed
    self._waitlimit = 2048

  def _terminate(self):
    self._running = False

  def is_active(self):
    return self._running

  def start(self, asynchronous=False):
    if self._started:
      return
    self._started = True
    self._running = True
    firstfile = True
    try:
      while True:
        nextfile = next(self._files)
        backoff = self._basespeed
        total_wait = 0
        while not os.path.exists(nextfile):
          if backoff >= self._waitlimit * 2:
            if self._callback:
              self._callback(file=nextfile, success=False, wait=total_wait)
            self._terminate()
            return
          if backoff > self._waitlimit:
            waittime = self._waitlimit
          else:
            waittime = random.uniform(self._basespeed, backoff)
          if firstfile:
            waittime = max(3, waittime)
          time.sleep(waittime)
          total_wait += waittime
          if firstfile:
            backoff += 40
          else:
            backoff *= 2

        if self._callback:
          self._callback(file=nextfile, success=True, wait=total_wait)
      firstfile = False
    except StopIteration:
      self._terminate()
      return
{'fraction_non_alphanumeric': 0.08353894959464223, 'fraction_numerical': 0.007402185407120197, 'mean_word_length': 2.122112211221122, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7186130', 'n_tokens_mistral': 836, 'n_tokens_neox': 812, 'n_words': 199}