Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

Ruby Fibers: 8 Useful Reads On Ruby’s New Concurrency Feature

By Peter Cooper / May 13, 2009

fibers.pngNew to Ruby 1.9 is the concept of fibers. Fibers are light-weight (green) threads with manual, cooperative scheduling, rather than the preemptive scheduling of Ruby 1.8's threads. Since Ruby 1.9's threads exist at the system level, fibers are, in a way, Ruby 1.9's answer to Ruby 1.8's green threads, but lacking the pre-emptive scheduling.

Now that Ruby 1.9 is becoming more important with each passing month, I've rounded up some of the best articles about Ruby 1.9's fiber functionality so that you can be up to speed with all of the major concepts in perhaps 10 to 15 minutes. If you're getting interested in Ruby 1.9, you'll want to read on:

Fibers & Cooperative Scheduling in Ruby - Ilya Grigorik hits it out of the ballpark with perhaps the best overview of fibers. Includes a snazzy diagram, a code example, and a look at how JRuby and Rubinius compare. If you read just one article, make it this one.

Pipelines Using Fibers in Ruby 1.9 - Dave Thomas gives one of his typically awesome code walkthroughs by showing how fibers can be used to emulate a UNIX-esque pipeline of operations. There's even a second installment.

Ruby 1.9 Fiber documentation - The actual Ruby 1.9 documentation for the Fiber class. There's not any depth but it's a reasonable introduction with a couple of simple examples.

Ruby 1.9 adds Fibers for lightweight concurrency - A summary post about fibers by Werner Schuster of InfoQ. It's two years old (back when it wasn't even 100% sure whether fibers would end up in 1.9 or not) but features a very simple example and features some information on fibers taken from both MSDN and Sasada Koichi (the YARV guy).

Ruby Fibers vs Ruby Threads - Muhammed Ali presents some benchmarks showing how fibers are much "lighter" than threads in terms of creation time and memory usage. Notably, Muhammed ran against a limit of 3070 threads whereas 100,000 fibers weren't a problem (except for crazy levels of memory usage).

Fibers implemented for Ruby 1.8.6/7 - Aman Gupta and Joe Damato quickly earned their code master badges with a set of patches for Ruby 1.8.6 and 1.8.7 that implemented fibers on those Ruby interpreters. I haven't tried it myself, but it looks like an impressive piece of work nonetheless.

Ruby Fiber Ring Benchmark - An implementation of the Erlang "ring" benchmark in Ruby. Considering Ruby and Erlang's relative differences, Ruby holds up pretty well in the benchmark. The code may also be of interest to you.

Ruby Concurrency with Actors - Pat Eyler takes a look at fibers and their use in implementing actor systems, principally with RevActor.

Still new to the Ruby 1.9 bandwagon? Check out our 23 Useful Ruby 1.9 Links and Resources post to whet your appetite.

Comments

  1. TPReal says:

    Good read, most of those. Another article with examples of Fiber usage: http://al2o3-cr.blogspot.com/2009/02/fiber.html

  2. Andy says:

    Didn't read the article yet, but the picture did remind me to pick up some Fiber Choice next time I'm grocery shopping. :)

  3. Peter Cooper says:

    I'm a big fan of Fiber One, lol :)

Other Posts to Enjoy

Twitter Mentions