Ruby 1.9 Fibers
Upcoming SlideShare
Loading in...5
×
 

Ruby 1.9 Fibers

on

  • 6,649 views

Cooperative Multi-Processing and Elegant Event-driven Programming.

Cooperative Multi-Processing and Elegant Event-driven Programming.

Why Fibers + Events = Ruby FTW.

Statistics

Views

Total Views
6,649
Views on SlideShare
6,648
Embed Views
1

Actions

Likes
11
Downloads
40
Comments
0

1 Embed 1

http://www.techgig.com 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Ruby 1.9 Fibers Ruby 1.9 Fibers Presentation Transcript

  • Ruby 1.9 FibersCooperative Multi-Processing and Elegant Event-Driven Programming Kevin Ball Co-Founder and CTO fashioningchange.com kball@fashioningchange.com http://twitter.com/kbal11
  • Outline• Multi-Processing Overview• Ruby Fibers• Event Driven Programming• Fibers + Events => Ruby FTW
  • Multi-Processing• Processes• Threads• Fibers• Events
  • Processes • Independent (no shared state) • Interact via message passing if at all • Preemptively scheduled • Fairly heavyweight
  • Why Processes?
  • Why Processes?• OS managed
  • Why Processes?• OS managed• Task-level parallelism
  • Why Processes?• OS managed• Task-level parallelism• Great for embarrassingly parallel (IE web)
  • Threads• Shared Data• Fairly lightweight• Also Preemptively Scheduled
  • Why Threading?
  • Why Threading?• Resource Utilization
  • Why Threading?• Resource Utilization• Asynchronous I/O
  • Why Threading?• Resource Utilization• Asynchronous I/O• Shared Data or Tight Coupling
  • Why Threading?• Resource Utilization• Asynchronous I/O• Shared Data or Tight Coupling• Good for fine-grained parallelism
  • Threading Problems
  • Threading Problems
  • Threading Problems
  • Threading Problems • Race conditions • Contention • Deadlocks
  • Fibers
  • Fibers• Pauseable/Resumable Code Blocks
  • Fibers• Pauseable/Resumable Code Blocks• Very lightweight
  • Fibers• Pauseable/Resumable Code Blocks• Very lightweight• Cooperatively scheduled
  • How Lightweight?
  • How Lightweight? Threads Fibers Time to create 1.14 sec 0.103 sec 10,000Memory for 1000 20.6M 3.3M Simultaneous
  • Cooperative Scheduling
  • Cooperative Scheduling • Explicit yielding and resuming
  • Cooperative Scheduling • Explicit yielding and resuming • No races or need for locks
  • Example: Fibonaccifib = Fiber.new do f1 = f2 = 1 loop do Fiber.yield f1 f1, f2 = f2, f1 + f2 endend5.times { puts fib.resume }
  • Example: Fibonaccifib = Fiber.new do f1 = f2 = 1 loop do Fiber.yield f1 f1, f2 = f2, f1 + f2 endend5.times { puts fib.resume }11235
  • Implicit Objectsfib = Fiber.new do f1 = f2 = 1 loop do Fiber.yield f1 f1, f2 = f2, f1 + f2 endend5.times { puts fib.resume }
  • Implicit Objectsfib = Fiber.new do fib = Object.new f1 = f2 = 1 fib.instance_eval do loop do @f1 = @f2 = 1; Fiber.yield f1 def next f1, f2 = f2, f1 + f2 @old = @f1 end @f1, @f2 = @f1, @f2, @f1 + @f2end @old end5.times { puts fib.resume } end 5.times { puts fib.next }
  • Why Bother?
  • Why Bother?Aren’t blocks just implicit functions?
  • Why Bother? Aren’t blocks just implicit functions?Changing the semantics changes the game.
  • Why Bother? Aren’t blocks just implicit functions?Changing the semantics changes the game. Fibers have more flexible entry points
  • SynchronousAsynchronicity
  • SynchronousAsynchronicityHave your asynchronous cake and eat it too!
  • Synchronous AsynchronicityWrite code that looks like puts "Setting up HTTP request #1" data = async_fetch(http://www.google.com/) puts "Fetched page #1: #{data.response_header.status}" *Example from http://www.igvita.com/2009/05/13/fibers-cooperative-scheduling-in-ruby/
  • Synchronous AsynchronicityWrite code that looks like puts "Setting up HTTP request #1" data = async_fetch(http://www.google.com/) puts "Fetched page #1: #{data.response_header.status}"Looks synchronous. Acts asynchronous. *Example from http://www.igvita.com/2009/05/13/fibers-cooperative-scheduling-in-ruby/
  • Events: The Hot New Thing
  • Events: The Hot New Thing • Node.js • Twisted (python) • EventMachine
  • What Is Event-Driven Programming?
  • What Is Event-Driven Programming?
  • What Is Event-Driven Programming?• Age-old Technique
  • What Is Event-Driven Programming?• Age-old Technique• Familiar to UI & Kernel Developers
  • What Is Event-Driven Programming?• Age-old Technique• Familiar to UI & Kernel Developers• Control Flow determined by Events
  • What Is Event-Driven Programming?• Age-old Technique• Familiar to UI & Kernel Developers• Control Flow determined by Events• Callbacks the central construct
  • EventMachine Exampleclass Echo < EventMachine::Connection def post_init send_data Hello end def receive_data(data) p data endendEventMachine.run { EventMachine.connect 127.0.0.1, 8081, Echo}
  • The Downside?
  • The Downside? Deeply Nested Events
  • The Downside? Deeply Nested EventsEventMachine.run { page = EventMachine::HttpRequest.new(http://google.ca/).get page.errback { p "Google is down! terminate?" } page.callback { about = EventMachine::HttpRequest.new(http://google.ca/search?q=eventmachine).get about.callback { # callback nesting, ad infinitum } about.errback { # error-handling code } }} *Example from http://www.igvita.com/2010/03/22/untangling-evented-code-with-ruby-fibers/
  • The Solution? Fibers
  • The Solution? Fibersdef http_get(url) f = Fiber.current http = EventMachine::HttpRequest.new(url).get  # resume fiber once http call is done http.callback { f.resume(http) } http.errback { f.resume(http) }  return Fiber.yieldend *Example from http://www.igvita.com/2010/03/22/untangling-evented-code-with-ruby-fibers/
  • The Solution? Fibersdef http_get(url) f = Fiber.current http = EventMachine::HttpRequest.new(url).get  # resume fiber once http call is done http.callback { f.resume(http) } http.errback { f.resume(http) }  return Fiber.yieldend EventMachine.run do Fiber.new{ page = http_get(http://www.google.com/) puts "Fetched page: #{page.response_header.status}"  page = http_get(http://www.google.com/search?q=eventmachine) puts "Fetched page 2: #{page.response_header.status}" }.resumeend *Example from http://www.igvita.com/2010/03/22/untangling-evented-code-with-ruby-fibers/
  • Still ugly, but...
  • Still ugly, but...• You can wrap it up in a library
  • Still ugly, but...• You can wrap it up in a library• In fact, someone already did
  • Still ugly, but...• You can wrap it up in a library• In fact, someone already did• gem install em-synchrony
  • Em-Synchrony Examplerequire rubygemsrequire eventmachinerequire em-synchronyrequire em-synchrony/em-httpEventMachine.synchrony do page = EventMachine::HttpRequest.new("http://www.google.com").get  p "Look Ma! No callbacks! Fetched page: #{page}" EventMachine.stopend
  • Where is it going?
  • Where is it going?• Rails 3.1 Automatic Flushing
  • Where is it going?• Rails 3.1 Automatic Flushing• Goliath Webserver
  • Where is it going?• Rails 3.1 Automatic Flushing• Goliath Webserver• Async Rails
  • Ruby FTW
  • Ruby FTW • Event-based architectures are winning
  • Ruby FTW • Event-based architectures are winning • Fibers => Events - Drawbacks
  • Ruby FTW • Event-based architectures are winning • Fibers => Events - Drawbacks • WIN!
  • More Resources• http://ruby-doc.org/core-1.9/classes/ Fiber.html• http://www.igvita.com/• https://github.com/eventmachine/ eventmachine
  • Thank You! Kevin Ball Co-Founder and CTOfashioningchange.com kball@fashioningchange.com http://twitter.com/kbal11