OS.File检查OS.read之前的最后修改日期 [英] OS.File check last modified date before OS.read

查看:113
本文介绍了OS.File检查OS.read之前的最后修改日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 OS.File.read 来读取文件内容。在窗口的焦点我做 OS.File.stat 并检查它是最后修改日期。然后我记录这个日期。在窗口的下一个重点我再次 OS.File.stat 文件,如果修改日期是新的,那么我会做一个 OS.File .read 就可以了。



我想知道有没有更高效的方法?例如:我开始 OS.File.read ,它默认获得文件头应包括最后修改的日期,我检查它的读数,如果它的不是一个新的日期,我只是取消阅读。从编码的角度来看,这会更有效率,但是从性能的角度来看,可能不是那么多?解决方案

简短的回答:取决于? !

更高效的取决于文件本身,使用模式和系统工作负载。



通常情况下, OS.File.stat ,然后选择性读取可能会更有效率,因为文件I / O可能是瓶颈,你只是避免一些/ O(大部分时间)。

对于经常阅读的小文件,只能偶尔进行阅读,读取文件可能更有效。文件元数据和数据可能在OS磁盘高速缓存中,这样文件I / O变得非常快,并且实际的线程间消息开销和 OS.File的js-ctypes开销code>成为瓶颈。这是一个非常特殊的情况,但是。

另外,如果文件变化非常频繁,几乎所有的 OS.stat + check将导致不得不再次读取文件,只读取文件可能更有效率。但在这种情况下,我会认真思考,如果选择使用文件是正确的路要走,或者如果另一种沟通模式不会更好(如套接字)。

结论



所以,我会用 .stat / check / .read ,因为在磁盘缓存和系统工作负载上做假设可能是坏的。

可以避免磁盘I / O吗? h2>

然而,如果可能的话,我会避免查询文件。如果它是一个特定于您的加载项的文件,您不希望其他进程写入,那么一次读取它,然后将数据保存在共享位置,如 JS代码模块 main.js 的SDK附加组件或 bootstrap.js 自举加载项。写入后,更新缓存的数据。



如果您担心其他进程可能会同时写入文件,那么只需打开文件独占锁,不要在你的加载项运行时关闭它。

  let options = {
winShare :0 //在Windows
上独占锁定;
if(OS.Constants.libc.O_EXLOCK){
//独占锁* nix
options.unixFlags = OS.Constants.libc.O_EXLOCK;
}
let file = yield OS.File.open(...,options);

如果您希望其他程序写入文件并且正确,或者如果您使用文件的进程间通信的方式,当然,你不能避免重新读取文件,当然。

I use OS.File.read to read a files contents. On focus of Window I do OS.File.stat and check it's last modified date. I then record this date. On next focus of window I OS.File.stat the file again, and if the modified date is new, then I will do a OS.File.read on it.

I was wondering is there a more efficient way? Like for example: I start the OS.File.read, and it by default gets the file header which should include date last modified, I check that as its reading and if its not a new date than I just cancel the read. This would be more efficient from a coding perspective, but from a performance perspective maybe not so much?

解决方案

Short answer: Depends?!

What is more efficient would depend on the file itself and the usage pattern and the system workload.

Normally, OS.File.stat and then a selectively read is likely to be more efficient, because the file I/O is the likely bottleneck and you'd just avoid some I/O (most of the time).

For small files that you read often but only write occasionally, it might be more effective to just read the file. The file meta-data and data are likely in the OS disk cache so that the file I/O becomes very fast, and the actual inter-thread messaging overhead and js-ctypes overhead of OS.File become the bottlenecks. This is a very particular case, however.

Also, if the file changes extremely often, so that almost every OS.stat + check would result in having to read the file again, it might be also more efficient to just read the file. But in that case I would seriously ponder if the choice to use file was the right way to go, or if another mode of communication wouldn't be better (such as sockets).

Conclusion

So, I'd go with .stat/check/.read, because making assumptions on the disk cache and system work load might be bad.

Can disk I/O be avoided?

However, I'd avoid polling the file in the first place, if possible. If it is a file specific to your add-on that you do not expect other processes to write to, then read it once and after that keep the data in memory in a shared place, such as a JS Code Module or the main.js of SDK add-ons or bootstrap.js of bootstrapped add-ons. Upon writes, update the cached data as well.

If you're concerned that some other process might write your file in the mean time, then just open your file with an exclusive lock, and do not close it again while your add-on is running.

let options = {
  winShare: 0 // Exclusive lock on Windows
};
if (OS.Constants.libc.O_EXLOCK) {
  // Exclusive lock on *nix
  options.unixFlags = OS.Constants.libc.O_EXLOCK;
}
let file = yield OS.File.open(..., options);

If you expect other programs to write to the file and are fine with that, or if you use that file for a way of inter-process-communication in the first place, then you cannot avoid re-reading the file, of course.

这篇关于OS.File检查OS.read之前的最后修改日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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