Created
March 27, 2018 10:57
-
-
Save brendanmaguire/f029a05a64de28252fa538c5c1947215 to your computer and use it in GitHub Desktop.
Run cats.effect.IO's in parallel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or…