需要按顺序运行Scala Future

时间:2019-06-03 18:53:46

标签: scala future

在我的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.")

1 个答案:

答案 0 :(得分:1)

您可以使用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.")