任务/未来的Ruby [英] Task/future in Ruby

查看:109
本文介绍了任务/未来的Ruby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是说重新presents与可能性潜在延迟异步计算订阅其完成图案的惯用红宝石模拟?即沿东西.NET的行 System.Threading.Task 或Python 3.X concurrent.futures.future

What is the idiomatic Ruby analog of a pattern that represents a potentially deferred asynchronous computation with the possibility to subscribe to its completion? i.e. something along the lines of .NET System.Threading.Task, or Python 3.x concurrent.futures.future.

请注意,这并不一定意味着多线程 - 实际执行的未来对象将同样有可能使用调度的工作和取得的结果一些其他的方式,而且是出了问题的范围。该问题涉及严格遵守是psented对象的用户$ P $的API。

Note that this does not necessarily imply multithreading - the actual implementation of the "future" object would just as likely use some other way of scheduling the work and obtaining result, and is out of scope of the question. The question concerns strictly with the API that is presented to the user of the object.

推荐答案

我不知道香草红宝石,但EventMachine的有<一个href=\"https://github.com/eventmachine/eventmachine/wiki/EM%3a%3aDeferrable-and-EM.defer\">deferrables.

I am not sure about vanilla Ruby, but EventMachine has deferrables.

此外,检查<一href=\"http://rubylearning.com/blog/2010/10/01/an-introduction-to-eventmachine-and-how-to-avoid-callback-spaghetti/\">this文章。

EM.run {
  detector = LanguageDetector.new("Sgwn i os yw google yn deall Cymraeg?")
  detector.callback { |lang| puts "The language was #{lang}" }
  detector.errback { |error| puts "Error: #{error}" }
}

这篇关于任务/未来的Ruby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆