Skip to content

Instantly share code, notes, and snippets.

@tmm1
Created February 11, 2009 22:59
Show Gist options
  • Select an option

  • Save tmm1/62335 to your computer and use it in GitHub Desktop.

Select an option

Save tmm1/62335 to your computer and use it in GitHub Desktop.
class FiberedQueue
def initialize
@queue = []
@busy = false
end
def busy?
!!@busy
end
def push &blk
@queue << blk
unless busy?
begin
@busy = Fiber.current
while b = @queue.shift
b.call
end
ensure
@busy = false
end
end
end
end
if __FILE__ == $0
require 'fiber'
require 'neverblock'
require 'eventmachine'
def sleep n
f = Fiber.current
EM.add_timer(n){ f.resume }
Fiber.yield
end
pool = NB::Pool::FiberPool.new(5)
queue = FiberedQueue.new
EM.run{
15.times do
pool.spawn{
queue.push{
# queue.push{
puts 'hi'; sleep 0.5; puts 'bye'
# }
}
}
end
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment