Skip to content

Instantly share code, notes, and snippets.

@reborg
Last active August 1, 2018 08:15
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 reborg/ba05335edb09d50e1bd9f0fc5d6593f1 to your computer and use it in GitHub Desktop.
Save reborg/ba05335edb09d50e1bd9f0fc5d6593f1 to your computer and use it in GitHub Desktop.
core.async master/worker pattern with throttling timeout
;; Install Clojure deps and try with:
;; clojure -Sdeps
;; '{:deps {user {:git/url "https://gist.github.com/reborg/ba05335edb09d50e1bd9f0fc5d6593f1"
;; :sha "435181444970c1fa2091283285830c63076fda4b"}}}' -e '(process [1 2 3])'
{:paths ["."]
:deps {org.clojure/core.async {:mvn/version "0.4.474"}}}
(ns user
(:require [clojure.core.async :refer
[go go-loop chan >! <! <!! close! timeout]]))
(defn master [items in]
(go
(doseq [item items]
(<! (timeout 100)) ;; ms
(>! in item))
(close! in)))
(defn worker [out]
(let [in (chan)]
(go-loop []
(if-some [item (<! in)]
(do
(>! out (str "*" item "*"))
(recur))
(close! out)))
in))
(defn process [items]
(let [out (chan)]
(master items (worker out))
(loop [res []]
(if-some [item (<!! out)]
(recur (conj res item))
res))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment