异步监控文件

发布于 2025-01-08 20:53:33 字数 353 浏览 0 评论 0原文

在 Unix 上:我已经使用过 FAMGamin,两者似乎都提供了客户端/服务器文件监控系统。我宁愿有一个系统,我告诉内核监视一些索引节点,并且当事件发生时它会提醒我。 Inotify 起初在这方面看起来很有希望:inotify_init1 让我传递 IN_NONBLOCK 这反过来导致 poll()直接返回。不过,我知道如果我想获得有关受监控文件的消息,我必须定期调用它。现在我有点缺乏想法。

有什么东西可以异步监控文件吗?

PS:我还没有看过Windows,但我也很想得到一些关于它的答案。

On Unix: I’ve been through FAM and Gamin, and both seem to provide a client/server file monitoring system. I would rather have a system where I tell the kernel to monitor some inodes and it pokes me back when events occur. Inotify looked promising at first on that side: inotify_init1 let me pass IN_NONBLOCK which in turn caused poll() to return directly. However I understood that I would have to call it regularly if I wanted to have news about the monitored files. Now I’m a bit short of ideas.

Is there something to monitor files asynchronously?

PS: I haven’t looked on Windows yet, but I would love to have some answers about it too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伪装你 2025-01-15 20:53:33

正如 Celada 在上面的评论中所说,inotify 和 poll 是做到这一点的正确方法。

信号不是一种合理的异步编程机制——信号处理程序对于没有经验的人甚至是有经验的人来说都是非常危险的。人们不会自愿将它们用于此类目的。

相反,人们应该围绕事件循环构建程序(请参阅http://en.wikipedia.org/ wiki/Event-driven_programming 以获得整体解释)使用 poll、select 或一些类似的系统调用作为程序事件处理机制的核心。

或者,您可以使用线程或线程加事件循环。

As Celada says in the comments above, inotify and poll are the right way to do this.

Signals are not a mechanism for reasonable asynchronous programming -- and signal handlers are remarkably dangerous for the inexperienced and even for the experienced. One does not use them for such purposes voluntarily.

Instead, one should structure one's program around an event loop (see http://en.wikipedia.org/wiki/Event-driven_programming for an overall explanation) using poll, select, or some similar system call as the core of your program's event handling mechanism.

Alternatively, you can use threads, or threads plus an event loop.

游魂 2025-01-15 20:53:33

无论您的答案多么有趣,很抱歉,当问题声明“异步”时,无论隐藏的深度如何,我都无法接受基于阻塞 poll 或 select 调用的机制。

另一方面,我发现可以通过将标志 IN_NONBLOCK 传递给 inotify_init1 来异步运行 inotify。信号不会像 aio 那样被触发,并且会阻止阻塞的 read 调用会将 errno 设置为 EWOULDBLOCK 代替。

However interesting are you answers, I am sorry but I can’t accept a mechanism based on blocking calls on poll or select, when the question states “asynchronously”, regardless of how deep it is hidden.

On the other hand, I found out that one could manage to run inotify asynchronously by passing to inotify_init1 the flag IN_NONBLOCK. Signals are not triggered as they would have with aio, and a read call that would block blocking would set errno to EWOULDBLOCK instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文