简体   繁体   中英

Ruby on Rails large amount of sql queries

I'm building an app that queries an api and that saves the output of the api into a database. This is no rocket science and it is working but as the amount of data is growing, the inserts are going slower.

I have a simple form that accepts a keyword which is added into the api string to get all the keywords. I now like to show the results of the api onto the screen so the user can choose what results to keep.

I've added threading in my code, so that the inserts are going faster.

Is there a way to trigger an action when all the threads are finished?

Thanks

The simplest way to do this is to join all the threads in a thread pool, this is effectively waiting for them to finish:

threadpool = []

threadpool << Thread.new { do_stuff }
threadpool << Thread.new { do_more }

threadpool.map &:join # wait for all threads to finish
do_final_stuff # code below the join can only run when all threads finish

But that's just if you're using plain threads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM