Skip to content

Instantly share code, notes, and snippets.

@p-pavel
Created September 11, 2023 04:35
Show Gist options
  • Save p-pavel/1de3652f4add4bb20141ff1a9ab19c17 to your computer and use it in GitHub Desktop.
Save p-pavel/1de3652f4add4bb20141ff1a9ab19c17 to your computer and use it in GitHub Desktop.
parallel jobs simplest demo
//>using lib "org.typelevel::cats-effect:3.5.1"
import cats.effect.*
import cats.implicits.*
object HowToRunJobsInParallel extends IOApp.Simple:
def jobConstructor(r: Ref[IO, Long])(jobId: Any) = r
.modify(n => (n + 1, n))
.flatMap(v => IO.println(s"Job $jobId processed task $v"))
def run =
for
ref <- Ref[IO].of(0l)
mkJob = jobConstructor(ref)
jobs = (1 to 10000).map(mkJob).toList
_ <- jobs.parSequence_
yield ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment