Q:

Reliability of inotifywait loop

I have of bash script with a loop watching a directory recursively:
while true
do
    if path=`inotifywait -q -r -e create --format %w%f $watchpath`; then
        #modify file
    fi
done

If I'm not mistaken this comes with this problem:
If many files are created in that directory or the machine is busy with other tasks, a file could be created before inotifywait is reached again - which would mean that it would be ignored. 
Is there a way to mitigate that?  Perhaps there is way to continuously "watch" and process a stream/feed of modfied files instead? 

A:

One way is to run inotifywait in monitor mode, e.g:
inotifywait -m -q -r -e create --format '%w%f' "$watchpath" |
    while read -r path; do
        : # do something with path
    done

There still will be a race between processes though, I'm not sure there is a way to avoid race conditions using shell utilities. 
Even the man page lists this under caveats. 

The inotify API identifies affected files by filename.   However, by
         the time an application processes an inotify event, the filename may
         already have been deleted or renamed.