Reputation: 31
How can I get the current amount of open Fibers in a ruby
application? My app uses EventMachine Synchrony lib for concurrency handling. While googling, I've not found any API which would return it.
For example if I have this piece of code:
EM::Synchrony::FiberIterator.new(@outputs, @outputs.size).each do |output|
# some code goes here
end
then how can I know how many fibers are there running? Without manually using counter++
and of course without @some_arr.size
.
Upvotes: 1
Views: 223
Reputation: 31
Ok, so 1 solution was found for this issue:
You can count number of existing objects (of all types) in ruby.
In our case we want to count the objects of type/class 'Fiber', so we use ObjectSpace.each_object
ruby API:
stats = {}
stats["Fiber"] = 0
ObjectSpace.each_object(Fiber) {|o| stats["Fiber"] += 1 if o.alive?}
(we are interested only in alive Fibers, that's why using alive?
API)
ObjectSpace for reference: https://ruby-doc.org/core-1.9.3/ObjectSpace.html
Fiber for reference: https://ruby-doc.org/core-1.9.3/Fiber.html
Upvotes: 2