Last active
September 30, 2018 09:43
-
-
Save vasily-kirichenko/9f59d7e067330af6d5b0440158608242 to your computer and use it in GitHub Desktop.
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
def processor(account: String, taskCh: MVar[Int]): Observable[String] = | |
Observable.fromAsyncStateAction[Unit, String](_ => | |
for { | |
task <- taskCh.take | |
// simulate real processing by an async sleep | |
_ <- Task.sleep(Random.nextInt(500).millis) | |
} yield { | |
println(s"[$account] processed $task") | |
((task + 100).toString, ()) | |
} | |
)() | |
val (taskCh, processors) = | |
Await.result( | |
MVar.empty[Int] | |
.map(ch => (ch, List("acc1", "acc2").map(account => processor(account, ch)))) | |
.runAsync, | |
Duration.Inf) | |
val cancelSource = | |
Observable.range(0, 10) | |
.dump("source") | |
.mapParallelUnordered(processors.length) { task => | |
taskCh.put(task.toInt) | |
} | |
.dump("mapParallel") | |
.subscribe() | |
val stream = | |
Observable | |
.merge(processors: _*) | |
.consumeWith(Consumer.foreach(x => println(s"consumed $x"))) | |
Await.result(stream.runAsync, 5.seconds) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment