hinotify icon indicating copy to clipboard operation
hinotify copied to clipboard

New API

Open kolmodin opened this issue 13 years ago • 9 comments

Thinking of a new API for version 0.4.

The current callback based API has several weaknesses, including silently dropping exceptions from the user's callback code which several users has noticed.

A new API could look something like this:

inotify :: IO INotify
addInotify :: INotify -> [EventVariety] -> FilePath -> IO WatchDescriptor
readEvents :: INotify -> IO [Event]

It'd be without background threads, without channels, thus much simpler. Event overflow would be left to the linux kernel.

kolmodin avatar Nov 06 '12 19:11 kolmodin

The problem is this requires polling in the client application. STM based queues would give you the best of both worlds (although adds more dependencies).

ChristopherKing42 avatar Dec 09 '15 01:12 ChristopherKing42

The application can solve it in whatever way it wishes if we provide the building blocks.

kolmodin avatar Dec 18 '15 15:12 kolmodin

One other building block I would add is waitForEvent :: INotify -> IO Event. This way instead of an application repeatedly calling readEvent waiting for a new Event to pop up (which would both consume unnecessary resources and be slower) they could just call waitForEvent to, well, wait for an Event.

(Actually, if we want to go for minimalism, all you need is waitForEvent :: INotify -> IO Event and noBlockEvent :: INotify -> Maybe Event (and define readEvents in terms of it) (and you need inotify and addInotify of course).)

A lot of different concurrency mechanisms (Async, STM, etc...) would benefit from this addition.

ChristopherKing42 avatar Dec 19 '15 02:12 ChristopherKing42

It wouldn't be efficient to only read a single event. For performance reasons they should be read in batch. It's also essential to wait until there is enough events to read. More details here; http://www.serpentine.com/blog/2008/01/04/why-you-should-not-use-pyinotify/

kolmodin avatar Dec 20 '15 18:12 kolmodin

Well, some sort of blocking call would be useful. Maybe waitFotEvents :: INotify -> n -> [Event]? Or have a cache of events, and waitEvent just takes off the top event (if available).

ChristopherKing42 avatar Dec 20 '15 19:12 ChristopherKing42

Looks like you're describing readEvents in the top post.

kolmodin avatar Dec 20 '15 21:12 kolmodin

Does readEvents block until events are ready, or does it return the empty list if there are no events?

ChristopherKing42 avatar Dec 22 '15 01:12 ChristopherKing42

At least on Linux it'd block until something is available, and then return that. It won't be a lot, which is why there should be a threshold before reading.

kolmodin avatar Dec 24 '15 08:12 kolmodin

Ah, okay. (Then I would recommend making a nonblocking version of readEvents. Note that readEventsNoBlock would still wait for thresholds and such, it just that if there aren't events (either because there really are no events, or because we do not want to check for them yet), it yields [].) It would be more efficient to put this in the library I think than to make the user check for it.)

ChristopherKing42 avatar Dec 24 '15 16:12 ChristopherKing42