Skip to content

Instantly share code, notes, and snippets.

@maxdignan
Last active January 10, 2016 01:45
Show Gist options
  • Save maxdignan/0b2cd75bb1495059e640 to your computer and use it in GitHub Desktop.
Save maxdignan/0b2cd75bb1495059e640 to your computer and use it in GitHub Desktop.
Async handler to manage multiple db calls within a Rails Controller Action concurrently, without any hassle
def async_handler(*args)
dass = self
threads = []
args.each do |t|
threads << Thread.new do
ActiveRecord::Base.connection_pool.with_connection do
dass.send t
end
end
end
threads.each {|t| t.join}
end
# Usage (within a rails action)
#
# def show
# def first
# #call db - waits for response
# end
# def second
# #call db - waits for response
# end
# async_handler :first, :second
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment