示例代码:
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
我希望foo内部的工作完成后立即得到通知.
解决方法 我建议使用观察者模式.赛璐珞通过通知支持此功能.查看维基以获取一些信息: https://github.com/celluloid/celluloid/wiki/Notifications这是一个有效的代码示例:
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}" endendclass Observer include CelluloID include CelluloID::Notifications def initialize subscribe "done!",:on_completion end def on_completion(*args) puts "finished,returned #{args.inspect}" endendy = Observer.newx = Test.new 111x.async.foosleep 3@H_301_34@ 总结
以上是内存溢出为你收集整理的ruby – 赛璐珞回调全部内容,希望文章能够帮你解决ruby – 赛璐珞回调所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)