Created
September 11, 2023 04:35
-
-
Save p-pavel/1de3652f4add4bb20141ff1a9ab19c17 to your computer and use it in GitHub Desktop.
parallel jobs simplest demo
This file contains hidden or 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
//>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