Scala在for循环中创建一个带有Future的List,并检查每个Future的结果

时间:2017-11-06 19:25:30

标签: scala future

我需要并行处理条目列表并验证结果。任何人都可以帮助我在Scala中实现这一目标

就像下面这样,

FileList = List { "/temp/File1", "/temp/File2","/temp/File3","/temp/File4"........}

for( file <- FileList){
  Future { getRecCount(file) } // Need to create a List with all Futures
}

现在我需要检查所有期货的结果直到完成

getRecCount - 将计算记录计数并写入文件

2 个答案:

答案 0 :(得分:0)

如果您只对Exception感兴趣,可以在Try中包含Future的正文,并使用Future.traverse作为@ adrice727提到的paralles文件处理:

// Sequence of files for processing
val files = Seq[String]("/tmp/1.txt", "/tmp/2.txt", "/tmp/3.txt", "/tmp/4.txt",
  "/tmp/5.txt", "/tmp/6.txt")

def fileHandler(path: String): String = {
  val idx = files.indexOf(path)
  // Throw exception for each second file
  if (idx % 2 == 0) throw new Exception(s"Unable to hande $path")
  else path
}

val futures: Future[Seq[Try[String]]] = Future.traverse(files){ path =>
  Future {
    // Wrap it by Try
    Try {
      // Each file handler
      fileHandler(path)
    }
  }
}

futures.map { seq =>
  seq.map {
    case Success(s) => println(s"Result: $s")
    case Failure(ex) => println(s"Exception: ${ex.getMessage}")
  }
}

答案 1 :(得分:0)

谢谢 - 这是我实施的方式

val tobeProcesssed:Seq [String] = Seq(&#34; FileName1&#34;,&#34; FileName2&#34;,&#34; FileName3,&#34; FileName4)//用文件创建Seq名称

    val latch = new CDL(tobeProcesssed.size)
      val trackFutures: Future[Seq[Try[Boolean]]] = Future.traverse(tobeProcesssed.seq) { fileNm =>
        Future {
          LOG.info("In the Futures Loop for {}", fileNm)
          Try {
                    doProcessing(fileNm)
          }
        }
      }

      trackFutures.map { seq =>
        seq.map {
          case Success(state) => {
            if (state) {
              LOG.info("On Sucess - {} - {}", latch.getCount, state)
              return = 0
              latch.countDown()

            } else {
              LOG.error("Failed - {} - {}", latch.getCount, state)
              while (latch.getCount > 0) {
                return  = 1
                latch.countDown()
              }
            }

          }
          case Failure(exception) => {
            LOG.error("Versioning Failed -{} - {}", latch.countDown(), exception)
            while (latch.getCount > 0) {
              return = = 1
              latch.countDown()
            }
          }
        }
      }
      latch.await()