Last active
February 20, 2020 10:20
-
-
Save loicdescotte/05a34d3c39be327dcf1796a54aecf898 to your computer and use it in GitHub Desktop.
ZIO queue example
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
import zio._ | |
import zio.console._ | |
import zio.stream._ | |
import zio.duration._ | |
object ZioQueuePullPush extends zio.App { | |
// c.f. ZIO type aliases https://zio.dev/docs/overview/overview_index#type-aliases | |
val result: URIO[Clock with Console, Unit] = for { | |
queue <- Queue.bounded[Int](100) | |
consumeQueue = ZStream.fromQueue(queue).foreach(e => putStrLn(e.toString)) | |
//Sleep without blocking threads thanks to ZIO fibers | |
feedQueue = ZIO.foreach(Range(1,1000))(e => ZIO.sleep(10.millis) *> queue.offer(e)) | |
//run consume and feed in parallel | |
_ <- consumeQueue.zipPar(feedQueue) | |
} yield () | |
override def run(args: List[String]) = result.map(_ => 1) | |
/* output : | |
1 | |
2 | |
3 | |
... | |
999 | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment