You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Scala Futures未启动问题求助:多Future任务无响应

Troubleshooting Unstarted Future Workers & Uncompleted Promises in Scala

Hey there! Let’s walk through some practical troubleshooting steps to figure out why your extraire_phrases-based Future workers aren’t running, your Promise never completes, and your program exits with no output:

  • Ensure you’re waiting for your Futures/Promise to finish
    Scala’s main thread doesn’t wait for asynchronous Futures by default. If you kick off your workers but don’t block to wait for their completion, the main thread will exit immediately—before your workers even get a chance to start.
    Fix this by using Await.result or Await.ready to block the main thread until your Futures resolve:

    import scala.concurrent.Await
    import scala.concurrent.duration._
    
    val future1 = extraire_phrases(chemin_corpus, chemin_dictionnaire)
    val future2 = extraire_phrases(chemin_corpus_backup, chemin_dictionnaire_backup)
    // Wait for both futures to complete (adjust timeout as needed)
    Await.result(Future.sequence(List(future1, future2)), 30.seconds)
    

    Alternatively, use a callback like onComplete with a blocking mechanism (e.g., a CountDownLatch) if you don’t want to block directly.

  • Validate the extraire_phrases implementation
    If extraire_phrases uses a Promise under the hood, double-check that every code path properly completes the Promise:

    • Did you forget to call promise.success(result) when the work finishes?
    • Did you handle exceptions with promise.failure(exception) in a try/catch block?
    • Is there an infinite loop or stuck IO operation inside the method that’s preventing completion?
      Add debug prints at the start and end of extraire_phrases to confirm the method is even being invoked.
  • Check your ExecutionContext setup
    Futures depend on an ExecutionContext to run their tasks. If you haven’t explicitly provided one, Scala uses the global context—but if it’s misconfigured (e.g., no available threads), your tasks won’t start.

    • Make sure you have the default ExecutionContext in scope: import scala.concurrent.ExecutionContext.Implicits.global
    • Test with a custom ExecutionContext to rule out global context issues:
      import java.util.concurrent.Executors
      import scala.concurrent.ExecutionContext
      
      val customEc = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(2))
      // Pass this EC to your Future if extraire_phrases accepts it, or use it implicitly
      
  • Catch unhandled Future failures
    If extraire_phrases throws an uncaught exception, the Future will enter a failed state—but without a callback, you’ll never see the error. Add failure handlers to debug this:

    future1.onFailure { case e: Exception => 
      println(s"Future 1 failed with error: ${e.getMessage}")
      e.printStackTrace()
    }
    
  • Verify file path validity and IO permissions
    Your code references file paths like src/corpus.txt—if these files don’t exist, or your program lacks read permissions, IO operations could hang or throw silent exceptions.
    Test the file paths manually by trying to read one of them in your main method:

    try {
      val lines = scala.io.Source.fromFile(chemin_corpus).getLines().toList
      println(s"Successfully read ${lines.size} lines from corpus")
    } catch {
      case e: Exception => println(s"Failed to read corpus file: ${e.getMessage}")
    }
    

内容的提问来源于stack exchange,提问作者Vanhaeren Thomas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:12:37