Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
Ruby vs Node EventMachine & Fiber ShiningRay  曹力 2011.6.18
Reactor 模式
Reactor 模式框架 <ul><li>Node.js </li></ul><ul><li>Gevent, Twisted, Tornado (Python) </li></ul><ul><li>EventMachine, Rev (Ruby...
Why Ruby? Node  我不会
EventMachine <ul><li>Scalable and performant but low-level I/O library </li></ul><ul><li>Built on ”libevent” </li></ul><ul...
Use EventMachine <ul><li>EventMachine::run do </li></ul><ul><li>… </li></ul><ul><li>… </li></ul><ul><li>… </li></ul><ul><l...
Echo Server Callback Module
Echo Server in node
Low Level HTTP Client
EM::HttpRequest
Callbacks Nightmare
Continuation-Passing Style Too Many Callbacks
Contiunation ->  Coroutine(Fiber)
Ruby 1.9 Fibers <ul><li>Ruby 1.9 Fibers  are a means of creating code blocks which can be paused and resumed by our applic...
Original CPS
Untangling Evented Code with Fibers 1. Wrap in a continuation 2. Pause Continuation 3. Resume when succeed
Em-synchrony
Em-synchrony   simple evented programming <ul><li>Fiber aware connection pool with sync/async query support </li></ul><ul>...
Use em-synchrony
Multiple asychronous tasks
Why Coroutine?
ENIAC ->  冯诺依曼结构  并行-> 串行
顺序编程更符合人类思维模式
Who cares?
Why we choose node.js? Performance Simplicity
不同语言的并发机制比较 任务单元 信息传递交互机制 多核支持 Node.js Callback Callback Multi-node Cluster Ruby Fiber(stdlib) ... JRuby Erlang process me...
2/8 Principles
2/8 Principles <ul><li>20% codes take 80% time </li></ul><ul><li>20% codes really needs asynchronous 80% codes only needs ...
Don't be afraid of using Coroutines
Node-fiber <ul><li>https://github.com/laverdet/node-fibers/ </li></ul>
Nächste SlideShare
Wird geladen in …5
×

Ruby vs Node ShiningRay Shanghai

4.107 Aufrufe

Veröffentlicht am

@ShiningRay Ruby vs. Node 2011-06-18

Veröffentlicht in: Technologie, Bildung
  • Als Erste(r) kommentieren

Ruby vs Node ShiningRay Shanghai

  1. 1. Ruby vs Node EventMachine & Fiber ShiningRay 曹力 2011.6.18
  2. 2. Reactor 模式
  3. 3. Reactor 模式框架 <ul><li>Node.js </li></ul><ul><li>Gevent, Twisted, Tornado (Python) </li></ul><ul><li>EventMachine, Rev (Ruby) </li></ul><ul><li>And many more </li></ul>
  4. 4. Why Ruby? Node 我不会
  5. 5. EventMachine <ul><li>Scalable and performant but low-level I/O library </li></ul><ul><li>Built on ”libevent” </li></ul><ul><li>API that removes threaded complexety </li></ul><ul><li>Works with Ruby, Rubinius, and JRuby </li></ul>
  6. 6. Use EventMachine <ul><li>EventMachine::run do </li></ul><ul><li>… </li></ul><ul><li>… </li></ul><ul><li>… </li></ul><ul><li>end </li></ul>Start Reactor Run Your Code until EM::stop is called
  7. 7. Echo Server Callback Module
  8. 8. Echo Server in node
  9. 9. Low Level HTTP Client
  10. 10. EM::HttpRequest
  11. 11. Callbacks Nightmare
  12. 12. Continuation-Passing Style Too Many Callbacks
  13. 13. Contiunation -> Coroutine(Fiber)
  14. 14. Ruby 1.9 Fibers <ul><li>Ruby 1.9 Fibers are a means of creating code blocks which can be paused and resumed by our application (think lightweight threads, minus the thread scheduler and less overhead). </li></ul>f = Fiber . new { while true do Fiber .yield &quot;Hi” end } p f.resume # => Hi p f.resume # => Hi p f.resume # => Hi Manual / cooperative scheduling!
  15. 15. Original CPS
  16. 16. Untangling Evented Code with Fibers 1. Wrap in a continuation 2. Pause Continuation 3. Resume when succeed
  17. 17. Em-synchrony
  18. 18. Em-synchrony simple evented programming <ul><li>Fiber aware connection pool with sync/async query support </li></ul><ul><li>Multi request interface which accepts any callback enabled client </li></ul><ul><li>Fibered iterator to allow concurrency control & mixing of sync / async </li></ul><ul><li>em-http-request: .get, etc are synchronous, while .aget, etc are async </li></ul><ul><li>em-mysqlplus: .query is synchronous, while .aquery is async </li></ul><ul><li>remcached: .get, etc, and .multi_* methods are synchronous </li></ul>
  19. 19. Use em-synchrony
  20. 20. Multiple asychronous tasks
  21. 21. Why Coroutine?
  22. 22. ENIAC -> 冯诺依曼结构 并行-> 串行
  23. 23. 顺序编程更符合人类思维模式
  24. 24. Who cares?
  25. 25. Why we choose node.js? Performance Simplicity
  26. 26. 不同语言的并发机制比较 任务单元 信息传递交互机制 多核支持 Node.js Callback Callback Multi-node Cluster Ruby Fiber(stdlib) ... JRuby Erlang process message 内置 自动 Go goroutine channel 内置 自动 Scala actor message 内置 自动 Python Coroutine(stdlib) Greenlets(3rdparty) ... Multi-processing(stdlib) Stackless Python Microthreads Channel Lua Coroutine
  27. 27. 2/8 Principles
  28. 28. 2/8 Principles <ul><li>20% codes take 80% time </li></ul><ul><li>20% codes really needs asynchronous 80% codes only needs syncrhonous </li></ul><ul><li>20% gurus take care of asynchronous codes, 80% guys just write synchronous codes </li></ul>
  29. 29. Don't be afraid of using Coroutines
  30. 30. Node-fiber <ul><li>https://github.com/laverdet/node-fibers/ </li></ul>

×