0

I have this code evaluated with Ammonite:

$ amm
Welcome to the Ammonite Repl 2.5.1 (Scala 2.13.8 Java 17.0.1)
@ import $ivy.`org.scala-lang.modules::scala-parallel-collections:1.0.4`
@ import scala.collection.parallel.CollectionConverters._
@ Seq(1,2).foreach { x => Thread sleep x*1000; println(s"Fin $x") };println("Fin") 
Fin 1
Fin 2
Fin

It completes ok. If I add the par to parallelize, then it never finishes:

@ Seq(1,2).par.foreach { x => Thread sleep x*1000; println(s"Fin $x") };println("Fin")

Is this a bug? With Scala 2.12 I get the same behaviour.

3
  • Parallel collections and REPLs never play well together. You may try just wiring a normal program or looking for the options to tune so it works in REPLs. Commented Feb 24, 2022 at 13:20
  • I've tried win an Ammonite script and it behaves the same. Commented Feb 25, 2022 at 8:16
  • I'm used to employ successfully parallelism in Ammonite scritps. Commented Feb 25, 2022 at 8:33

1 Answer 1

1
+50

You're having this problem due this bug with Scala's lambda encoding that will also happen in the Scala REPL.

The bug on the Scala side: https://github.com/scala/scala-parallel-collections/issues/34

The corresponding ammonite bug report is here: https://github.com/com-lihaoyi/Ammonite/issues/556

You can work around this in two ways that I'm aware of. The first is to put your parallel work inside of an object e.g.

  object work {
    def execute() = Seq(1, 2).foreach { x =>
      Thread.sleep(x * 1000); println(s"Fin $x")
    }; println("Fin")
  }
  work.execute

I believe running Ammonite with amm --class-based should also do the trick, but I'm not able to test this right now.

Not the answer you're looking for? Browse other questions tagged or ask your own question.