Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing mailbox for actor initialised through pool in Celluloid

Tags:

ruby

celluloid

I have 2 actors in my app. Service and PushSocket. I am using mailbox for communication between two actors Service and PushSocket. It works well when I just create single instance on PushSocket and add message to it's mailbox.

**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

But when try same with pool it doesn't work as expected. I do not receive any message in push socket.

**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

To get this working, I am using instance method of push socket which gives proper results. Not sure whats problem when I try to use mailbox with pool size defined.

like image 442
Pandurang Waghulde Avatar asked Dec 05 '25 09:12

Pandurang Waghulde


1 Answers

You are directly interacting with the mailbox of an actor, which Pool implementations block direct access to.

But you shouldn't be directly interacting with the mailbox anyway.

Instead of this:

@pushsocket.mailbox << "test string"

Do this:

@pushsocket.write("test string")

NOTE: You still might have a logic error in your implementation of pools. When you write to a socket actor, you have no idea what underlying socket you're writing to. That is fine, if you are implementing some sort of sequence-agnostic pipeline, where each push socket connects to a single pull socket, and you don't care which socket actor actually performs the write operation.

like image 186
digitalextremist Avatar answered Dec 07 '25 06:12

digitalextremist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!