2015-09-09 17 views
3

在我的ruby脚本中,我使用了celluloid-zmq gem。这里我想里面使用轮询evaluate_response异步运行,在轮询器内部异步运行代码

async.evaluate_response(socket.read_multipart) 

但是,如果我从环去除睡眠,不知何故那不是工作了,它不伸手“evaluate_response”的方法。但是如果我把睡眠放在循环内,它可以完美地工作

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! 

任何想法为什么会发生这种情况?

+1

为什么你根本定义'Celluloid :: ZMQ :: Socket.socket'? – digitalextremist

+0

我用代码更新了问题。 –

+0

我更新了我的答案。 – digitalextremist

回答

1

问题是100%改变,所以我以前的答案没有帮助。 现在,问题是......

ZMQ::Poller不是Celluloid::ZMQ

您直接使用ffi-rzmq绑定,而不是使用Celluloid::ZMQ包装,它提供了插座的事件触发&线程处理部(S) 。

最好是让多个角色 - 每个角色一个 - 或者直接在一个角色中使用Celluloid::ZMQ,而不是破坏它。

你演员从来没有得到时间响应

这部分使得它的重复工作:

最好的答案就是使用afterevery而不是loop ......这是主宰你的演员。

您需要:

  • 移动evaluate_response到另一个演员。
  • 将每个套接字移到他们自己的actor上。

这段代码需要分解成几个角色才能正常工作,在程序结束时主要为sleep。但在此之前,请尝试使用afterevery而不是loop

+0

其实我不能跟随或每一个,因为我想处理的东西,只要我拿到他们在拉或经销商插座。这就是为什么我想从循环中删除睡眠的原因。 –

+0

然后你需要两个演员。 – digitalextremist

+0

@digitalextremist我只是没有得到这个你提到'你直接使用ffi-rzmq'是的,这是真的,但如果我遵循'celluloid-zeromq'代码问题,我只是没有看到任何区别在两个他们。 – Viren