I compiled a list of Communicating Sequential Processes / core.async links for a friend after my Great British Node Conf lightning talk so thought I would share them here too.
I still think the best intro to CSP is these talks from Rob Pike
- Rob Pike - ‘Concurrency Is Not Parallelism’
- Rob Pike - Go Concurrency Patterns
- Sameer Ajmani - Advanced Go Concurrency Patterns
Very interesting is this point of Rob’s IO talk.
The select statement is a control structure somewhat like a switch that lets you control the behaviour of your program based on what communications are able to procede at any moment. In fact this select statement is a key part of why concurrency is built into Go as features of the language rather than just a library. It’s hard to do control structures that depend on libraries…
This is where the power of macros is apparent, we can (somewhat) easily add CSP to clojure as a first class citizen alongside all the other great concurrency models that have existed since it was created. It is amazing that core.async works on the JVM and in clojurescript.
core.async in cljs from David Nolen
- Using CSP book from Oxford
I can’t resist including one example myself as it’s so neat a programming model
(let [c1 (chan) c2 (chan)] (go (while true (let [[v ch] (alts! [c1 c2])] (println "Read" v "from" ch)))) (go (>! c1 "hi")) (go (>! c2 "there")))
Here we create 2 channels and 3 go blocks that are to be thought of as
acting concurrently, the first one is an infinite loop pulling off
both channels and printing the message received (
alts! gets whatever
result is ready from the list of channels provided), the next two
simply send (with the
>! function) the strings “hi” and “there” into
c1 and c2 respectively.