A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
Limit the number of threads waiting for files to be ready during polling
Trenton H4a5f21dd8769
diff --git a/src/documents/management/commands/document_consumer.py b/src/documents/management/commands/document_consumer.pyindex c3f6bbed4..d4ace3f1b 100644--- a/src/documents/management/commands/document_consumer.py+++ b/src/documents/management/commands/document_consumer.py@@ -1,10 +1,10 @@import loggingimport os+from concurrent.futures import ThreadPoolExecutorfrom fnmatch import filterfrom pathlib import Pathfrom pathlib import PurePathfrom threading import Event-from threading import Threadfrom time import monotonicfrom time import sleepfrom typing import Final@@ -168,11 +168,15 @@ def _consume_wait_unmodified(file: str) -> None:class Handler(FileSystemEventHandler):+ def __init__(self, pool: ThreadPoolExecutor) -> None:+ super().__init__()+ self._pool = pool+def on_created(self, event):- Thread(target=_consume_wait_unmodified, args=(event.src_path,)).start()+ self._pool.submit(_consume_wait_unmodified, event.src_path)def on_moved(self, event):- Thread(target=_consume_wait_unmodified, args=(event.dest_path,)).start()+ self._pool.submit(_consume_wait_unmodified, event.dest_path)class Command(BaseCommand):@@ -246,17 +250,18 @@ class Command(BaseCommand):timeout = self.testing_timeout_slogger.debug(f"Configuring timeout to {timeout}s")- observer = PollingObserver(timeout=settings.CONSUMER_POLLING)- observer.schedule(Handler(), directory, recursive=recursive)- observer.start()- try:- while observer.is_alive():- observer.join(timeout)- if self.stop_flag.is_set():- observer.stop()- except KeyboardInterrupt:- observer.stop()- observer.join()+ with ThreadPoolExecutor(max_workers=4) as pool:+ observer = PollingObserver(timeout=settings.CONSUMER_POLLING)+ observer.schedule(Handler(pool), directory, recursive=recursive)+ observer.start()+ try:+ while observer.is_alive():+ observer.join(timeout)+ if self.stop_flag.is_set():+ observer.stop()+ except KeyboardInterrupt:+ observer.stop()+ observer.join()def handle_inotify(self, directory, recursive, is_testing: bool):logger.info(f"Using inotify to watch directory for changes: {directory}")