Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Task/future in Ruby

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.

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.

like image 467
Pavel Minaev Avatar asked Feb 21 '12 03:02

Pavel Minaev


1 Answers

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

Also, check out this article.

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}" }
}
like image 85
Sergio Tulentsev Avatar answered Oct 21 '22 04:10

Sergio Tulentsev