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

Concurrency in Ruby: Tools of the trade

Concurrency in Ruby: Tools of the trade

Concurrency has been a popular topic in the programming community in the latest decade and has received special attention in the Ruby community in the latest years. In this talk, José Valim will showcase the tools we have available in Ruby and in the community today, focusing on common pitfalls. This is a great opportunity to discuss why concurrency matters and how we could move forward.

Plataformatec

June 08, 2013
Tweet

More Decks by Plataformatec

Other Decks in Technology

Transcript

  1. •Started with Ruby in 2006 •Open Source Contributor •Joined Rails

    Core in 2010 •Author of elixir-lang.org About me
  2. hash = {} 10.times do |i| Thread.new { hash[i] =

    "Hello #{rand}" } end Threadsafe?
  3. Without the GVL, other Ruby implementations may try to change

    the same Hash at the same time, corrupting it
  4. class Auth def initialize(callback) @callback = callback end def call(info)

    @user = user_from_info(info) res = @callback.call(info) update_user(res) # access @user res end end
  5. class Auth def initialize(callback) @callback = callback end def call(info)

    @user = user_from_info(info) res = @callback.call(info) update_user(res) # access @user res end end
  6. class AuthMiddleware def initialize(callback) @callback = callback end def call(info)

    @user = user_from_info(info) res = @callback.call(info) update_user(res) # access @user res end end
  7. @@p = Hash.new do |h, k| h[k] = "#{k}Presenter".constantize end

    def presenter_for(model) @@p[model.class.name].new(model) end
  8. hash = {} mutex = Mutex.new 10.times do |i| Thread.new

    { mutex.synchronize { hash[i] = "Hello #{rand}" } } end
  9. class Manager attr_reader :queue def initialize @subscribers = [] @queue

    = Queue.new end def loop while message = @queue.pop event, *args = message send(event, *args) end end
  10. We need to push for better semantics at the same

    time we change how we think about concurrency