Created
February 1, 2015 01:51
-
-
Save jdivock/bd486d60af86ca8d370c to your computer and use it in GitHub Desktop.
clojure async channels
This file contains 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
(ns async-tea-party.core | |
(:require [clojure.core.async :as async]) | |
) | |
(def tea-channel (async/chan 10)) | |
(async/>!! tea-channel :cup-of-tea) | |
(async/<!! tea-channel) | |
(async/>!! tea-channel :cup-of-tea-2) | |
(async/>!! tea-channel :cup-of-tea-3) | |
(async/>!! tea-channel :cup-of-tea-4) | |
(async/close! tea-channel) | |
(async/<!! tea-channel) | |
(async/<!! tea-channel) | |
(async/<!! tea-channel) | |
(async/<!! tea-channel) | |
;; async channel work | |
(let [tea-channel (async/chan)] | |
(async/go (async/>! tea-channel :cup-of-tea-1)) | |
(async/go (println "Thanks for the " (async/<! tea-channel)))) | |
(def tea-channel (async/chan 10)) | |
(async/go-loop [] | |
(println "Thanks for the " (async/<! tea-channel)) | |
(recur)) | |
(async/>!! tea-channel :hot-cup-of-tea) | |
(async/>!! tea-channel :mint-tea) | |
(def tea-channel (async/chan 10)) | |
(def milk-channel (async/chan 10)) | |
(def sugar-channel (async/chan 10)) | |
(async/go-loop [] | |
(let [[v ch] (async/alts! [tea-channel | |
milk-channel | |
sugar-channel])] | |
(println "Got " v " from " ch) | |
(recur))) | |
(async/>!! tea-channel :cup-of-tea-1) | |
(async/>!! milk-channel :cup-of-milk-1) | |
(async/>!! sugar-channel :cup-of-sugar-1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment