我必须运行一个更新某些数据的长循环并将其存储在我公司的服务器中。问题是该公司在午夜运行备份程序,为此,他们关闭服务器大约15分钟。
因此,鉴于我必须为每次迭代写下一个文件,当服务器关闭时它会打破循环。
我设法通过编写如下循环来解决问题
for(i in bills.list){
url = paste0("ulalah",i,"/")
# Download the data
bill.result <- try(getURL(url)) # if there is an error try again
while(class(bill.result)=="try-error"){
Sys.sleep(1)
bill.result <- try(getURL(url))
}
# if iteration is between 23:59:00 and 23:59:40 wait 17 min to restart the loop
if(as.numeric(format(Sys.time(), "%H%M%S")) > 235900 &
as.numeric(format(Sys.time(), "%H%M%S")) < 235940){
Sys.sleep(1020)
}
# Write the page to local hard drive
write(bill.result, paste0("bill", i, ".txt"))
# Print progress of download
cat(i, "\n")
}
问题是,通过评估所有迭代的时间,我会失去一些宝贵的时间。还有更有效的想法吗?
答案 0 :(得分:1)
我认为你可以简单地尝试存储日期。如果失败,则可能是您在备份窗口内
store <- function(data, retry = 10) {
while(retry > 0) {
result <- try(write(data, "/some_broken_place"))
if(class(result) == "try-error") {
# it might be we are in the backup window
cat("I will sleep if it turns out that it's backup time")
if(as.numeric(format(Sys.time(), "%H%M%S")) > 235900 &
as.numeric(format(Sys.time(), "%H%M%S")) < 235940){
Sys.sleep(1020)
}
retry <- retry - 1
}
}
if(retry == 0) {
cat("Very, very bad situation - no chance to store data")
}
}