O'Reilly logo

Seven Concurrency Models in Seven Weeks by Paul Butcher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Day 2: Multiple Channels and IO

Today we’ll see how core.async makes asynchronous IO both simpler and easier to understand. But before then, we’ll look at a feature we’ve not yet seen​—​handling multiple channels at a time.

Handling Multiple Channels

So far we’ve dealt only with a single channel at a time, but there’s no reason we have to restrict ourselves to doing so. The alt! function allows us to write code that can deal with more than one channel:

 
channels.core=>​ (def ch1 (chan))
 
#'channels.core/ch1
 
channels.core=>​ (def ch2 (chan))
 
#'channels.core/ch2
 
channels.core=>​ (go-loop []
 
#_=>​ (alt!
 
#_=>​ ch1 ([x] (println "Read" x "from channel 1"))
 
#_=>​ ch2 ([x] (println "Twice" x "is" (* x 2))))
 
#_=>​ (recur))
 
#<ManyToManyChannel ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required