Skip to content

Instantly share code, notes, and snippets.

@aprescott
Created May 13, 2011 18:06
Show Gist options
  • Save aprescott/971008 to your computer and use it in GitHub Desktop.
Save aprescott/971008 to your computer and use it in GitHub Desktop.
Simple example of concurrency in Ruby with Fiber.
require "fiber"
f1 = Fiber.new { |f2| f2.resume Fiber.current; while true; puts "A"; f2.transfer; end }
f2 = Fiber.new { |f1| f1.transfer; while true; puts "B"; f1.transfer; end }
f1.resume f2
Copy link

ghost commented May 13, 2011

require "fiber"

f1 = Fiber.new { |f2| f2.resume Fiber.current; while true; puts "A"; f2.transfer; end }
f2 = Fiber.new { |f1| f1.transfer; while true; puts "B"; f1.transfer; end }

f1.resume f2

@aprescott
Copy link
Author

A-ha! That was really pretty obvious, I guess, having re-read the docs. Too busy thinking about Fibers and not enough focus elsewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment