【问题标题】:How to retry for loop when timeout error occurs: Where to add trycatch or withTimeout发生超时错误时如何重试for循环:在哪里添加trycatch或withTimeout
【发布时间】:2021-02-03 03:03:27
【问题描述】:

我正在运行一个 for 循环来尝试查看并从我的 aws s3 帐户中提取信息。由于某种原因,当我运行 for 循环时,循环将在整个循环中随机停止。当我说随机时,每次运行循环时,循环都会在不同的位置停止。我得到的是与进程超时有关的错误。如果循环每次都停在同一个桶上,那么我想知道特定桶是否有问题。我注意到当我手动运行命令来列出存储桶中的文件时,有时会出现超时错误。如果我运行相同的命令,比如 3 次,可能会出现 3 次中的 2 次超时错误,但第 3 次会通过。我想知道是否可以在循环中添加 trycatch 或 withTimeout,但我正在努力弄清楚如何将它们实现到我的 for 循环中。当我尝试添加它们时,似乎在我收到超时错误之前仍然只有一次尝试。这是我要运行的循环

files <- NULL
#sample.id <- NULL
for(i in 1:length(sample.id.path2[[1]])){
  a <- as.data.frame(system(paste0("mcli ls s3/path/to/bucket/", sample.id.path2[['V2']][i], sep = ""), intern=TRUE))
  colnames(a)[1] <- "V1"
  a$V1 <- gsub("^.{0,34}", "", a$V1)
  a <- a %>% filter(str_detect(V1, 'fastq'))
  #print(a)
  b <- as.data.frame(paste0("", sample.id.path2[['V2']][i],  sep = ""))
  colnames(b)[1] <- "V1"
  c <- as.data.frame(paste0(b$V1, a$V1,  sep = ""))
  colnames(c)[1] <- "V2"
  d <- cbind(a,c)
  print(d)
  files <- rbind(files, d)
}

这是我尝试运行的一些示例

files <- NULL
#sample.id <- NULL
for(i in 1:length(sample.id.path2[[1]])){
  a <- withTimeout(as.data.frame(system(paste0("mcli ls s3/path/to/bucket/", sample.id.path2[['V2']][i], sep = ""), intern=TRUE)), timeout = 100, cpu = 100, elapsed = 100, onTimeout = "error")
  colnames(a)[1] <- "V1"
  a$V1 <- gsub("^.{0,34}", "", a$V1)
  a <- a %>% filter(str_detect(V1, 'fastq'))
  #print(a)
  b <- as.data.frame(paste0("", sample.id.path2[['V2']][i],  sep = ""))
  colnames(b)[1] <- "V1"
  c <- as.data.frame(paste0(b$V1, a$V1,  sep = ""))
  colnames(c)[1] <- "V2"
  d <- cbind(a,c)
  print(d)
  files <- rbind(files, d)
}

还有一个

files <- NULL
#sample.id <- NULL
for(i in 1:length(sample.id.path2[[1]])){
  while(TRUE){
  a <- try(as.data.frame(system(paste0("mcli ls s3/path/to/bucket/", sample.id.path2[['V2']][i], sep = ""), intern=TRUE)), silent=TRUE)
  if(!is(df, 'try-error')) break
  colnames(a)[1] <- "V1"
  a$V1 <- gsub("^.{0,34}", "", a$V1)
  a <- a %>% filter(str_detect(V1, 'fastq'))
  }
  #print(a)
  b <- as.data.frame(paste0("", sample.id.path2[['V2']][i],  sep = ""))
  colnames(b)[1] <- "V1"
  c <- as.data.frame(paste0(b$V1, a$V1,  sep = ""))
  colnames(c)[1] <- "V2"
  d <- cbind(a,c)
  print(d)
  files <- rbind(files, d)
}

我觉得有一个简单的解决方案,但我正在努力找出我需要在循环中添加 trycatch 或 withTimeout 的确切位置

【问题讨论】:

  • 不知道有没有办法在系统命令中添加until?或类似的东西
  • @DaveArmstrong 哇.. 那是正确的。这解决了问题。我不知道我错过了。如果您想将其放入答案中,我会接受它..这样您就可以获得更多的街头信誉。我想出了另一个解决方案,它更多地涉及系统命令而不是 R。我将发布它以帮助其他任何人在这篇文章中的树桩

标签: r loops


【解决方案1】:

也许我遗漏了一些东西,但在第三个示例中,使用try(),您评估if(!is(df, 'try-error')),但a 是存储try() 结果的对象。所以不应该是if(!is(a, 'try-error'))

files <- NULL
#sample.id <- NULL
for(i in 1:length(sample.id.path2[[1]])){
  while(TRUE){
  a <- try(as.data.frame(system(paste0("mcli ls s3/path/to/bucket/", sample.id.path2[['V2']][i], sep = ""), intern=TRUE)), silent=TRUE)
  if(!is(a, 'try-error')) break
  colnames(a)[1] <- "V1"
  a$V1 <- gsub("^.{0,34}", "", a$V1)
  a <- a %>% filter(str_detect(V1, 'fastq'))
  }
  #print(a)
  b <- as.data.frame(paste0("", sample.id.path2[['V2']][i],  sep = ""))
  colnames(b)[1] <- "V1"
  c <- as.data.frame(paste0(b$V1, a$V1,  sep = ""))
  colnames(c)[1] <- "V2"
  d <- cbind(a,c)
  print(d)
  files <- rbind(files, d)
}

【讨论】:

    【解决方案2】:

    DaveArmstrong 的回答效果很好!我发现了另一个不完全涉及 R 的解决方案。我发现命令行工具 retrysystem() 一样好用。我把循环改成了这个

    files <- NULL
    #sample.id <- NULL
    for(i in 1:length(sample.id.path2[[1]])){
      a <- as.data.frame(system(paste0("retry -t 5 mcli ls s3/path/to/bucket/", sample.id.path2[['V2']][i], sep = ""), intern=TRUE))
      colnames(a)[1] <- "V1"
      a$V1 <- gsub("^.{0,34}", "", a$V1)
      a <- a %>% filter(str_detect(V1, 'fastq'))
      #print(a)
      b <- as.data.frame(paste0("", sample.id.path2[['V2']][i],  sep = ""))
      colnames(b)[1] <- "V1"
      c <- as.data.frame(paste0(b$V1, a$V1,  sep = ""))
      colnames(c)[1] <- "V2"
      d <- cbind(a,c)
      print(d)
      files <- rbind(files, d)
    }
    

    我在系统命令中添加了retry,并添加了-t 5以在超时之前重试该命令5次

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-25
      • 2016-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-20
      • 2015-10-23
      • 1970-01-01
      相关资源
      最近更新 更多