关闭文件描述符并删除 inotify watch 真的有必要吗?
使用 python inotifyx,如果我需要它们直到程序退出,我是否必须删除监视并关闭打开的系统文件描述符?例如,如果我在每次运行时创建一个(文件描述符+监视)并且不关闭它,是否存在一些可能的问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每当您使用完资源(例如释放内存、关闭文件描述符、子进程上的 waitpid(2) 等)时,释放它们总是一个好主意。偷懒并在退出时让操作系统为您处理这件事肯定会在将来导致错误。
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.
内核将监视存储为完整路径,因此最好关闭监视,这也会减少 VFS 的不必要工作。至于文件描述符,这取决于您打开了多少个其他文件描述符。
有点像打电话,很高兴告诉对方你已经停止听了,挂断电话是可选的,但很常规。如果您需要它来做某事,请保留它。
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.