Ruby 1.9 Fibers

7,336
-1

Published on

Cooperative Multi-Processing and Elegant Event-driven Programming.

Why Fibers + Events = Ruby FTW.

Published in: Technology, Business
0 Comments
13 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
7,336
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
49
Comments
0
Likes
13
Embeds 0
No embeds

No notes for slide
  • \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

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

      Clipping is a handy way to collect important slides you want to go back to later.

    ×