Excerpts from Dmytrii Nagirniak's message of 2011-12-02 18:18:11 +1100: > > On 02/12/2011, at 6:02 PM, James Healy wrote: > > > You definitely don't want to farm HTTP requests into threads. > > > > Farm CPU bound code onto threads, keep IO bound code on the main event loop. > > > > Use em-http-request to do HTTP requests on the event loop. > > > > Thanks a lot. It looks like exactly what I need. > > The only difference is that I am not writing a server. I am going to use it > to lazily execute execute HTTP requests while keep the app running. > So for example, in the typical Rails app this code would not care about > sync/async: > > # in the controller > post = Post.find(123) # Executes HTTP request, but doesn't block > do_somethig_else_here > > # A bit further, maybe in the view: > post.title # Here it either waits for the request to finish or, if it's > already finished, just returns the value > > This hides the complexity of the async processing with much simpler interface. > Do you think it is a valid use-case for the em-http-request? > > Cheers.
Using EventMachine in that scenario is probably more grief than it's worth. When using EM from within Rails you will need to either use an EM server such as thin or spin up another thread in which to start the reactor. Neither of these things are particularly hard to do. That, however, is not the main cause for concern! Given that you are running async code in the model you will get a non-local return error (from memory - it's been a while since I've used EM based code in rails). So you need to sync it using fibers. That's pretty much the only way and it dosen't actually gain you anything! You could use em-synchrony to give the impression that you are writing "normal ruby" but that still uses fibers. You might be able to do something funky by spinning up a thread at startup in which you would run some EM code that listens on a named pipe so you could write a message to that from the rails code. But that sounds like more trouble than it's worth. I would suggest that you best bet is to have an independent process that does the downloading for you such as cloudist or one of the many other job queues. cloudist: https://github.com/ivanvanderbyl/cloudist em-synchrony: https://github.com/igrigorik/em-synchrony rgh -- [e]: [email protected] [im]: [email protected] You're worried criminals will continue to penetrate into cyberspace, and I'm worried complexity, poor design and mismanagement will be there to meet them - Marcus Ranum -- You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rails-oceania?hl=en.
