Skip to content

Instantly share code, notes, and snippets.

@loicdescotte
Last active February 20, 2020 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loicdescotte/05a34d3c39be327dcf1796a54aecf898 to your computer and use it in GitHub Desktop.
Save loicdescotte/05a34d3c39be327dcf1796a54aecf898 to your computer and use it in GitHub Desktop.
ZIO queue example
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