[SOLVED] ParSeq operration hangs in REPL

I try using ParSeq, but am not sure why it hangs after pressing enter button after ... pseq.map {e => e + 1}. Any reasons why?

Welcome to Scala 2.12.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_151).
Type in expressions for evaluation. Or try :help.

scala> import scala.collection.parallel.ParSeq
import scala.collection.parallel.ParSeq

scala> val seq = Seq(1, 2, 3)
seq: Seq[Int] = List(1, 2, 3)

scala> val pseq = seq.par
pseq: scala.collection.parallel.ParSeq[Int] = ParVector(1, 2, 3)

scala> val pseq1 = pseq.map {e => e + 1} 
# <- cursor stops here infinite

This is a problem with the way objects are initialized in Scala. In short, the REPL creates an object with your entered code in the constructor, the constructor starts the parallel code and waits for its completion while the parallel code waits for the object initialization to complete, so you get a deadlock. See this excellent StackOverflow answer for a more detailed explanation.

A possible workaround is lazy evaluation of the parallel operation, i.e. in your case lazy val pseq1. That way, the parallel code will only be executed when the val is used, so as long as all uses inside the same object initialization (or REPL line) are lazy, the deadlock will not happen.

1 Like

Thanks, that fixes my problem!

This kept driving me crazy – I knew parallel code would run within the REPL when I called it within other classes but whenever I had blocks that could benefit from a quick and dirty (e.g. anything with I/O per block) in the REPL it would freeze.
Answer makes perfect sense! (Everything is easy once you know how…)
That lazy val trick works like magic!
Thank you.