Running code asynchronously inside pollers
Asked Answered
A

1

3

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) 

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!

Any idea why this is happening?

Astrology answered 9/9, 2015 at 12:42 Comment(3)
Why do you define Celluloid::ZMQ::Socket.socket at all?Deianira
I have updated question with code.Astrology
I updated my answer.Deianira
D
1

The question was 100% changed, so my previous answer does not help. Now, the issues are...

ZMQ::Poller is not part of 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).

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.

Your actor never gets time to work with the response

This part makes it a duplicate of:

The best answer is to use after or every and not loop ... which is dominating your actor.

You need to either:

  • Move evaluate_response to another actor.
  • Move each socket to their own actor.

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.

Deianira answered 11/9, 2015 at 13:10 Comment(4)
actually I cant go with after or every, as i want to process things as soon as i get them on PULL or DEALER socket. and thats the reason why i want to remove sleep from loop.Astrology
Then you need two actors.Deianira
@Deianira I just don't get this you mention you are directly using the ffi-rzmq yes that is true but If I follow the celluloid-zeromq code problem, I just don't see any difference in both of them.Browbeat
Using the poller outside Celluloid::ZMQ avoids the reactor. But the real problem here is that you need to be using multiple actors for what you are trying to do. Is there a reason to avoid using multiple actors?Deianira

© 2022 - 2024 — McMap. All rights reserved.