3

我的应用中有 2 个演员。服务和 PushSocket。我正在使用邮箱在两个演员 Service 和 PushSocket 之间进行通信。当我在 PushSocket 上创建单个实例并将消息添加到它的邮箱时,它运行良好。

**File: service.rb**

Celluloid::ZMQ.init

class Service
  include Celluloid::ZMQ
  attr_accessor :pushsocket

  def initialize
    initialize_pushsock_actor
    send_messages
  end

  def initialize_pushsock_actor
    @pushsocket = PushSocket.new
  end

  def send_messages
    10.times do
      puts 'sending data'
      @pushsocket.mailbox << 'test'
    end
  end
end

**File: push_socket.rb**

Celluloid::ZMQ.init
class PushSocket
  include Celluloid::ZMQ

  def initialize
    async.wait_for_my_messages
  end

  def wait_for_my_messages
    loop do
      message = receive { |msg| msg }
      puts "Got a Message: #{message.inspect}"
    end
  end
end

但是当尝试与池相同时,它不会按预期工作。我在推送套接字中没有收到任何消息。

**File: service.rb**

Celluloid::ZMQ.init
class Service
  include Celluloid::ZMQ
  attr_accessor :pushsocket

  def initialize
    initialize_pushsock_actor
    send_messages
  end

  def initialize_pushsock_actor
    @pushsocket = PushSocket.pool(size: 10)
  end

  def send_messages
    10.times do
      puts 'sending data'
      @pushsocket.mailbox << 'test'
    end
  end
end


**File: push_socket.rb**

Celluloid::ZMQ.init
class PushSocket
  include Celluloid::ZMQ

  def initialize
    async.wait_for_my_messages
  end

  def wait_for_my_messages
    loop do
      message = receive { |msg| msg }
      puts "Got a Message: #{message.inspect}"
    end
  end
end

为了让这个工作,我使用了推送套接字的实例方法,它给出了正确的结果。当我尝试使用定义了池大小的邮箱时,不确定有什么问题。

4

1 回答 1

1

您正在直接与参与者的邮箱进行交互,Pool实现阻止直接访问。

但无论如何,您不应该直接与邮箱交互。

而不是这个:

@pushsocket.mailbox << "test string"

做这个:

@pushsocket.write("test string")

注意:您在池的实现中仍然可能存在逻辑错误。当你写一个套接字actor时,你不知道你正在写什么底层的套接字。这很好,如果您正在实现某种与序列无关的管道,其中每个push套接字连接到单个pull套接字,并且您不在乎哪个套接字参与者实际执行写操作。

于 2016-03-14T11:48:42.360 回答