нужно запустить Scala в будущем последовательно - PullRequest
0 голосов
/ 03 июня 2019

В моем коде 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 Ответ

1 голос
/ 03 июня 2019

Вы можете использовать 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.")
...