CSP

C4ce16f549c450f4759eb37f5d5d1a63?s=47 othree
February 14, 2015

 CSP

Communicating Sequential Processes

C4ce16f549c450f4759eb37f5d5d1a63?s=128

othree

February 14, 2015
Tweet

Transcript

  1. 4.
  2. 5.
  3. 16.

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

    string, 1) messages <- "ping" msg := <-messages fmt.Println(msg) }
  4. 17.

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

    string, 1) messages <- "ping" msg := <-messages fmt.Println(msg) }
  5. 19.

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

    string, 1) messages <- "ping" msg := <-messages fmt.Println(msg) }
  6. 20.

    Receive Operator • Receive message • From channel • or

    channel receive message • Also imply direction of message <-
  7. 21.

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

    string, 1) messages <- "ping" msg := <-messages fmt.Println(msg) }
  8. 22.
  9. 24.

    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 }
  10. 25.

    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 }
  11. 26.

    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 }
  12. 28.
  13. 30.

    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 } }
  14. 31.

    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 } }
  15. 33.

    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 }
  16. 34.

    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 }
  17. 35.

    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 }
  18. 36.

    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 }
  19. 39.
  20. 41.
  21. 42.

    Idle • Not able to idle in JavaScript • Use

    recursive function call can emulate, bad performance • ES6 have async function
  22. 44.

    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++; }
  23. 45.

    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 }
  24. 46.

    yield • Function will stop and return on yield •

    Next call will exec from last yield • Sort of idle
  25. 47.
  26. 48.

    js-csp • by Nguyễn Tuấn Anh • Major implementation •

    Use async function • Also implement `go`, dispatcher…etc
  27. 49.
  28. 51.

    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); } }
  29. 52.

    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
  30. 53.

    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(); });
  31. 54.

    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
  32. 56.

    csp.go(function* () { func main() { var table = csp.chan();

    table := make(chan *Ball) yield csp.timeout(1000); time.Sleep(1 * time.Second)
  33. 61.

    operations alt mult mix merge onto pipe split close timeout

    https://clojure.github.io/core.async/
  34. 63.

    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
  35. 64.

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

    = csp.buffers; var channel = csp.chan(); var m = mult(channel);
  36. 65.

    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); });
  37. 66.

    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); });
  38. 67.

    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); });
  39. 71.

    js-csp • Syntax from Go • Operations from Clojure •

    Have disadvantages • Can use transducer from Clojure