Skip to content

Commit

Permalink
lock watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Feb 16, 2024
1 parent c760c40 commit c755119
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/toolong/poll_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ def run(self) -> None:
while not self._exit_event.is_set():
successful_read = False
for fileno, watched_file in self._file_descriptors.items():
try:
position = lseek(fileno, 0, SEEK_CUR)
if chunk := read(fileno, chunk_size):
successful_read = True
breaks = scan_chunk(chunk, position)
watched_file.callback(position + len(chunk), breaks)
position += len(chunk)
except Exception as error:
watched_file.error_callback(error)
self._file_descriptors.pop(fileno, None)
break
with watched_file._lock:
try:
position = lseek(fileno, 0, SEEK_CUR)
if chunk := read(fileno, chunk_size):
successful_read = True
breaks = scan_chunk(chunk, position)
watched_file.callback(position + len(chunk), breaks)
position += len(chunk)
except Exception as error:
watched_file.error_callback(error)
self._file_descriptors.pop(fileno, None)
break
else:
if not successful_read:
time.sleep(0.05)

0 comments on commit c755119

Please sign in to comment.