Skip to content

Instantly share code, notes, and snippets.

@MichaelDrogalis
Created December 5, 2014 22:24
Show Gist options
  • Save MichaelDrogalis/ee4f4e4b8d978d4226b6 to your computer and use it in GitHub Desktop.
Save MichaelDrogalis/ee4f4e4b8d978d4226b6 to your computer and use it in GitHub Desktop.
(def chan-a (chan 10)) ;; channel with a buffer of 10 elements
(def chan-b (chan 10))
(def chan-c (chan 10))
(>!! chan-a :message-1) ;; >!! = put onto channel
(>!! chan-b :message-2)
(>!! chan-c :message-3)
(loop [chs [chan-a chan-b chan-c]]
;; alts!! = try to take a value from any of the supplied channels
;; :priority? true = try to take from the channels in order
;; (timeout 1000) = if no channels returned a value, return nil after 1s
(println (first (alts!! (conj chs (timeout 1000)) :priority? true)))
;; rotate the channel order for fairness
(recur (conj (vec (rest chs)) (first chs))))
;;;:message-1
;;;:message-2
;;;:message-3
;;;nil
;;;nil
;;;nil
;...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment