需要按顺序 运行 scala future

need to run scala future sequentially

在我的 akka scala 代码中,我试图操纵由相同代码创建的文件。

流程

1. create a file A
2. use file A and create file B
3. use file B and do some mapping and create an ouput file

现在在创建第 1 步之前,正在执行第 2 步,第 3 步出现类似问题

请注意:步骤 1 和步骤 2 各耗时 2 分钟。 为了处理这种情况,我把 Thread.sleep 让代码转到第 2 步,但是第 2 步 更耗时并且放置 thread.sleep(5000) 会抛出 Akka 超时错误。

有没有办法优雅地处理这个问题。

我的要求的要点是我想 运行 按顺序执行步骤。

实际代码如下

val tmpDir = "src/main/resources/"
logger.debug("Import all documents to mallet...")
Text2Vectors.main(("--input " + tmpDir + "new_corpus/ --keep-sequence --remove-stopwords " + "--output " + tmpDir + "new_corpus.mallet --use-pipe-from " + tmpDir + "corpus.mallet").split(" "))
logger.debug("Run training process...")
Thread.sleep(10000)
InferTopics.main(("--input " + tmpDir + "new_corpus.mallet --inferencer " + tmpDir + "inferencer " + "--output-doc-topics " + tmpDir + "doc-topics-new.txt --num-iterations 1000").split(" "))
Thread.sleep(50000)

logger.debug("Inferring process finished.")

您可以使用 Await.result( yourFuture ,Duration.Inf)

或者使用地图并在地图内部使用(首选方式)

val tmpDir = "src/main/resources/"
logger.debug("Import all documents to mallet...")
Await.result(Text2Vectors.main(("--input " + tmpDir + "new_corpus/ --keep-sequence --remove-stopwords " + "--output " + tmpDir + "new_corpus.mallet --use-pipe-from " + tmpDir + "corpus.mallet").split(" ")),Duration.Inf)
logger.debug("Run training process...")
Await.result(InferTopics.main(("--input " + tmpDir + "new_corpus.mallet --inferencer " + tmpDir + "inferencer " + "--output-doc-topics " + tmpDir + "doc-topics-new.txt --num-iterations 1000").split(" ")),Duration.Inf)
logger.debug("Inferring process finished.")

或使用地图:

val tmpDir = "src/main/resources/"
logger.debug("Import all documents to mallet...")
val firstFuture = Text2Vectors.main(("--input " + tmpDir + "new_corpus/ --keep-sequence --remove-stopwords " + "--output " + tmpDir + "new_corpus.mallet --use-pipe-from " + tmpDir + "corpus.mallet").split(" "))
logger.debug("Run training process...")

firstFuture.map(InferTopics.main(("--input " + tmpDir + "new_corpus.mallet --inferencer " + tmpDir + "inferencer " + "--output-doc-topics " + tmpDir + "doc-topics-new.txt --num-iterations 1000").split(" ")))

logger.debug("Inferring process finished.")