Using watchdog of python to monitoring afp shared folder from linux

2024/9/8 10:55:12

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 FileSystemEventHandlerclass 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):@staticmethoddef on_any_event(event):if event.is_directory:return Noneelif event.event_type == 'created':# Take any action here when a file is first created.print "Received created event - %s." % event.src_pathelif event.event_type == 'modified':# Taken any action here when a file is modified.print "Received modified event - %s." % event.src_pathif __name__ == '__main__':w = Watcher()w.run()
Answer

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://en.xdnf.cn/q/73320.html

Related Q&A

Fitting curve: why small numbers are better?

I spent some time these days on a problem. I have a set of data:y = f(t), where y is very small concentration (10^-7), and t is in second. t varies from 0 to around 12000.The measurements follow an est…

Fast numpy roll

I have a 2d numpy array and I want to roll each row in an incremental fashion. I am using np.roll in a for loop to do so. But since I am calling this thousands of times, my code is really slow. Can you…

IndexError: fail to coerce slice entry of type tensorvariable to integer

I run "ipython debugf.py" and it gave me error message as belowIndexError Traceback (most recent call last) /home/ml/debugf.py in <module>() 8 fff = …

How to detect lines in noisy line images?

I generate noisy images with certain lines in them, like this one:Im trying to detect the lines using OpenCV, but something is going wrong.Heres my code so far, including the code to generate the noisy…

How can I connect a StringVar to a Text widget in Python/Tkinter?

Basically, I want the body of a Text widget to change when a StringVar does.

python csv writer is adding quotes when not needed

I am having issues with writing json objects to a file using csv writer, the json objects seem to have multiple double quotes around them thus causing the json objects to become invalid, here is the re…

How to install google.cloud automl_v1beta1 for python using anaconda?

Google Cloud AutoML has python example code for detection, but I have error when importing these modulesfrom google.cloud import automl_v1beta1 from google.cloud.automl_v1beta1.proto import service_pb2…

Python3.8 - FastAPI and Serverless (AWS Lambda) - Unable to process files sent to api endpoint

Ive been using FastAPI with Serverless through AWS Lambda functions for a couple of months now and it works perfectly.Im creating a new api endpoint which requires one file to be sent.It works perfectl…

How to create a function for recursively generating iterating functions

I currently have a bit of Python code that looks like this:for set_k in data:for tup_j in set_k:for tup_l in tup_j:The problem is, Id like the number of nested for statements to differ based on user in…

How to get molecular weight of a compound in python?

User inputs a formula, for example: C12H2COOHWe have to calculate its molecular weight given that C = 12.01, H = 1.008 and O = 16. We were told to be careful of elements with double digits after it and…