1

I'm a novice with Python & I'm trying to automate synchronization with my home server with unison. I discovered watchdog & I'm trying to use it, but whenever I run "touch test.txt", the script continuously launches new unison processes. Since the observer is using classes inherited from Queue, I assumed that it should just block after it pops the first event off the top of the queue. Have I overlooked something here?

Code:

#!/usr/bin/python
import sys
import subprocess
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class ChangeHandler(FileSystemEventHandler):
   def on_any_event(self, event):
      subprocess.call(["/usr/bin/unison", "-batch", "-silent", "-ui", "text",
         "default"])

if __name__ == "__main__":
   observer = Observer()
   observer.schedule(ChangeHandler(), '/home/philip', True)
   observer.start()
   try:
      while True:
         time.sleep(1)
   except KeyboardInterrupt:
      observer.stop()
   observer.join()

Output:

props    <-?-> props      /  props    <-?-> props      Documents  props    <-?-> props      Downloads  props    <-?-> props      /  props    <-?-> props      Documents  props    <-?-> props      Downloads  props    <-?-> props      /  props    <-?-> props      Documents  props    <-?-> props      Downloads  props    <-?-> props      /  props    <-?-> props      Documents  props    <-?-> props      Downloads  props    <-?-> props      /  props    <-?-> props      Documents  props    <-?-> props      Downloads  props    <-?-> props      /  props    <-?-> props      Documents  props    <-?-> props      Downloads
  • while True: time.sleep(1) looks like this will go on until the time itself comes to an end... – Jingo Sep 03 '12 at 20:20
  • @Jingo or, more likely, until the user presses a 'Ctrl+C' and causes a `KeyboardInterrupt`... – Pierre GM Sep 03 '12 at 21:36
  • 1
    It took working with pyinotify to finally understand that I'm synchronizing a directory while simultaneously watching that same directory for filesystem events. Of course, i'll get multiple calls unless I suspend the Observer/Notifier when processing an event. – Philip Hoffman Sep 04 '12 at 04:52

1 Answers1

1

OP sufficiently answered this in a comment:

It took working with pyinotify to finally understand that I'm synchronizing a directory while simultaneously watching that same directory for filesystem events. Of course, i'll get multiple calls unless I suspend the Observer/Notifier when processing an event.

Mike Pierce
  • 1,390
  • 1
  • 12
  • 35