在我的 ruby 脚本中,我使用 celluloid-zmq gem。我尝试在轮询器内异步运行评估响应,使用,
async.evaluate_response(socket.read_multipart)
但是,如果我从循环中删除睡眠,不知何故,这不起作用,它没有达到“评估响应”方法。但如果我把 sleep 放在循环中,它就可以完美工作。
require 'celluloid/zmq'
Celluloid::ZMQ.init
module Celluloid
module ZMQ
class Socket
def socket
@socket
end
end
end
end
class Indefinite
include Celluloid::ZMQ
## Readers
attr_reader :dealersock,:pullsock,:pollers
def initialize
prepare_dealersock and prepare_pullsock and prepare_pollers
end
## prepare DEALER SOCK
def prepare_dealersock
@dealersock = DealerSocket.new
@dealersock.identity = "IDENTITY"
@dealersock.connect("tcp://localhost:20482")
end
## prepare PULL SOCK
def prepare_pullsock
@pullsock = PullSocket.new
@pullsock.connect("tcp://localhost:20483")
end
## prepare the Pollers
def prepare_pollers
@pollers = ZMQ::Poller.new
@pollers.register_readable(dealersock.socket)
@pollers.register_readable(pullsock.socket)
end
def run!
loop do
pollers.poll ## this is blocking operation never mind though we need it
pollers.readables.each do |socket|
## we know socket.read_multipart is blocking call this would give celluloid the chance to run other process in mean time.
async.evaluate_response(socket.read_multipart)
end
## If you remove the sleep the async evaluate response would never be executed.
## sleep 0.2
end
end
def evaluate_response(message)
## Hmmm, the code just not reaches over here
puts "got message: #{message}"
...
...
...
...
end
end
## Code is invoked like this
Indefinite.new.run!
知道为什么会发生这种情况吗?
这个问题已经100%改变了,所以我之前的回答没有帮助。
现在,问题是...
ZMQ::Poller
不属于Celluloid::ZMQ
您直接使用ffi-rzmq
绑定,并且不使用Celluloid::ZMQ
包装,它提供套接字的事件化和线程化处理。
最好制作多个参与者——每个套接字一个——或者只使用Celluloid::ZMQ
直接作用于一个演员,而不是破坏它。
你的演员永远没有时间处理回应
这部分使其成为以下内容的副本:
- 红宝石块内的赛璐珞异步不起作用 https://stackoverflow.com/questions/32492560/celluloid-async-inside-ruby-blocks-does-not-work/32569978#32569978
最好的答案是使用after
or every
并不是loop
...这主导了你的演员。
您需要:
- Move
evaluate_response
给另一位演员。
- 将每个套接字移至其自己的 actor。
该代码需要分解为多个参与者才能正常工作,其中一个主要sleep
在程序结束时。但在此之前,请尝试使用after
or every
代替loop
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)