Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Thread gem

Thread gem

A short lightning talk I gave on the Ruby User Group Berlin in October 2014.

Thread#every with a slight modification can be used as a circuit breaking, self updating cache of an external service.

Find many other goodies for working with threads in the Thread gem by @meh:
https://github.com/meh/ruby-thread

Lukas Rieder

October 02, 2014
Tweet

More Decks by Lukas Rieder

Other Decks in Technology

Transcript

  1. class Life! ! def self.why?! ! ! sleep(rand(5))! ! !

    raise "" unless give_answer?! ! ! "answer: #{rand(42)}"! ! end! ! ! def self.give_answer?! ! ! rand(2) > 0 ? true : false! ! end! ! end
  2. class Life! ! def self.why?! ! ! sleep(rand(5))! ! !

    raise "" unless give_answer?! ! ! "answer: #{rand(42)}"! ! end! ! ! def self.give_answer?! ! ! rand(2) > 0 ? true : false! ! end! ! end
  3. class Life! ! def self.why?! ! ! sleep(rand(5))! ! !

    raise "" unless give_answer?! ! ! "answer: #{rand(42)}"! ! end! ! ! def self.give_answer?! ! ! rand(2) > 0 ? true : false! ! end! ! end
  4. class RelaxedLife! ! def self.why?! @every ||= Thread.every(60) do! Life.why?!

    end! @every.gimme_value! || "go have fun"! end! ! end
  5. class RelaxedLife! ! def self.why?! @every ||= Thread.every(60) do! Life.why?!

    end! @every.gimme_value! || "go have fun"! end! ! end
  6. RelaxedLife.why?! # …waiting…! # => "go have fun"! RelaxedLife.why?! #

    => 8! RelaxedLife.why?! # => 8! RelaxedLife.why?! # => 41!
  7. RelaxedLife.why?! # …waiting…! # => "go have fun"! RelaxedLife.why?! #

    => 8! RelaxedLife.why?! # => 8! RelaxedLife.why?! # => 41! RelaxedLife.why?! # …