rubyasynchronouscelluloid

Celluloid resize pool


I have the following program structure.

client = Client.new
params = client.get_params
pool = client.pool(size: params.size)

futures = params.map do |p|
    pool.future(:perform_work, p)
end

futures.map(&:value)

Client is Celluloid-enabled class using include Celluloid. This works great until I try to execute the program in a loop. I need to dynamically resize pool of workers based on number of parameters I receive from external data-feed.

client = Client.new
pool = client.pool(size: 1)

loop do
   params = client.get_params

   ....
   **? pool.resize(size: params.size) ?**
   ....

   futures = params.map do |p|
      pool.future(:perform_work, p)
   end

   futures.map(&:value)
   sleep 1
end

I tried include pool creation into the loop with subsequent pool.terminate but it's spamming threads and leads to actor crash.


Solution

  • Setting pool.size explicitly did the trick it seems

    client = Client.new
    pool = client.pool(size: 1)
    
    loop do
       params = client.get_params
       pool.size = params.size
    
       futures = params.map do |p|
          pool.future(:perform_work, p)
       end
    
       futures.map(&:value)
       sleep 1
    end