在轮询器中异步运行代码 [英] Running code asynchronously inside pollers
问题描述
在我的 ruby 脚本中,我使用的是 celluloid-zmq gem.我试图在轮询器中异步运行评估响应,
In my ruby script,I am using celluloid-zmq gem. where I am trying to run evaluate_response asynchronously inside pollers using,
async.evaluate_response(socket.read_multipart)
但是如果我从循环中删除睡眠,不知何故那是行不通的,它不会达到evaluate_response"方法.但是如果我将 sleep 放入循环中,它就可以完美运行.
But if I remove sleep from loop, somehow thats not working out, It is not reaching to "evaluate_response" method. But if I put sleep inside loop it works perfectly.
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% 更改,因此我之前的回答没有帮助.现在,问题是...
The question was 100% changed, so my previous answer does not help. Now, the issues are...
您直接使用了 ffi-rzmq
绑定,而不是使用 Celluloid::ZMQ
包装,它提供了事件 &套接字的线程处理.
You are directly using the ffi-rzmq
bindings, and not using the Celluloid::ZMQ
wrapping, which provides evented & threaded handling of the socket(s).
最好制作多个actor——每个socket一个——或者直接在一个actor中使用Celluloid::ZMQ
,而不是破坏它.
It would be best to make multiple actors -- one per socket -- or to just use Celluloid::ZMQ
directly in one actor, rather than undermining it.
这部分使它与以下内容重复:
This part makes it a duplicate of:
最好的答案是使用 after
或 every
而不是 loop
......这是支配你的演员.
The best answer is to use
after
orevery
and notloop
... which is dominating your actor.
您需要:
- 将
evaluate_response
移动到另一个参与者. - 将每个插槽移动到自己的角色.
- Move
evaluate_response
to another actor. - Move each socket to their own actor.
这段代码需要分解成几个actor才能正常工作,程序末尾有一个主要的sleep
.但在此之前,请尝试使用 after
或 every
而不是 loop
.
This code needs to be broken up into several actors to work properly, with a main sleep
at the end of the program. But before all that, try using after
or every
instead of loop
.
这篇关于在轮询器中异步运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!