简体   繁体   中英

Python Watchdog: Why is my subprocess called continuously?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM