简体   繁体   English

异步监视文件

[英]Monitoring files asynchronously

On Unix: I've been through FAM and Gamin , and both seem to provide a client/server file monitoring system. 在Unix上:我已经使用过FAMGamin ,并且两者似乎都提供了客户端/服务器文件监视系统。 I would rather have a system where I tell the kernel to monitor some inodes and it pokes me back when events occur. 我宁愿有一个系统,告诉内核监视某些inode并在事件发生时回叫我。 Inotify looked promising at first on that side: inotify_init1 let me pass IN_NONBLOCK which in turn caused poll() to return directly. 首先, Inotify在那方面看起来很有希望: inotify_init1让我传递IN_NONBLOCK ,这又导致poll()直接返回。 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. PS:我还没有在Windows上看过,但是我也想对此有一些答案。

As Celada says in the comments above, inotify and poll are the right way to do this. 正如Celada在上述评论中所言,进行inotify和poll是正确的方法。

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. 取而代之的是,应该使用轮询,选择或类似的系统调用作为程序事件的核心,围绕事件循环构建程序(有关总体说明,请参见http://en.wikipedia.org/wiki/Event-driven_programming )。处理机制。

Alternatively, you can use threads, or threads plus an event loop. 或者,您可以使用线程,也可以使用线程加事件循环。

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 . 另一方面,我发现可以通过将标志IN_NONBLOCK传递给inotify_init1来异步运行inotify。 Signals are not triggered as they would have with aio , and a read call that would block blocking would set errno to EWOULDBLOCK instead. 信号不会像使用aio那样被触发,并且阻止阻塞的read调用会将errnoEWOULDBLOCK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM