Skip to content

Instantly share code, notes, and snippets.

@shawndeprey
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shawndeprey/910ae2417300703aabd0 to your computer and use it in GitHub Desktop.
Save shawndeprey/910ae2417300703aabd0 to your computer and use it in GitHub Desktop.
Threading Pattern for Request Processing in Rails
def index
Thread.abort_on_exception = true
thr_one = Thread.new do
@list_one = ModelOne.find_model_ones_in_elasticsearch(params[:page])
end
thr_two = Thread.new do
@list_two = ModelTwo.find_model_twos_in_elasticsearch(params[:page])
end
# Pause the main thread and allow our threads to execute
thr_one.join
thr_two.join
# Ensure we clean up our threads after processing is finished
thr_one.exit
thr_two.exit
render json: {ones: @list_one, twos: @list_two}, root: false, serializer: SomeSerializer
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment