I have a program that keeps writing to a file every second. File writing is happening in a thread parallel to the UI. Due to some hardware issue it stops writing sometimes o
To determine whether the file changes on time in a GUI program, you could use standard tools for your event loop, to run a function every interval seconds e.g., here's how to do it in tkinter
:
#!/usr/bin/env python3
import logging
import os
import sys
import tkinter
from datetime import datetime
from time import monotonic as timer, localtime
path = sys.argv[1]
interval = 120 # check every 2 minutes
def check(last=[None]):
mtime = os.path.getmtime(path) # or os.path.getsize(path)
logging.debug("mtime %s", datetime.fromtimestamp(mtime))
if last[0] == mtime: #NOTE: it is always False on the first run
logging.error("file metadata hasn't been updated, exiting..")
root.destroy() # exit GUI
else: # schedule the next run
last[0] = mtime
root.after(round(1000 * (interval - timer() % interval)), check)
logging.basicConfig(level=logging.DEBUG,
filename=os.path.splitext(__file__)[0] + ".log",
format="%(asctime)-15s %(message)s", datefmt="%F %T")
root = tkinter.Tk()
root.withdraw() # hide GUI
root.after(round(1000 * (interval - timer() % interval)), check) # start on boundary
root.mainloop()
You could use supervisord, or systemd, or upstart, etc to respawn your script automatically.
See How to run a function periodically in python.