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

Fiber and em-synchrony

代码之力
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
Tweet

More Decks by 代码之力

Other Decks in Programming

Transcript

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

    Memory, Preemptive • Thread simulates CPU, Preemptive • Fiber: none, cooperative, manually schedule
  2. 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.
  3. Em-Synchrony: Good Parts • High Concurrency of evented model •

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

    http://www.igvita.com/2010/03/22/untangling-eve nted-code-with-ruby-fibers/
  5. 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.
  6. 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
  7. Em-Synchrony: The Bad Parts • You must use a customized

    fiber aware driver for asynchronous tasks. (em-mysql em-redis ...)
  8. You need • Em-synchrony • Rack-fiber_pool • Config.threadsafe! • Thin(evented

    server) • Fiber aware driver for (db/http/service) connections
  9. 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: