Scala Futures未启动问题求助:多Future任务无响应
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 asynchronousFutures 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 usingAwait.resultorAwait.readyto 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
onCompletewith a blocking mechanism (e.g., aCountDownLatch) if you don’t want to block directly.Validate the
extraire_phrasesimplementation
Ifextraire_phrasesuses aPromiseunder 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 atry/catchblock? - Is there an infinite loop or stuck IO operation inside the method that’s preventing completion?
Add debug prints at the start and end ofextraire_phrasesto confirm the method is even being invoked.
- Did you forget to call
Check your ExecutionContext setup
Futures depend on anExecutionContextto 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
- Make sure you have the default ExecutionContext in scope:
Catch unhandled Future failures
Ifextraire_phrasesthrows 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 likesrc/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

