Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a `List` of `IO` in parallel

This is an example when we want to execute 3 IO in parallel

def test: Unit = {
    val ioA = IO.shift *> IO(println("Running ioA"))
    // ioA: cats.effect.IO[Unit] = <function1>

    val ioB = IO.shift *> IO(println("Running ioB"))
    // ioB: cats.effect.IO[Unit] = <function1>

    val ioC = IO.shift *> IO(println("Running ioC"))
    // ioC: cats.effect.IO[Unit] = <function1>

    val program: IO[Unit] = (ioA, ioB, ioC).parMapN { (_, _, _) => () }
    // program: cats.effect.IO[Unit] = <function1>

    program.unsafeRunSync()
  }

First question: What if the point of using IO.shift in this example ?

Second question: What if we have a List of IO we want to execute in parallel ? I've created a function for this task but I don't know if this thing existed already in the library

def parallelize(ios: List[IO[Unit]]): IO[Unit] = {
    ios.foldLeft(IO.pure(())) { case (currentResult, currentItem) =>
      (currentResult, currentItem).parMapN((_, _) => ())
    }
  }
like image 286
nam Avatar asked Apr 16 '18 12:04

nam


1 Answers

This works with "cats-core:1.1.0" and "cats-effect:0.10.1"

import cats.instances.list._
import cats.syntax.parallel._

//ios: List[IO[A]]
ios.parSequence //IO[List[A]]
like image 156
Laurynas Tretjakovas Avatar answered Nov 02 '22 04:11

Laurynas Tretjakovas