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

Fiber and em-synchrony

0483666667da0ede142e86a3cc190d22?s=47 代码之力
September 24, 2013

Fiber and em-synchrony

Introduce Fiber in Ruby and demonstrate how to run Rails application using em-synchrony to utilize asynchronous councurrency



September 24, 2013


  1. Fiber and Em-synchrony @ShiningRay

  2. Fiber Fibers are primitives for implementing light weight cooperative concurrency

    in Ruby. Add to Ruby core in 1.9
  3. Fiber == Coroutine

  4. Fiber vs Thread vs Process • Process simulates CPU and

    Memory, Preemptive • Thread simulates CPU, Preemptive • Fiber: none, cooperative, manually schedule
  5. Coroutine vs Routine • Routine: return only once • Coroutine:

    can return(yield) multiple times
  6. Benefit of Fibers • Small & lightweight: much smaller than

    thread/process, so we can spawn more. • Can be schedule only when necessary, reduced context-switch, lighten the burden of CPU. • So can handle higher concurrency than per- thread or per-process mode.
  7. PS: Coroutines can be implemented by call/cc.

  8. Warning: Fiber can also lead to concurrent problem like race

  9. Em-Synchrony = Fiber + EventMachine

  10. Em-Synchrony: Good Parts • High Concurrency of evented model •

    Ease of sequential programming, no pain of callbacks https://github.com/igrigorik/em-synchrony
  11. Basic Idea • Untangling Evented Code with Ruby Fibers •

    http://www.igvita.com/2010/03/22/untangling-eve nted-code-with-ruby-fibers/
  12. Basic Idea • Use Fiber to save the context after

    sending a asynchronous request. • Switch back to parent Fiber(for scheduling other fibers) • Resume the saved Fiber after asynchonous request completes.
  13. None
  14. More • Fiber aware ConnectionPool with sync/async query support •

    Fiber aware Iterator to allow concurrency control & mixing of sync / async • Fiber aware async inline support: turns any async function into sync • Fiber aware Multi-request interface for any callback enabled clients • Fiber aware TCPSocket replacement, powered by EventMachine • Fiber aware Thread, Mutex, ConditionVariable clases • Fiber aware sleep, defer, system
  15. High-concurrency Framework using em-synchrony • Goliath • Sinatra-synchrony

  16. Em-Synchrony: The Bad Parts • You must use a customized

    fiber aware driver for asynchronous tasks. (em-mysql em-redis ...)
  17. Suitable for Web Service APIs

  18. Asynchronous Rails

  19. You need • Em-synchrony • Rack-fiber_pool • Config.threadsafe! • Thin(evented

    server) • Fiber aware driver for (db/http/service) connections
  20. Gemfile

  21. Config.ru

  22. Application.rb

  23. Database.yml

  24. Use Ruby 2.0 Ruby 1.9 4k Fiber stack size. Too

    small to fit common Rails app. Ruby 2.0 has 200k default Fiber stack size. And you can change it via:
  25. Thank you