I've got a set of data that I'd like to do some calculations on inside my rails application, each calculation is independent of each other so I'd like to thread them so my response is much faster.
Here's what I've got ATM:
def show
@stats = Stats.new
Thread.new {
@stats.top_brands = #RESULT OF FIRST CALCULATION
}
Thread.new {
@stats.top_retailers = #RESULT OF SECOND CALCULATION
}
Thread.new {
@stats.top_styles = #RESULT OF THIRD CALCULATION
}
Thread.new {
@stats.top_colors = #RESULT OF FOURTH CALCULATION
}
render json: @stats
end
Now this returns a bunch of empty arrays for each of the member instances of @stats
, however, if I join the threads together, it runs, but defeats the purpose of threading since each of the threads block.
Since I'm very new to threads, I'm wondering what I'm doing wrong here or if its even possible to accomplish what I'm trying do, that is, run 4 calcs in paralell and return the result to the client.
Thanks,
Joe