Skip to content

Instantly share code, notes, and snippets.

@sduckett
Last active August 29, 2015 14:24
Show Gist options
  • Save sduckett/b928416bfffe7c5b02ef to your computer and use it in GitHub Desktop.
Save sduckett/b928416bfffe7c5b02ef to your computer and use it in GitHub Desktop.
Reading: Clojure Applied

This might have come from Clojure Applied

Thinking in Processes

The use of threads, data parallelism with reducers, and task parallelism is for parallelism (of a few tasks), not concurrent threads of execution.

Channels

  • like queues
  • can be passed between threads (thus, they are stateful)
  • hold values in a buffer (which defau;ts to zero)
  • also has dropping buffers and sliding buffers
  • nil means a channel has been closed, and there is no more data to be read from it
(require '[clojure.core.async :as async :refer (<!!, >!!)])

(def c (async/chan 1))
(>!! c "Hello, core.async") ;; put a value into the channel (for ordinary threads)
(println (<!! c))           ;; take a value from the channel  (^^^)
  • core.async channels are never unbounded

‘go’ blocks

  • create lightweight processing loop[s that can be supported by a thread pool
  • try not to block, but rather park outside the channel until there is more space
  • ‘put’ and ‘take’, within a ‘go’ block are known as ‘>!’ and ‘<!’ (respectively)

“Go blocks are a great tool for breaking up a program into potentially concurrent processes.”

For connecting two channels with a parallel transformationstage, we have pipelines: ‘pipeline’, ‘pipeline-blocking’, and ‘pipeline-async’.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment