Slide 1

Slide 1 text

CSP othree

Slide 2

Slide 2 text

Communicating Sequential Processes

Slide 3

Slide 3 text

https://medium.com/@addyosmani/javascript-application-architecture-on-the-road-to-2015-d8125811101b

Slide 4

Slide 4 text

1978

Slide 5

Slide 5 text

1985

Slide 6

Slide 6 text

What is CSP • Communication between processes • Born to be async • Using channel

Slide 7

Slide 7 text

Process B Process A

Slide 8

Slide 8 text

Process B Process A Channel msg

Slide 9

Slide 9 text

Process B Process A msg PUT

Slide 10

Slide 10 text

Process B Process A msg

Slide 11

Slide 11 text

Process B Process A msg TAKE

Slide 12

Slide 12 text

Process B Process A Channel msg

Slide 13

Slide 13 text

Implements Go Clojure goroutine core.async

Slide 14

Slide 14 text

https://speakerdeck.com/kachayev/channels-and-concurrency-go-clojure-erlang-haskell

Slide 15

Slide 15 text

Go Example

Slide 16

Slide 16 text

package main import "fmt" func main() { messages := make(chan string, 1) messages <- "ping" msg := <-messages fmt.Println(msg) }

Slide 17

Slide 17 text

package main import "fmt" func main() { messages := make(chan string, 1) messages <- "ping" msg := <-messages fmt.Println(msg) }

Slide 18

Slide 18 text

Short Variable Declarations • Declare variable • and assign value at the same time :=

Slide 19

Slide 19 text

package main import "fmt" func main() { messages := make(chan string, 1) messages <- "ping" msg := <-messages fmt.Println(msg) }

Slide 20

Slide 20 text

Receive Operator • Receive message • From channel • or channel receive message • Also imply direction of message <-

Slide 21

Slide 21 text

package main import "fmt" func main() { messages := make(chan string, 1) messages <- "ping" msg := <-messages fmt.Println(msg) }

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Go Example II

Slide 24

Slide 24 text

package main import "fmt" import "time" func worker(done chan bool) { fmt.Print("working...") time.Sleep(time.Second) fmt.Println("done") done <- true } func main() { done := make(chan bool, 1) go worker(done) <-done }

Slide 25

Slide 25 text

package main import "fmt" import "time" func worker(done chan bool) { fmt.Print("working...") time.Sleep(time.Second) fmt.Println("done") done <- true } func main() { done := make(chan bool, 1) go worker(done) <-done }

Slide 26

Slide 26 text

package main import "fmt" import "time" func worker(done chan bool) { fmt.Print("working...") time.Sleep(time.Second) fmt.Println("done") done <- true } func main() { done := make(chan bool, 1) go worker(done) <-done }

Slide 27

Slide 27 text

Go Statement • Execute function as independent process(goroutine) go

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Go Pingpong Example

Slide 30

Slide 30 text

package main import "fmt" import "time" type Ball struct{ hits int } func player(name string, table chan *Ball) { for { ball := <-table ball.hits++ fmt.Println(name, ball.hits) time.Sleep(100 * time.Millisecond) table <- ball } }

Slide 31

Slide 31 text

package main import "fmt" import "time" type Ball struct{ hits int } func player(name string, table chan *Ball) { for { ball := <-table ball.hits++ fmt.Println(name, ball.hits) time.Sleep(100 * time.Millisecond) table <- ball } }

Slide 32

Slide 32 text

player • Receive ball from channel • Hit ball(+1) • Sleep • Send ball back

Slide 33

Slide 33 text

func main() { table := make(chan *Ball) go player("ping", table) go player("pong", table) table <- new(Ball) // game on; toss the ball time.Sleep(1 * time.Second) <-table // game over; grab the ball }

Slide 34

Slide 34 text

func main() { table := make(chan *Ball) go player("ping", table) go player("pong", table) table <- new(Ball) // game on; toss the ball time.Sleep(1 * time.Second) <-table // game over; grab the ball }

Slide 35

Slide 35 text

func main() { table := make(chan *Ball) go player("ping", table) go player("pong", table) table <- new(Ball) // game on; toss the ball time.Sleep(1 * time.Second) <-table // game over; grab the ball }

Slide 36

Slide 36 text

func main() { table := make(chan *Ball) go player("ping", table) go player("pong", table) table <- new(Ball) // game on; toss the ball time.Sleep(1 * time.Second) <-table // game over; grab the ball }

Slide 37

Slide 37 text

Process Channel Message players table Ball

Slide 38

Slide 38 text

player pong player ping table Ball

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

Key Requirement • Channel • Ability to wait(idle) message

Slide 41

Slide 41 text

Channel • First class object • First in first out • Accessible across processes

Slide 42

Slide 42 text

Idle • Not able to idle in JavaScript • Use recursive function call can emulate, bad performance • ES6 have async function

Slide 43

Slide 43 text

async function

Slide 44

Slide 44 text

function* foo(){ var index = 0; while (index <= 2) // when index reaches 3, // yield's done will be true // and its value will be undefined; yield index++; }

Slide 45

Slide 45 text

var iterator = foo(); console.log(iterator.next()); // { value:0, done:false } console.log(iterator.next()); // { value:1, done:false } console.log(iterator.next()); // { value:2, done:false } console.log(iterator.next()); // { value:undefined, done:true }

Slide 46

Slide 46 text

yield • Function will stop and return on yield • Next call will exec from last yield • Sort of idle

Slide 47

Slide 47 text

js-csp

Slide 48

Slide 48 text

js-csp • by Nguyễn Tuấn Anh • Major implementation • Use async function • Also implement `go`, dispatcher…etc

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

Pingpong Example

Slide 51

Slide 51 text

function* player(name, table) { while (true) { var ball = yield csp.take(table); if (ball === csp.CLOSED) { console.log(name + ": table's gone"); return; } ball.hits += 1; console.log(name + " " + ball.hits); yield csp.timeout(100); yield csp.put(table, ball); } }

Slide 52

Slide 52 text

function* player(name, table) { while (true) { var ball = yield csp.take(table); if (ball === csp.CLOSED) { console.log(name + ": table's gone"); return; } ball.hits += 1; console.log(name + " " + ball.hits); yield csp.timeout(100); yield csp.put(table, ball); } } func player(name string, table chan *Ball) { for { ball := <-table ball.hits++ fmt.Println(name, ball.hits) time.Sleep(100 * time.Millisecond) table <- ball } } Go

Slide 53

Slide 53 text

csp.go(function* () { var table = csp.chan(); csp.go(player, ["ping", table]); csp.go(player, ["pong", table]); yield csp.put(table, {hits: 0}); yield csp.timeout(1000); table.close(); });

Slide 54

Slide 54 text

csp.go(function* () { var table = csp.chan(); csp.go(player, ["ping", table]); csp.go(player, ["pong", table]); yield csp.put(table, {hits: 0}); yield csp.timeout(1000); table.close(); }); func main() { table := make(chan *Ball) go player("ping", table) go player("pong", table) table <- new(Ball) time.Sleep(1 * time.Second) <-table } Go

Slide 55

Slide 55 text

csp.go(player, ["ping", table]); go player("ping", table) yield csp.put(table, {hits: 0}); table <- new(Ball) yield csp.take(table); <-table

Slide 56

Slide 56 text

csp.go(function* () { func main() { var table = csp.chan(); table := make(chan *Ball) yield csp.timeout(1000); time.Sleep(1 * time.Second)

Slide 57

Slide 57 text

js-csp • Easy to port Go program • yield is annoying

Slide 58

Slide 58 text

What Can Use CSP • Sequential async data • Event

Slide 59

Slide 59 text

Real World Event • Might have multiple event handler on single event

Slide 60

Slide 60 text

Channel Message • Only can take once

Slide 61

Slide 61 text

operations alt mult mix merge onto pipe split close timeout https://clojure.github.io/core.async/

Slide 62

Slide 62 text

mult • one to many

Slide 63

Slide 63 text

var csp = require("./src/csp"); var mult = csp.operations.mult; var buffers = csp.buffers; var channel = csp.chan(); var m = mult(channel); https://github.com/ubolonton/js-csp/issues/31#issuecomment-68089526

Slide 64

Slide 64 text

var csp = require("./src/csp"); var mult = csp.operations.mult; var buffers = csp.buffers; var channel = csp.chan(); var m = mult(channel);

Slide 65

Slide 65 text

csp.go(function*() { var out = mult.tap(m, csp.chan(buffers.sliding(1))); var val = yield csp.take(out); console.log('process 1', val); }); csp.go(function*() { var out = mult.tap(m, csp.chan(buffers.sliding(1))); var val = yield csp.take(out); console.log('process 2', val); });

Slide 66

Slide 66 text

csp.go(function*() { var out = mult.tap(m, csp.chan(buffers.sliding(1))); var val = yield csp.take(out); console.log('process 1', val); }); csp.go(function*() { var out = mult.tap(m, csp.chan(buffers.sliding(1))); var val = yield csp.take(out); console.log('process 2', val); });

Slide 67

Slide 67 text

csp.go(function*() { var out = mult.tap(m, csp.chan(buffers.sliding(1))); var val = yield csp.take(out); console.log('process 1', val); }); csp.go(function*() { var out = mult.tap(m, csp.chan(buffers.sliding(1))); var val = yield csp.take(out); console.log('process 2', val); });

Slide 68

Slide 68 text

csp.putAsync(channel, 42);

Slide 69

Slide 69 text

not stable on js-csp

Slide 70

Slide 70 text

Cons • Annoying yield • Not stable operations* • Lib size • Not support Web Worker

Slide 71

Slide 71 text

js-csp • Syntax from Go • Operations from Clojure • Have disadvantages • Can use transducer from Clojure

Slide 72

Slide 72 text

https://www.youtube.com/watch?v=7rDsRXj9-cU