了解赛璐珞池

时间:2016-03-10 20:22:26

标签: ruby celluloid

我想我对赛璐珞池的理解有点破碎。我将尝试在下面解释,但在此之前快速说明。

注意:我们的系统正在通过ZeroMQ传递非常fast client个消息。

使用以下Vanilla Celluloid应用程序

class VanillaClient
   include Celluloid::ZMQ

   def read
      loop { async.evaluate_response(socket.read_multipart)
   end

   def evaluate_response(data)
      ## the reason for using defer can be found over here.

      Celluloid.defer do 
        ExternalService.execute(data)
      end
   end
end

我们的系统在一段时间后导致失败,原因'Can't spawn more thread'(或类似的东西)

所以我们打算使用Celluloid Pool(以避免上述问题),以便我们可以限制产生的线程数

我对赛璐珞池的理解是 赛璐珞池为您维护一个演员池,以便您可以并行分配任务。

因此,我决定测试它,但根据我的测试用例,它似乎是连续的行为(即事物永远不会分布或并行发生。)

复制此内容的示例。

sender-1.rb

## Send message `1` to the the_client.rb

sender-2.rb

## Send message `2` to the the_client.rb

the_client.rb

## take message from sender-1 and sender-2 and return it back to receiver.rb
## heads on, the `sleep` is introduced to test/replicate the IO block that happens in the actual code.

receiver.rb

## print the message obtained from the_client.rb

如果sender-2.rbsender-1.rb之前运行,则表示该池在20秒内被阻止(the_client.rb中的休眠时间,可以在{{3}上看到在使用sender-1.rb

发送的数据之前

在ruby-2.2.2和jRuby-9.0.5.0下的行为相同。 Pool可能会以这种方式行事的原因是什么?

1 个答案:

答案 0 :(得分:0)

您的pool来电不是异步的。

evaluate@pool的执行需要.async,就像在原始示例中一样,不使用池。你仍然想要异步行为,但你也想拥有多个处理程序角色。

接下来您可能会遇到Pool.async错误。

这意味着在5点击evaluate后,您的池将无响应,直到池中至少有一个actor完成。最糟糕的情况是,如果快速连续收到6+个请求,则6th将花费120秒,因为它将在执行前5*20秒,然后20秒1}}秒执行自己。

根据您的实际操作导致延误,您可能需要调整游泳池大小。