I have a view with an AJAX call to a function in the controller. I want
this function to run in the background when the user clicks the link in
the view.
The function makes calls to another function many times in a loop, and I
don’t want these calls to get blocked. Something like this:
This function gets called by the AJAX
def loop
for i in (1…1000)
say(i)
end
render(:nothing => true)
end
def say(i)
Code that inserts i into a database table
end
The view periodically calls a function using ajax, and this function
prints stuff from the database table into the user’s view.
Currently this ajax call is getting blocked by the loop, so instead of
the user seeing the numbers appear as they’re inserted into the
database, he sees them all together in the end of the loop.
I’m thinking that multithreading could be an idea, but I don’t know how
to do it. Any other ideas that would work would be greatly appreciated.
keep in mind that we (my coworker and i) ran into a similar problem
and I’m not sure if this is what you might be experiencing.
the current http 1.1 spec suggests that clients have no more than 2
simultaneuous connections to a server at any time. browsers such as
Firefox and IE adhere to these suggestions but they can be overridden.
but thats neither here nor there…the limit is what is important.
so anyways, in our situtation, we had to fire off several ajax
requests, not knowing about this 2 request limit, and we found that if
one of the requests got hung up on the server side, then we were down
to 1 and if that one hung up…well, we’re SOL.
so what we’ve done is (as Ezra suggests) is use backgroundrb for our
intesive work (spawning many threads for said work) and use a
periodical request to poll the server for any new data we may need.
it works very well and is quite fast.