Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Non blocking HTTP requests?

I want to display some Amazon products, loaded via Ajax.

I call the method below with Ajax, but the request takes a couple of seconds.

@items = []
@shows.shuffle.first(5).each do |show|
    req = AmazonProduct["us"]
    req.configure do |c|
      c.key = "###"
      c.secret = "###"
      c.tag = "###"
    end
    req << { :operation => 'ItemSearch',
             :search_index => params[:product_type],
             :response_group => %w{ItemAttributes Images},
             :keywords => show.name,
             :sort => "" }
    resp = req.get
    @items << resp.find('Item').shuffle.first
end

I've nothiced that this Action blocks the server. I've tried having the site open in another tab. That tab won't start loading until the first tab with the Ajax call completes.

How can I go about solving this problem?


Setup:

Ubuntu 10.10
Rails 3.1.1
Ruby 1.9.2
Gem: https://github.com/hakanensari/amazon_product

like image 206
Frexuz Avatar asked Nov 05 '22 11:11

Frexuz


1 Answers

I suspect this blocks because you are doing your testing in development mode, using the default Rails sever, Webrick.

My understanding is that webrick can only process one request at a time (which is why it's not suggested for production use).

A production level Rails sever, like Phusion Passenger, or a cluster of mongrel/thin servers, will get you your concurrency :)

like image 108
RyanWilcox Avatar answered Nov 09 '22 15:11

RyanWilcox