Make this directory sync script detect change and run in the background
There's an app a library for that:
import sys
import time
import logging
from watchdog.observers import Observer
def event_handler(*args, **kwargs):
print(args, kwargs)
if __name__ == "__main__":
path = '/tmp/fun'
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
If you're running your script on linux, you can use inotify. (GitHub).
It uses a kernel feature that notifies an event when something happens in a watched directory, as file modification, access, creation, etc. This has very little overhead, as it's using epoll
system call to watch for changes.
import inotify.adapters
i = inotify.adapters.Inotify()
i.add_watch(b'/tmp')
try:
for event in i.event_gen():
if event is not None:
(header, type_names, watch_path, filename) = event
if 'IN_MODIFY' in type_names:
# Do something
sync(sourcedir, targetdir, "sync")
finally:
i.remove_watch(b'/tmp')
Also, it's recommended to use multiprocessing to execute the sync
part, unless the script will not watch for changes during the sync process. Depending on your sync
implementation, this could lead to process synchronization problems, a huge topic to discuss here.
My advice, try the easy approach, running everything on the same process and test if it suits your needs.
For Windows, you have watcher, a Python port of the .NET FileSystemWatcher API.
And for Linux, inotifyx which is a simple Python binding to the Linux inotify file system event monitoring API.