1

I have some basic questions about pyinotify that I can't seem to find the answers to elsewhere.

1) For a continuous directory monitor (and event processor) is it necessary to have a while( True ) loop or is the continuous event 'loop' handled by the notify watch and ended when I remove the watch?

2) What happens if files are pre-existing when the inotify instance is 'turned-on'? Initially I just want to monitor for IN_CREATE but this won't handle pre-existing files.

3) Similar to #2, what happens if a file gets created while I'm in my event processor function? Will pyinotify cache it in its queue and process it when the 'loop' starts again, or will I lose this event?

sadmicrowave
  • 39,964
  • 34
  • 108
  • 180

1 Answers1

2
  • is it necessary to have a while( True ) loop

You'll need a while-loop, but it can be set up implicitly by calling the notifier.loop method:

    wm = pyinotify.WatchManager()
    mask = pyinotify.IN_CREATE
    notifier = pyinotify.Notifier(wm, MyProcessEvent(path))
    wdd = wm.add_watch(path, mask, rec=True, auto_add=True)
    notifier.loop()

If you wish to set up the while-loop yourself, you might start with this source code from notifier.loop:

    while 1:
        try:
            notifier.process_events()
            # check_events is blocking
            if notifier.check_events():
                notifier.read_events()
        except KeyboardInterrupt:
            # Stop monitoring if sigint is caught (Control-C).
            break
    notifier.stop()

To remove a watch of a particular file or directory, call wm.rm_watch.

  • What happens if files are pre-existing when the inotify instance is 'turned-on'?

    No event is generated before wm.add_watch is called.

  • what happens if a file gets created while I'm in my event processor function?

    The events are queued in a buffer of size /proc/sys/fs/inotify/max_queued_events. For example, on my system that number is

    % cat /proc/sys/fs/inotify/max_queued_events
    16384
    

    If the filesystem generates enough events to fill up the buffer while you are processing a prior event, then you get a IN_Q_OVERFLOW event.

    See the comment in blucz's answer.

Community
  • 1
  • 1
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • so the only time I need to account for files that pyinotify won't find is when the program starts and the directory (potentially) has files already. I will need a directory iterator function to manually handle these, then intro into the pyinotify instance.... – sadmicrowave Dec 19 '11 at 15:04
  • @unutbu - alternatively I found that I can implement a asyncore.loop() with pyinotify to create the infinite while loop, docs found here: https://github.com/seb-m/pyinotify/blob/master/python2/examples/tutorial_asyncnotifier.py – sadmicrowave Dec 27 '11 at 18:20
  • Oh! I hadn't known about that. Thanks for sharing. – unutbu Dec 27 '11 at 18:53
  • `notifier`, the instance of `pyinotify.Notifier` has a method `loop` that takes care of the looping so you don't have to write a `while True: …`. See [the example here.](https://github.com/seb-m/pyinotify/blob/master/python2/examples/loop.py) – dmmfll Jan 24 '16 at 22:03