Chuyển đến nội dung

Bài 26 · State and Concurrency

core.async and Concurrency Patterns

Learn core.async channels in Clojure: chan, put!, take!, go blocks, <! and >!, buffers, alts!, timeouts, pipelines and fan-out patterns, with runnable stand-ins.

Atoms, refs and agents manage shared state. A different school of concurrency avoids sharing: independent workers communicate by passing messages over channels. In Clojure this style comes from the core.async library, modeled on Communicating Sequential Processes (CSP) and the channels of Go. It is a separate library (org.clojure/core.async), not part of Clojure itself, and it works in both Clojure and ClojureScript.

CLOJURE
(require '[clojure.core.async :as a :refer [chan go go-loop <! >! <!! >!! close! alts! timeout]])

Channels

A channel is a thread-safe queue. One side puts values in, the other takes them out.

CLOJURE
(def c (chan 2))        ; buffer of 2

(>!! c :a)              ; blocking put (from an ordinary thread)
(>!! c :b)
(<!! c)                 ;=> :a      blocking take, first in first out
(close! c)              ; no more puts; takes drain the buffer, then return nil

Key rules:

  • chan with no argument is unbuffered: a put waits until someone takes, and vice versa (a rendezvous).
  • (chan n) has a fixed buffer of n; (a/dropping-buffer n) and (a/sliding-buffer n) throw away new or old values when full instead of blocking.
  • nil cannot be put on a channel, because nil is what a take returns for a closed, empty channel.
  • The doubled-bang functions (>!!, <!!) block the current thread and are for ordinary code. The single-bang ones (>!, <!) park a go block and are allowed only inside go.

go blocks

A go block is a lightweight process. It looks like sequential code, but when it hits <! or >! it parks (frees its thread) until it can continue. Thousands of go blocks can share a small thread pool.

CLOJURE
(def results (chan))

(go
  (let [x (<! (go (* 6 7)))]      ; a go block returns a channel with its result
    (>! results x)))

(<!! results)   ;=> 42

Use go-loop for a go block that loops: (go-loop [] (when-let [m (<! in)] (println m) (recur))) reads until in closes.

Do not block inside a go block with <!!, Thread/sleep or heavy I/O: you tie up one of the few pool threads. For blocking work, use a/thread, which runs a body on a real thread and returns a channel with the result.

Waiting on several channels: alts! and timeout

alts! waits for whichever of several channel operations can complete first, and returns [value channel].

CLOJURE
(go
  (let [[v ch] (alts! [work-ch (timeout 1000)])]
    (if (= ch work-ch)
      (println "got" v)
      (println "timed out"))))

(timeout ms) returns a channel that closes after the delay, which makes it the standard way to give an operation a deadline. A :default option makes alts! non-blocking, and :priority true checks channels in order.

Common patterns

Producer and consumer. One go block puts, another takes. The buffer smooths bursts, and an unbuffered channel gives natural back-pressure.

Fan-out. Start several workers reading from the same input channel; each value goes to exactly one worker. It is an easy way to parallelize.

CLOJURE
(defn start-workers [n in out f]
  (dotimes [_ n]
    (go-loop []
      (when-some [x (<! in)]
        (>! out (f x))
        (recur)))))

Pipelines. (a/pipeline n out xf in) runs a transducer on n parallel workers and preserves order; pipeline-blocking is for blocking f, and pipeline-async for async callbacks. Passing a transducer to chan itself works too: (chan 10 (map inc)).

Pub/sub and mult. (a/mult c) copies each value to every channel tapped to it; pub/sub route by topic.

Request/response. Send a map containing a reply channel; the server puts its answer there. This gives you RPC with no shared state at all.

CLOJURE
(defn ask [server request]
  (let [reply (chan 1)]
    (>!! server {:req request :reply reply})
    (<!! reply)))

Which concurrency tool when

Channels shine for pipelines, event-driven systems, UI event handling in ClojureScript (where there are no threads to block), and anywhere you want to model "process A tells process B". Atoms and refs are simpler for plain shared state, and futures for one-shot background work. Mixing them is normal: an atom for a cache, a channel for incoming events.

Typical pitfalls:

  • Forgetting to close! a channel, so consumers wait forever.
  • Putting nil (throws) or taking from a channel nobody feeds (parks forever).
  • Unbounded buffers that hide a slow consumer; prefer small buffers.
  • Swallowed exceptions inside go blocks: they do not propagate to the caller, so catch and report errors on a channel.

Try it: channels with async JavaScript

This stand-in models an unbuffered/buffered channel with promises so you can see put/take order, closing and alts!-style racing.

JS A channel, a worker and a close

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Try it: fan-out to workers

Three workers share one input channel; each job is handled by exactly one worker.

JS Fan-out

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What does a take (<!!) return from a closed, empty channel?

    nil signals closed and drained, which is why nil can never be put on a channel.
  2. 2. Where may you use <! (single bang)?

    <! and >! park a go block and work only inside go; use <!! and >!! in ordinary threads.
  3. 3. How do you give a channel read a one-second deadline?

    timeout returns a channel that closes after the delay; alts! returns whichever completes first.
  4. 4. Why avoid Thread/sleep inside a go block?

    Go blocks should park, not block. For blocking work use a/thread, or a/timeout with <!.

Summary

  • core.async passes messages over channels instead of sharing state; it is a library, not core Clojure.
  • go blocks park on <!/>!; ordinary threads block on <!!/>!!; a/thread runs blocking work.
  • Unbuffered channels rendezvous; buffers can block, drop or slide; closed empty channels return nil.
  • alts! and timeout handle selection and deadlines; fan-out, pipelines, mult/pub and reply channels cover most designs.
  • Close channels, catch errors inside go blocks and keep buffers small.

Next: Macros: The Basics

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.