views:

174

answers:

2

With python inotifyx, do I have to remove watch and close opened system file descriptor if I need them until program exit? E.g. is there some possible problems if I create one (file descriptor + watch) with each run and don't close it?

A: 

The kernel stores watches as full paths, so closing the watch is preferable, it also takes unnecessary work off of VFS. As for the file descriptor, that would depend on how many others you had opened.

Kind of like a phone call, its nice to tell the other party that you have stopped listening, hanging up the phone is optional, but conventional. If you need it for something, keep it.

Tim Post
But shouldn't linux do that automatically when the process exits, anyway? Or there is noticeable chance of leaving some extra trace in OS by not removing the inotify watch?
@hell.orts.ru: Yes, Linux will do that when the process _terminates_, from the way your question is worded, it sounds like you no longer need the watch but want to keep the file descriptor. In that case, yes, remove the watch and keep the FD.
Tim Post
A: 

It's always a good idea to release resources (e.g. free memory, close file descriptors, waitpid(2) on child processes, etc) whenever you're done using them. Being lazy and letting the operating system take care of it for you when you exit is a sure way to cause bugs in the future.

Zach Hirsch
As far as I understand those “bugs in the future” would mean pretty serious bugs in Linux kernel or in the Python. Or I'm missing something?
Well, they could be, but that's unlikely. More likely is that you'll modify your code in some way so that you wind up leaking resources. For example, refactoring a chunk of code into a function and calling it multiple times---if that new function leaks a file descriptor, then that's a problem.
Zach Hirsch