Scala-期货未开始 [英] Scala - Futures not starting

查看:75
本文介绍了Scala-期货未开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个名为 extraire_phrases 的Future方法启动两个工作线程。我称呼他们为主要任务,但Promise似乎从未实现,并且在任务结束时我什么也得不到,就好像工人没有开始一样。有任何想法吗?

I want to start two workers on a Future method called extraire_phrases . I call them in my main, but it seems that the Promise is never fulfilled and I don't get anything at the end of my main, as if the workers don't start. Any ideas? Thanks a lot.

object Main {

    val chemin_corpus:String = "src/corpus.txt"
    val chemin_corpus_backup:String = "src/tartarinalpes.txt"
    val chemin_dictionnaire:String = "src/dicorimes.dmp"
    val chemin_dictionnaire_backup:String = "src/dicorimes2.dmp"

    def main(args:Array[String]){

        val quatrain = Promise[List[Phrase]]()

        var grosPoeme = List[Phrase]()

        Future {
          val texte_1 = Phrases.extraire_phrases(chemin_corpus, chemin_dictionnaire)
          val texte_2 = Phrases.extraire_phrases(chemin_corpus_backup, chemin_dictionnaire_backup)

          texte_1.onComplete {
            case Success(list) => {
              val poeme = new DeuxVers(list)
              poeme.ecrire :: grosPoeme
            }
            case Failure(ex) => {
              quatrain.failure(LameExcuse("Error: " + ex.getMessage))
            }
          }

          texte_2.onComplete {
            case Success(lst) => {
              val poeme2 = new DeuxVers(lst)
              poeme2.ecrire :: grosPoeme
            }
            case Failure(ex) => {
              quatrain.failure(LameExcuse("Error: " + ex.getMessage))
            }
          }
        quatrain.success(grosPoeme)
        }

        println(quatrain.future)
        println(grosPoeme)
    }

}

执行后,这是我的控制台中的内容:

Here is what I have in my console after execution:

Future(<not completed>)
List()

即使我删除了未来{ val texte_1 之前似乎没有一个能够正确触发,texte_1以某种方式启动,有时可以正常工作,有时不工作,并且texte_2从不启动(永远不会完成)。

Even if I remove the Future { before val texte_1 it seems that none of them fire properly, texte_1 starts somehow, sometimes it works, sometimes not, and texte_2 never starts (never goes to completion). No failure either.

//编辑:Alvaro Carrasco的答案是正确的。谢谢你们俩的帮助

// Alvaro Carrasco's answer is the correct one. Thank both of you however for the help

推荐答案

期货是异步执行的,您的代码不会等待完成。 onComplete 将安排一些代码在将来完成时运行,但不会强迫您的程序等待结果。

Futures are executed asynchronously and your code won't "wait" for them to finish. onComplete will schedule some code to run when the future completes, but it won't force your program to wait for the result.

您需要使用 map / flatMap / sequence 对内部期货进行处理,因此最终您将获得一个单一的期货,然后使用 Await.result(...)

You need to thread the inner futures using map/flatMap/sequence so you end up with a single future at the end and then wait for it using Await.result(...).

您并不需要 Promise

You don't really need Promise here, as exceptions will caught by the future.

像这样的事情:

object Main {

  val chemin_corpus:String = "src/corpus.txt"
  ...

  def main(args:Array[String]){
    ...

    val f1 = texte_1
      .map {list => 
        val poeme = new DeuxVers(list)
        poeme.ecrire :: grosPoeme
      }

    val f2 = texte_2
      .map {lst => 
        val poeme2 = new DeuxVers(lst)
        poeme2.ecrire :: grosPoeme
      }

    // combine both futures
    val all = for {
      res1 <- f1
      res2 <- f2
    } yield {
      println(...)
    }
    // wait for the combined future
    Await.result(all, 1.hour)
  }
}

这篇关于Scala-期货未开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆