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

Like this? Share it with your network

Share

Ruby 1.9 Fibers

on

  • 6,866 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,866
Views on SlideShare
6,865
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 Presentation Transcript

  • 1. 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
  • 2. Outline• Multi-Processing Overview• Ruby Fibers• Event Driven Programming• Fibers + Events => Ruby FTW
  • 3. Multi-Processing• Processes• Threads• Fibers• Events
  • 4. Processes • Independent (no shared state) • Interact via message passing if at all • Preemptively scheduled • Fairly heavyweight
  • 5. Why Processes?
  • 6. Why Processes?• OS managed
  • 7. Why Processes?• OS managed• Task-level parallelism
  • 8. Why Processes?• OS managed• Task-level parallelism• Great for embarrassingly parallel (IE web)
  • 9. Threads• Shared Data• Fairly lightweight• Also Preemptively Scheduled
  • 10. Why Threading?
  • 11. Why Threading?• Resource Utilization
  • 12. Why Threading?• Resource Utilization• Asynchronous I/O
  • 13. Why Threading?• Resource Utilization• Asynchronous I/O• Shared Data or Tight Coupling
  • 14. Why Threading?• Resource Utilization• Asynchronous I/O• Shared Data or Tight Coupling• Good for fine-grained parallelism
  • 15. Threading Problems
  • 16. Threading Problems
  • 17. Threading Problems
  • 18. Threading Problems • Race conditions • Contention • Deadlocks
  • 19. Fibers
  • 20. Fibers• Pauseable/Resumable Code Blocks
  • 21. Fibers• Pauseable/Resumable Code Blocks• Very lightweight
  • 22. Fibers• Pauseable/Resumable Code Blocks• Very lightweight• Cooperatively scheduled
  • 23. How Lightweight?
  • 24. How Lightweight? Threads Fibers Time to create 1.14 sec 0.103 sec 10,000Memory for 1000 20.6M 3.3M Simultaneous
  • 25. Cooperative Scheduling
  • 26. Cooperative Scheduling • Explicit yielding and resuming
  • 27. Cooperative Scheduling • Explicit yielding and resuming • No races or need for locks
  • 28. Example: Fibonaccifib = Fiber.new do f1 = f2 = 1 loop do Fiber.yield f1 f1, f2 = f2, f1 + f2 endend5.times { puts fib.resume }
  • 29. Example: Fibonaccifib = Fiber.new do f1 = f2 = 1 loop do Fiber.yield f1 f1, f2 = f2, f1 + f2 endend5.times { puts fib.resume }11235
  • 30. Implicit Objectsfib = Fiber.new do f1 = f2 = 1 loop do Fiber.yield f1 f1, f2 = f2, f1 + f2 endend5.times { puts fib.resume }
  • 31. 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 }
  • 32. Why Bother?
  • 33. Why Bother?Aren’t blocks just implicit functions?
  • 34. Why Bother? Aren’t blocks just implicit functions?Changing the semantics changes the game.
  • 35. Why Bother? Aren’t blocks just implicit functions?Changing the semantics changes the game. Fibers have more flexible entry points
  • 36. SynchronousAsynchronicity
  • 37. SynchronousAsynchronicityHave your asynchronous cake and eat it too!
  • 38. 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/
  • 39. 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/
  • 40. Events: The Hot New Thing
  • 41. Events: The Hot New Thing • Node.js • Twisted (python) • EventMachine
  • 42. What Is Event-Driven Programming?
  • 43. What Is Event-Driven Programming?
  • 44. What Is Event-Driven Programming?• Age-old Technique
  • 45. What Is Event-Driven Programming?• Age-old Technique• Familiar to UI & Kernel Developers
  • 46. What Is Event-Driven Programming?• Age-old Technique• Familiar to UI & Kernel Developers• Control Flow determined by Events
  • 47. What Is Event-Driven Programming?• Age-old Technique• Familiar to UI & Kernel Developers• Control Flow determined by Events• Callbacks the central construct
  • 48. 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}
  • 49. The Downside?
  • 50. The Downside? Deeply Nested Events
  • 51. 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/
  • 52. The Solution? Fibers
  • 53. 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/
  • 54. 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/
  • 55. Still ugly, but...
  • 56. Still ugly, but...• You can wrap it up in a library
  • 57. Still ugly, but...• You can wrap it up in a library• In fact, someone already did
  • 58. Still ugly, but...• You can wrap it up in a library• In fact, someone already did• gem install em-synchrony
  • 59. 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
  • 60. Where is it going?
  • 61. Where is it going?• Rails 3.1 Automatic Flushing
  • 62. Where is it going?• Rails 3.1 Automatic Flushing• Goliath Webserver
  • 63. Where is it going?• Rails 3.1 Automatic Flushing• Goliath Webserver• Async Rails
  • 64. Ruby FTW
  • 65. Ruby FTW • Event-based architectures are winning
  • 66. Ruby FTW • Event-based architectures are winning • Fibers => Events - Drawbacks
  • 67. Ruby FTW • Event-based architectures are winning • Fibers => Events - Drawbacks • WIN!
  • 68. More Resources• http://ruby-doc.org/core-1.9/classes/ Fiber.html• http://www.igvita.com/• https://github.com/eventmachine/ eventmachine
  • 69. Thank You! Kevin Ball Co-Founder and CTOfashioningchange.com kball@fashioningchange.com http://twitter.com/kbal11