Open only recently changed files to conserve file descriptors

This commit is contained in:
Peter J. Holzer 2023-04-13 00:11:00 +02:00
parent 411939468b
commit 09357a65dc
1 changed files with 34 additions and 24 deletions

12
kitsune
View File

@ -12,6 +12,11 @@ class WatchedFile:
def __init__(self, path, seek_to_end):
self.path = path
self.seek_to_end = seek_to_end
stf = os.stat(self.path)
self.st_ctime_ns = stf.st_ctime_ns
self.st_size = stf.st_size
self.fileno = None
if stf.st_ctime_ns >= time.time_ns() - 1E9:
self.reopen()
def reopen(self):
@ -19,7 +24,7 @@ class WatchedFile:
self.fileno = self.fd.fileno()
self.last_ts = 0
if self.seek_to_end:
self.fd.seek(0, 2)
self.fd.seek(self.st_size, 0)
self.seek_to_end = False
def format_ts(ts_ns):
@ -59,6 +64,7 @@ def watch(args):
# has any of the files changed
for f in watched_files.values():
# XXX - we should also detect replaced files.
if f.fileno:
st = os.stat(f.fileno)
try:
stf = os.stat(f.path)
@ -83,6 +89,10 @@ def watch(args):
lines.pop()
for ln in lines:
print(f"{f.path:{filename_length}}", format_ts(f.last_ts), dead, ln)
else:
stf = os.stat(f.path)
if stf.st_ctime_ns != f.st_ctime_ns:
f.reopen()
time.sleep(0.1)
seek_to_end = False