Watchdog Python Event handling takes longer than event creation interval

66 views Asked by At

I would like to understand better how does the Watchdog event handler works. I am monitoring a folder where images are uploaded every 15s, I want to evaluate each image and this evaluation may take longer than 15s. Therefore the events (image is uploaded) need to be put in a FIFO queue and processed in an asynchronous way. Do the Watchdog classes and methods already deal with this automatically or should I use Watchdog just to populate a queue and then process the queue?

Below an example of my code:

from watchdog.observers import Observer
from watchdog.observers.polling import PollingObserver
from watchdog.events import FileSystemEventHandler

def evaluate_image(image_path, *args, **kwargs):
    # I omitted the evaluate_image code
 

class FileHandler(FileSystemEventHandler):
    def __init__(self,*args, **kwargs):
        super().__init__()
 
    def on_created(self, event):
        # Process the new file if it is not a directory
        if not event.is_directory:
            evaluate_image(event.src_path, *args, **kwargs)

if __name__ == '__main__':
    observer = PollingObserver()
    event_handler = FileHandler(*args, **kwargs)
    observer.schedule(event_handler, folder_to_watch, recursive=True)

    # Start the observer
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        # Stop the observer when Ctrl+C is pressed
        observer.stop()

    # Wait for the observer to gracefully exit
    observer.join()
0

There are 0 answers