Skip to content

Instantly share code, notes, and snippets.

@igrigorik
Created March 16, 2010 04:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igrigorik/333657 to your computer and use it in GitHub Desktop.
Save igrigorik/333657 to your computer and use it in GitHub Desktop.
# example for multi-fiber workers
require "fiber"
require "eventmachine"
results = []
work = [:a, :b, :c]
EventMachine.run {
Fiber.new do
f = Fiber.current
need = work.size
work.each do |unit|
Fiber.new do
# simulate delay in query, etc
EM.add_timer(0.5) do
results << unit
need -= 1
p [:current, results]
if need.zero?
p [:finished, results]
f.transfer
end
end
end.resume
end
Fiber.yield
p [:done, results]
EventMachine.stop
end.resume
}
# [:current, [:a]]
# [:current, [:a, :b]]
# [:current, [:a, :b, :c]]
# [:finished, [:a, :b, :c]]
# [:done, [:a, :b, :c]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment