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] ...

Get Seven Concurrency Models in Seven Weeks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.