Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSP

Avatar for othree othree
February 14, 2015

 CSP

Communicating Sequential Processes

Avatar for othree

othree

February 14, 2015
Tweet

More Decks by othree

Other Decks in Technology

Transcript

  1. package main import "fmt" func main() { messages := make(chan

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

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

    string, 1) messages <- "ping" msg := <-messages fmt.Println(msg) }
  4. Receive Operator • Receive message • From channel • or

    channel receive message • Also imply direction of message <-
  5. package main import "fmt" func main() { messages := make(chan

    string, 1) messages <- "ping" msg := <-messages fmt.Println(msg) }
  6. 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 }
  7. 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 }
  8. 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 }
  9. 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 } }
  10. 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 } }
  11. 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 }
  12. 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 }
  13. 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 }
  14. 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 }
  15. Idle • Not able to idle in JavaScript • Use

    recursive function call can emulate, bad performance • ES6 have async function
  16. 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++; }
  17. 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 }
  18. yield • Function will stop and return on yield •

    Next call will exec from last yield • Sort of idle
  19. js-csp • by Nguyễn Tuấn Anh • Major implementation •

    Use async function • Also implement `go`, dispatcher…etc
  20. 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); } }
  21. 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
  22. 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(); });
  23. 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
  24. csp.go(function* () { func main() { var table = csp.chan();

    table := make(chan *Ball) yield csp.timeout(1000); time.Sleep(1 * time.Second)
  25. operations alt mult mix merge onto pipe split close timeout

    https://clojure.github.io/core.async/
  26. 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
  27. var csp = require("./src/csp"); var mult = csp.operations.mult; var buffers

    = csp.buffers; var channel = csp.chan(); var m = mult(channel);
  28. 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); });
  29. 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); });
  30. 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); });
  31. js-csp • Syntax from Go • Operations from Clojure •

    Have disadvantages • Can use transducer from Clojure