Reputation: 4943
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
Upvotes: 5
Views: 1506
Reputation: 7824
EDIT:
Just to clarify, it depends on the server you use in your development environment, it's not rails.
WebRick (default http server in dev) can only handle one request at the time. When you deploy your application you should use puma, unicorn, Phusion Passenger, or any other server that has more than one process (or thread) handling your requests.
OLD Answer:
I don't think this is possible in rails.
The way how I would approach this problem is to create a simple sinatra-synchrony app that has only action for fetching products that you want from amazon.
To be able to send ajax request to this app, you need to point this app to your domain (subdomain).
I don't see any other solution, at the moment. Of course you can have more than one application process on your server, but this will not solve your problem in long term.
So to wrap up:
AJAX -> amazon-producs-sinatra-app.yourdomain.com
Upvotes: 0
Reputation: 13972
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 :)
Upvotes: 2