Using watchdog of python to monitoring afp shared folder from linux

放肆的年华 提交于 2020-04-10 04:14:45

问题


I want linux machine(Raspberry pi) to monitor a shared folder by AFP(Apple file protocol, macbook is host).

I can mount shared folder by mount_afp, and installed watchdog python library to monitor a shared folder. The problem is that watchdog can monitor only modifications from linux machine itself.

If a monitoring folder has been modified by the host machine(Apple macbook) or other pc, linux machine couldn't find out the change. No logs came out.

After I tested the same watchdog python file in host machine(Apple macbook), I can get every logs of modifications by other machine.

Host machine can get every modifications of file or folder. But other machine(guest machine) can not monitor modifications of file from host or others.

Is it normal status of watchdog? Or is there any problem in account and permission?

Here is watchdog sample.

  import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler


class Watcher:
    DIRECTORY_TO_WATCH = "/path/to/my/directory"

    def __init__(self):
        self.observer = Observer()

    def run(self):
        event_handler = Handler()
        self.observer.schedule(event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
        self.observer.start()
        try:
            while True:
                time.sleep(5)
        except:
            self.observer.stop()
            print "Error"

        self.observer.join()


class Handler(FileSystemEventHandler):

    @staticmethod
    def on_any_event(event):
        if event.is_directory:
            return None

        elif event.event_type == 'created':
            # Take any action here when a file is first created.
            print "Received created event - %s." % event.src_path

        elif event.event_type == 'modified':
            # Taken any action here when a file is modified.
            print "Received modified event - %s." % event.src_path


if __name__ == '__main__':
    w = Watcher()
    w.run()

回答1:


For network mounts, the usual filesystem events don't always get emitted. In such cases, instead of using Observer, try using PollingObserver -- i.e., change from:

   self.observer = Observer()

to

   from watchdog.observers.polling import PollingObserver
   ...
   self.observer = PollingObserver()

There is also a PollingObserverVFS class you could experiment with.

Documentation: https://pythonhosted.org/watchdog/api.html#module-watchdog.observers.polling



来源:https://stackoverflow.com/questions/45441623/using-watchdog-of-python-to-monitoring-afp-shared-folder-from-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!