Skip to content

Instantly share code, notes, and snippets.

@brendanmaguire
Created March 27, 2018 10:57
Show Gist options
  • Save brendanmaguire/f029a05a64de28252fa538c5c1947215 to your computer and use it in GitHub Desktop.
Save brendanmaguire/f029a05a64de28252fa538c5c1947215 to your computer and use it in GitHub Desktop.
Run cats.effect.IO's in parallel
import scala.concurrent.ExecutionContext.Implicits.global
import cats.effect.IO
import cats.implicits._
object CatsIOParallelApp extends App {
def printThreadId(msg: String) =
println(s"${Thread.currentThread.getId} : $msg")
def io(x: String) = IO {
println(s"starting $x")
printThreadId(x)
Thread.sleep(100)
println(s"finishing $x")
"XX " + x + " XX"
}
printThreadId("main start")
val i1 = IO.shift *> io("one")
val i2 = IO.shift *> io("two")
val i3 = IO.shift *> io("three")
val result = List(i1, i2, i3).parSequence
printThreadId("main wait")
println(result.unsafeRunSync())
printThreadId("main end")
}
@arbitrary-dev
Copy link

Or…

implicit val cs = IO.contextShift()
i1 &> i2 &> i3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment