Celluloid callback

How can I get a notification when the async method completed the job (callback) when using Celluloid?

Code example:

require 'celluloid/autostart' class Test include Celluloid def initialize(aaa) @aaa = aaa end def foo sleep 20 @bbb = 'asdasd' end def bar "aaa is: #{@aaa}, bbb is: #{@bbb}" end end x = Test.new 111 x.async.foo 

I would like to be notified as soon as the work inside foo is done.

+4
source share
2 answers

I recommend using the Observer pattern. Celluloid supports this through Notifications. Take a look at the wiki for some info: https://github.com/celluloid/celluloid/wiki/Notifications

Here is an example of working code:

 require 'rubygems' require 'celluloid/autostart' class Test include Celluloid include Celluloid::Notifications def initialize(aaa) @aaa = aaa end def foo sleep 2 @bbb = 'asdasd' publish "done!", "Slept for 2 seconds and set @bbb = #{@bbb}" end def bar "aaa is: #{@aaa}, bbb is: #{@bbb}" end end class Observer include Celluloid include Celluloid::Notifications def initialize subscribe "done!", :on_completion end def on_completion(*args) puts "finished, returned #{args.inspect}" end end y = Observer.new x = Test.new 111 x.async.foo sleep 3 
+1
source

Right now, I think the new Terms feature is the preferred way to handle this.

The sample Celluloid wiki page on conditions is too large to be inserted here, but you can simply create a Condition object that is signaled by the called method as soon as it is done. The caller can simply wait until the condition is met.

0
source

Source: https://habr.com/ru/post/1496807/


All Articles