Are there any good gems/patterns for rendering the results of a loop in a Ruby/Sinatra app as the loop is being processed? At the moment my loop can take 1-2 mins to finish processing and ideally I’d like to output the results in real time rather than have the user wait 1-2 mins.
Share
What you can do is, when you get a request first time, you can start the process in another thread, and return the request with a ‘process started flag’.
Now this thread is running the loop and and storing the intermediate results in a database (may be in temporary table).
At the same time your view (JavaScript i assume), will start make Ajax requests to this temporary table which will yield intermediate results while the loop is running.
Once the process is complete the ajax will returned ‘finish’ and the requests can stop. At the same time the thread can delete the table and be stopped.