pyinotify: Handling IN_MODIFY triggers
I am trying to watch a directory, and is looking for file modifications. Thinking of using pyinotify. Problem is that while using IN_MODIFY event to check for a file change, it triggers quite a number of ev开发者_如何学Goents if I am copying even a small file of say 12 MB to the directory over a network.
I dont want to handle so many triggers. I want to only trigger a single event, after the file is copied. How do I achieve that?
Any Pyinotify gurus can help
Try changing IN_MODIFY
to IN_CLOSE_WRITE
.
An IN_CLOSE_WRITE
event occurs when a writable file is closed. That should happen only once, unless the program that is copying the file chooses to close the file multiple times.
The above change is probably all you need, but if not, this basic code can be a very useful tool for seeing what events occur when. With it, you should be able to determine what event to use.
# Example: loops monitoring events forever.
#
import pyinotify
# Instanciate a new WatchManager (will be used to store watches).
wm = pyinotify.WatchManager()
# Associate this WatchManager with a Notifier (will be used to report and
# process events).
notifier = pyinotify.Notifier(wm)
# Add a new watch on /tmp for ALL_EVENTS.
wm.add_watch('/tmp', pyinotify.ALL_EVENTS)
# Loop forever and handle events.
notifier.loop()
精彩评论