Created
February 11, 2009 22:59
-
-
Save tmm1/62335 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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