tags:

views:

124

answers:

3

I'm trying to do something with "neverblock" and I can't seem to get it to work.
What I expect: The blocking sleep shouldn't slow down the whole process. I expected to see 5 times "bla" with basically the same timestamp.

Ruby:

$ ruby --version
ruby 1.9.2dev (2010-03-31 trunk 27123) [x86_64-darwin10.2.0]

Code:

require "neverblock"
fiber_pool = NeverBlock::Pool::FiberPool.new(25)

5.times do
fiber_pool.spawn do
  puts "bla @ #{Time.now}"
  sleep 1
end
end

Result:

$ ruby test.rb 
Using Neverblock
bla @ 2010-04-07 11:00:28 -0400
bla @ 2010-04-07 11:00:29 -0400
bla @ 2010-04-07 11:00:30 -0400
bla @ 2010-04-07 11:00:31 -0400
bla @ 2010-04-07 11:00:32 -0400
A: 

you need to "freeze" your time.

Time.now will always execute itself when you are calling it

time = Time.now
5.times do
  fiber_pool.spawn do
    puts "bla @ #{time}"
    sleep 1
  end
end
fl00r
that was the idea... I want to see when the fibers are actually executed. As I understood, they should be basically executed in parallel (non-blocking) which they don't seem to be
Marc Seeger
I've tried few test with neverblock - no results. ruby 1.8.6
fl00r
A: 

http://ruby-doc.org/core-1.9/classes/Fiber.html

Fibers are scheduled using suspend and resume - since you don't use either in this example, the code will run sequentially. NeverBlock, as I understand it, makes use of the FiberPool in conjunction with Fiber- and event-aware database drivers to intelligently suspend and resume Fibers as necessary to deal with I/O events.

Greg Campbell
If that is true, I don't understand the example provided by the author in this article: http://oldmoe.blogspot.com/2008/11/ruby-networking-on-steroids.html ("Second we will create a client that will issue requests to the server")
Marc Seeger
That example may be out of date - when I ran it (after making a few tweaks, as some of the NeverBlock API has changed), it executed the requests sequentially.
Greg Campbell
A: 

Do this:

require "neverblock"
require "eventmachine"

fiber_pool = NeverBlock::Pool::FiberPool.new(5)

EM::run do
  5.times do
    fiber_pool.spawn do
      puts "bla @ #{Time.now}"
      EM.add_timer(1) do
        EM.stop
      end
    end
  end
end

And it should work!

vishnugopal