Generating multiple observers with Python watchdog Generating multiple observers with Python watchdog python python

Generating multiple observers with Python watchdog


Great question. This thread is older but I found it while looking up the exact thing and I expanded on your work and added the ability to pass in a file with a list of directories to watch. By default I do not look recursively, I leave that to someone else to test. Hopefully this helps anyone looking up the same topic. Great work!

Run using python watcher.py filename.

Where watcher.py is what I called my script and filename is the name of the file with my paths.

I list the full paths in the file and these are separated by newlines.

i.e.:

C:\path1C:\Path2\subpath1C:\PATH3

watcher.py

import loggingimport sysimport timefrom watchdog.observers import Observerfrom watchdog.events import LoggingEventHandler# Attach a logging event AKA FileSystemEventHandlerevent_handler = LoggingEventHandler()# Create Observer to watch directoriesobserver = Observer()# Take in list of paths. If none given, watch CWDpaths = open(sys.argv[1], 'r') if len(sys.argv) > 1 else '.'# Empty list of observersobservers = []# Base logging configurationlogging.basicConfig(level=logging.INFO,                    format='%(asctime)s - %(message)s',                    datefmt='%Y-%m-%d %H:%M:%S')# Iterate through paths and attach observersfor line in paths:    # Convert line into string and strip newline character    targetPath = str(line).rstrip()    # Schedules watching of a given path    observer.schedule(event_handler, targetPath)    # Add observable to list of observers    observers.append(observer)# Start observerobserver.start()try:    while True:        # Poll every second        time.sleep(1)except KeyboardInterrupt:    for o in observers:        o.unschedule_all()        # Stop observer if interrupted        o.stop()for o in observers:    # Wait until the thread terminates before exit    o.join()


The example code here shows a function called start, not start_new_thread. Have you tried that?

https://pypi.python.org/pypi/watchdog

Also, you should probably call start just once, after the for loop, not inside of it.


Just wanna add some notes:

The threading lib and threads list in the code can be a little bit confusing for people who just start using watchdog (including myself). They are actually not necessary in the solution. A simple way to explain it is just:

  • create one observer
  • schedule multiple "watching events"
  • and start the observer.

That's it.