1

I am trying to track a log file that is being created by another running program. I would like to return True if the word 'day' is in found in the updated log (which means the code other has run successfully). The problem is- sometimes the other program isn't sucessful and just gets stuck on the same line and I would like to return false if this is happens. I've tried tracking if it gets stuck on the same line using seek() and tell() like this:

def is_successful_test(self):
    '''
    checks if external code was successful
    '''
    day_out = os.path.join(self.working_dir, 'day.out')
    day_out = open(day_out)
    i = 0

    while True:
            day_out.seek(i)
            latest_line = day_out.readline()
            i_old = dat_out.tell()
            if 'day' in latest_line:
                    return True
            time.sleep(60)
            i = aims_out.tell()
            if i == i_old:
                    self.output("day file not updating")
                    break
    return False

But this always return the same thing for i and i_old and always returns false when log is indeed still updating (I'm probably not doing it correctly I am a newbie). It also returned the same thing when I tried to track the size of the file (using os.stat).

I've also tried to use,

def is_successful(self):
    '''
    checks if external code was successful
    '''
    day_out = os.path.join(self.working_dir, 'day.out')
    day_out = open(day_out,"r")
    while True:
            line = day_out.readline()
            if line == '':
                    break
            if 'day' in line:
                    day_out.close()
                    return True
    day_out.close()
    return False

Which works well for when the other program runs very quickly but seems to sort of time out and return false if the code has been running for too long.

I am sort of stumped as to why I can't find a consistent solution. Tracking the size or line of the file or even the time difference seems to be a good idea but I can't quite seem to implement it. I would like to find the simplest solution without downloading something like watchdog which I've seem in other posts. Any help would be appreciated! Thanks.

1 Answers1

1

You are trying to make use of seek and tell in ways that make no sense: tell just return an offset of the position in the current open file (in the current process) - it would not return a different number if the file has been opened again in other process and that instance of the openfile is in another position. Unless prior to calling tell you issue a seek asking the cursor to be placed at the end of the file:day_out.seek(0, OS.SEEK_END) . But your idea is so convoluted it would not work either.

For what you want, you can just keep issuing readline on your file: if you ar at the end of the file, you will get an empty string. If the other process have written extra lines from the point your cursor is at, the next line will be retrieved.

counter = 0
while True:
   line = day_out.readline()
   if "day" in line:
      do_things()
      counter = 0
   elif line == '':
      time.sleep(60)
      counter += 1
   if counter > 5:
       output("day file not updating")
jsbueno
  • 77,044
  • 9
  • 114
  • 168
  • Hi thank you for the response. For what I gather- this works if the program finishes normally(and returns an empty string) but doesnt work if the program hangs on a line for an extended period of time. ideally I would like to add another functionality that returns false if the program is hanging on the same line too long (and therefore needs to be aborted). Thanks again. – user3806522 Jul 24 '15 at 18:07
  • Just add a counter them, and if readline returns empty for a specified number of times in a row, declare it failed. I had updated my example above to reflect this. – jsbueno Jul 25 '15 at 06:38
  • Great this is what I was looking for. Thanks! – user3806522 Jul 25 '15 at 19:19