Concurrency and Channels
CSP : Communicating Sequential Processes
Pattern from 60ies, Hore
modelling concurrency with channels
there's no buffer, so backpressure built-in.
Backpressure: the example of a hose where the two ends cannot communicate. The spraying end just blocks the hose to stop more water going through.
This is how messages the producer that the consumer doesn't want more.
Channel (don't forget that the producer and consumer don't "see" each other, cannot communicate): "don't send anything until I'm ready to receive it and can't receive anything anything until the other end is ready to send it"
no queue
Actor-model concurrency - related pattern
With actors the message sending is async
with CSP the message sending is sync. The other end has to be ready to receive it.
It's about setting up the application in independent pieces. But sometimes they need to communicate with each other and coordinate. Of course in JS there are no threads.
Generators are like this. They can "run" and block independently.
We can model each piece of the application independently. And just communicate by messages. They could each run in a different thread.
Go and Clojuescript work like this.
Demo:
var ch = chan();
function *process1() {
yield put(ch,"Hello");
var msg = yield take(ch);
console.log(msg);
}
function *process2() {
var greeting = yield take(ch);
yield put(ch,greeting + " World");
console.log("done!");
}
// Hello World
// done!
put
is a way to send a message down the channel. We stop ( yield
) until someone is ready to take this message. The two process can now be independent and the message taking semantics take care of starting and stopping them.
The people at Go and Clojurescript and Kyle included are convinced that this is the most powerful concurrency management model invented so far.Clojuscript OM framework by David Nolen. Most performant user interface.