Last active
January 10, 2016 01:45
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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