实时监控文件更改 [英] monitoring for changes in file(s) in real time

查看:89
本文介绍了实时监控文件更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,可以监视某些文件的更改.文件更新后,即会对其进行处理.到目前为止,我已经提出了在R中处理实时分析"的一般方法.我希望你们还有其他方法.也许我们可以讨论它们的优缺点.

I have a program that monitors certain files for change. As soon as the file gets updated, the file is processed. So far I've come up with this general approach of handing "real time analysis" in R. I was hoping you guys have other approaches. Maybe we can discuss their advantages/disadvantages.

monitor <- TRUE
start.state <- file.info$mtime # modification time of the file when initiating

while(monitor) {
  change.state <- file.info$mtime
  if(start.state < change.state) {
    #process
  } else {
    print("Nothing new.")
  }
  Sys.sleep(sleep.time)
}

推荐答案

类似于使用系统API的建议,也可以使用

Similar to the suggestion to use a system API, this can be also done using qtbase which will be a cross-platform means from within R:

dir_to_watch <- "/tmp"

library(qtbase)
fsw <- Qt$QFileSystemWatcher()
fsw$addPath(dir_to_watch)

id <- qconnect(fsw, "directoryChanged", function(path) {
  message(sprintf("directory %s has changed", path))
})

cat("abc", file="/tmp/deleteme.txt")

这篇关于实时监控文件更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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