ruby – 赛璐珞回调

ruby – 赛璐珞回调,第1张

概述当使用赛璐珞时异步方法完成它的工作(回调)时,如何收到通知? 示例代码: require 'celluloid/autostart' class Test include Celluloid def initialize(aaa) @aaa = aaa end def foo sleep 20 @bbb = 'asdas 当使用赛璐珞时异步方法完成它的工作(回调)时,如何收到通知?

示例代码:

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 – 赛璐珞回调所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1269154.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存