シンプルに処理を非同期にする その2

Railsっぽく、スレッドセーフじゃない感じにしてみる。
async_callback.rbはLoadingModuleが勝手に読んでくれるとこに置いとく。
developmentモードだと何度も読み込まれるのが微妙な感じ…

async_callback.rb

class AsyncCallback
  @@queue = Queue.new

  Thread.fork {
    while func_args = @@queue.pop
      func, args = func_args
      Thread.fork { func.call(*args) }
    end
  }

  def self.invoke(*args, &block)
    @@queue.push([block, args])
  end
end

controller

class BarController < ApplicationController
  def index
    AsyncCallback.invoke('あれあれ', 'しょりが', 'おくれて', 'はしるよ?') {|*args|
      args.each do |arg|
        puts "#{arg}".tosjis
        sleep 2
      end
    }

    render :text => 'hello'
  end
end