【问题标题】:need to run scala future sequentially需要按顺序运行 scala 未来
【发布时间】:2019-10-19 08:15:58
【问题描述】:

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

【问题讨论】:

  • 这里的未来在哪里?阿卡在哪里?谁在消费这些结果?这没有任何意义

标签: scala future


【解决方案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.")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 2022-01-04
    • 2019-04-07
    • 2015-02-15
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多