Node.JS:“fs.watchFile”工作? [英] Node.JS: How does "fs.watchFile" work?

查看:447
本文介绍了Node.JS:“fs.watchFile”工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Node 0.4.3的 API文档

a>, fs.watchFile(filename,[options],listener)函数启动一个例程,它将会


监视 filename 的更改。每次访问文件时,都会调用 callback 监听器。

也说


如果提供选项应该是一个包含两个成员的对象boolean ,持久间隔,以毫秒为单位的轮询值

这表明它会根据间隔时间来检查每一个。但它也表示:
$ b


默认是 {persistent:true,interval:0}

这是否意味着它将检查每个毫秒来查看文件时间是否改变?它是否监听操作系统级别的事件?这是如何工作的?

解决方案

是的,cpedros是正确的,这似乎是重复的。我想我可以更多地了解这一点。



每个操作系统都有自己的文件更改事件被触发。在Linux上,它是inotify(以前是dnotify),在Mac上是fsevents,在Windows上是FileSystemWatcher。我不确定底层代码是否处理每个案例,但是这是一般的想法。



如果您只想在Linux上观看文件,我推荐 node-inotify-plus-plus 。如果您要观看目录,请使用带有节点行程的inotify-plus-plus。我已经实现了这一点,它像一个魅力。



如果你有兴趣,我可以发布一些代码。 node-inotify-plus-plus背后的美妙之处在于,它吸收了inotify的大部分缺陷,并提供了一个直观的API来监听文件上的特定事件。

编辑:这不应该被用来观看大量的文件。在我的系统上,最大值是8192.你可以使用这个命令找到最大值: cat / proc / sys / fs / inotify / max_user_watches 。这可以用来只是看目录的变化,然后从那里找出单个文件。编辑:感谢@guiomie指出现在完全支持在Windows上观看文件。修改后的事件将触发,如果直接在该目录下的文件被修改。

我认为这是在v0.6.x版本。


According to the API docs for Node 0.4.3, the fs.watchFile(filename, [options], listener) function starts a routine that will

Watch for changes on filename. The callback listener will be called each time the file is accessed.

It also says

The options if provided should be an object containing two members a boolean, persistent, and interval, a polling value in milliseconds

Which indicates that it will check every so often based on what is in interval. But it also says

The default is { persistent: true, interval: 0 }.

So does that mean it will check every millisecond to see if the file time changed? Does it listen for OS level events? How does that work?

解决方案

Yes, cpedros is correct, this does seem to be a duplicate. I think I can shed some more light on this though.

Each OS has its own file change event that gets fired. On Linux, it is inotify (used to be dnotify), on Mac it is fsevents, and on Windows it is FileSystemWatcher. I'm not sure if the underlying code handles each case, but that's the general Idea.

If you just want to watch a file on Linux, I recommend node-inotify-plus-plus. If you want to watch a directory, use inotify-plus-plus with node-walk. I've implemented this and it worked like a charm.

I can post some code if you're interested. The beauty behind node-inotify-plus-plus is that it abstracts much of the nastiness of inotify and gives an intuitive API for listening to specific events on a file.

EDIT: This shouldn't be used to watch tons of files. On my system, the max is 8192. Your max can be found by using this command cat /proc/sys/fs/inotify/max_user_watches. This could be used to just watch directories for changes and then figure out the individual files from there. A modified event will fire if a file directly under that directory is modified.

EDIT: Thanks @guiomie for pointing out that watching files is now fully supported on Windows. I assume this is with the v0.6.x release.

这篇关于Node.JS:“fs.watchFile”工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆