我的应用程序中有两个演员。服务和PushSocket。我使用邮箱在两个角色服务和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为了让它工作,我使用了push socket的实例方法,它给出了适当的结果。当我尝试使用定义了池大小的邮箱时,我不确定有什么问题。
发布于 2016-03-14 19:48:42
您直接与参与者的邮箱进行交互,Pool实现会阻止直接访问该邮箱。
,但无论如何你都不应该直接与邮箱交互。
而不是这个:
@pushsocket.mailbox << "test string"执行此操作:
@pushsocket.write("test string")注意:你可能在你的池实现中仍然有一个逻辑错误。当您向套接字参与者写入数据时,您并不知道您正在写入的底层套接字是什么。如果您正在实现某种顺序不可知的管道,其中每个push套接字都连接到一个pull套接字,并且您并不关心哪个套接字参与者实际执行写操作,那么这是很好的。
https://stackoverflow.com/questions/35982064
复制相似问题