Source

python-inotify / examples / performant.py

# A somewhat more performant example of how to use the inotify
# subsystem.

# In this example, we delay reading from the inotify file descriptor
# until either it has enough events queued to be worth reading, or a
# timeout passes.

# This greatly reduces the number of read() system calls we issue when
# monitoring a busy filesystem, and substantially reduces CPU
# overhead.  This also has the nice feature of delaying work for a few
# moments, which will in many cases put it off until the system would
# otherwise be idle.

from inotify import watcher
import inotify
import select
import sys

w = watcher.AutoWatcher()

paths = sys.argv[1:] or ['/tmp']

for path in paths:
    try:
        # Watch all paths recursively, and all events on them.
        w.add_all(path, inotify.IN_ALL_EVENTS)
    except OSError, err:
        print >> sys.stderr, '%s: %s' % (err.filename, err.strerror)

# If we have nothing to watch, don't go into the read loop, or we'll
# sit there forever.

if not len(w):
    sys.exit(1)

poll = select.poll()
poll.register(w, select.POLLIN)

timeout = None

threshold = watcher.Threshold(w, 512)

while True:
    events = poll.poll(timeout)
    nread = 0
    if threshold() or not events:
        print 'reading,', threshold.readable(), 'bytes available'
        for evt in w.read(0):
            nread += 1

            # The last thing to do to improve efficiency here would be
            # to coalesce similar events before passing them up to a
            # higher level.

            # For example, it's overwhelmingly common to have a stream
            # of inotify events contain a creation, followed by
            # multiple modifications of the created file.

            # Recognising this pattern (and others) and coalescing
            # these events into a single creation event would reduce
            # the number of trips into our app's presumably more
            # computationally expensive upper layers.

            print repr(evt.fullpath), ' | '.join(inotify.decode_mask(evt.mask))
    if nread:
        print 'plugging back in'
        timeout = None
        poll.register(w, select.POLLIN)
    else:
        print 'unplugging,', threshold.readable(), 'bytes available'
        timeout = 1000
        poll.unregister(w)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.