Skip to content

Instantly share code, notes, and snippets.

@tomasherman
Last active November 19, 2018 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomasherman/d673a5d4d32c1f0ed133e6a018bce28f to your computer and use it in GitHub Desktop.
Save tomasherman/d673a5d4d32c1f0ed133e6a018bce28f to your computer and use it in GitHub Desktop.
paralellism cats
import java.util.concurrent.Executors
import cats.effect.{Effect, IO}
import monix.eval.Task
import scala.concurrent.Await
import scala.concurrent.duration._
import scala.language.{higherKinds, postfixOps}
object Paralellism extends App {
implicit val s = monix.execution.Scheduler.apply(Executors.newFixedThreadPool(10))
def runParallel[F[_]: Effect, A, B](a: F[A], b: F[B]): F[B] = {
val E = Effect[F]
E.flatMap(E.pure(println("running")))(_ => {
val delayed = E.runAsync(a)(IO.fromEither[A] _ andThen {io => io.map(_ => ())}).to[F] //do some recovery here
E.map2(delayed, b)((a, b) => b)
})
}
val task = runParallel(
Task.sleep(5 seconds).flatMap(_ => Task.eval({println("5 seconds passed")})),
Task.eval("done son")
).map(println).flatMap(_ => Task.sleep(10.seconds)).runToFuture
Await.result(task, 30 seconds)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment