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.