gorakhargosh/watchdog — Issue #82: read-only mmap results in 'file modified' events on OS X
Author: agrif
Created: 2011-12-04 20:07:44+00:00
State: open

I'm using watchdog in combination with a library that uses `mmap` to pull data out of a watched file. Whenever the file changes, I open it using this library, pull the data out, and close it. However, doing this results in *another* file modification event, even though the library isn't changing the file. It isn't even opening the file in write mode, and the file modification time never changes.

I'm not entirely familiar with how file watching works on OSX, so maybe this is how it should be working. It's certainly not what I expected to happen.

Here are two small Python scripts that completely reproduce the problem:

~~~~{.py}
# Usage: watcher.py [file]

import sys
import time

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

class MyEventHandler(FileSystemEventHandler):
def on_modified(self, event):
print "modified:", event.src_path

obs = Observer()
obs.schedule(MyEventHandler(), sys.argv[1], recursive=False)
obs.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
obs.stop()
obs.join()
~~~~

~~~~{.py}
# Usage: reader.py [file]

import sys
import mmap

with open(sys.argv[1], "rb") as f:
m = mmap.mmap(f.fileno(), 0, mmap.MAP_SHARED, mmap.PROT_READ)
print "read byte:", repr(m[0])
m.close()
~~~~