-
-
Notifications
You must be signed in to change notification settings - Fork 272
Protocol Interaction
NOTE: This is an advanced feature which is difficult to use correctly. We would recommend avoiding it.
The asynchronous message protocol Celluloid uses can be used directly to add new behavior to actors.
To send a raw asynchronous message to an actor, use Celluloid::Mailbox#<<:
actor.mailbox << MyMessage.new
Methods can wait on incoming MyMessage objects using the Celluloid#receive method:
class MyActor
include Celluloid
def initialize
async.wait_for_my_messages
end
def wait_for_my_messages
loop do
message = receive { |msg| msg.is_a? MyMessage }
puts "Got a MyMessage: #{message.inspect}"
end
end
end
The #receive method takes a block, and yields any incoming messages which are received by the current actor to the block, waiting for the block to return true. Calls to #receive sleep until a message is received which makes the block return true, at which point the matching message is returned.
Using the #receive method from outside of the actor is marginally useful. Make sure to setup the #receive before sending the messages, otherwise the messages will be dropped as unhandled.
In the future, customized actor behaviors will be possible.
Always feel free to:
- Visit the
#celluloid
channel on freenode. - Post a bug report or feature request.
- Ask questions on our mailing list.