I am using Watchdog to monitor a directory and keep it in sync with Dropbox.
I am facing a situation where every time I download a file from Dropbox, I trigger an upload event as I need to write to the directory Watchdog is monitoring. This is the code I am using.
event_handler = UploadHandler.UploadHandler()
observer = Observer()
observer.schedule(event_handler, path=APP_PATH, recursive=True)
observer.start()
try:
while True:
# Apply download here
time.sleep(20)
except KeyboardInterrupt:
observer.stop()
observer.join()
Is there a way to "pause" the observer while I apply the download and "unpause" it again when I'm done?
I needed pausing functionality so I'm using the following observer:
I can then pause the observer directly with the
pause()
andresume()
methods, but my primary use case is for when I just want to ignore any events caused by writing to a directory I'm watching, for which I use the context manager:You can test this out by saving both code blocks in a file, running it, and adding/editing/removing files in the created 'watchdir' directory. The timestamps of your modifications will be appended to 'watchdir/modifications.log'.
This functionality appears to be built into pyinotify, but that library only works in linux and watchdog is OS-independent.