我正在製作一個數據密集型Web應用程序,我正試圖優化。我聽說過分叉和線程化,但我不知道它們是否適用於我正在嘗試做的事情,以及如何實現它們。我的代碼如下所示:如何加快我的Ruby應用程序?
def search
@amazon_data=Hash.from_xml(item.retrieve_amazon(params[:sku]))
unless @amazon_data['results'] == nil
@amazon_data['results']['item'].size.times do |i|
@all_books << { :vendor => 'Amazon.com',
:price => @amazon_data['results']['item'][i]['price'].to_f,
:shipping => @amazon_data['results']['item'][i]['ship'].to_f,
:condition => @amazon_data['results']['item'][i]['condition'],
:total => @amazon_data['results']['item'][i]['price'].to_f + @amazon_data['results']['item'][i]['ship'].to_f,
:availability => 'In Stock',
:link_text => 'Go to Amazon.com',
:link_url => "http://www.amazon.com/gp/offer-listing/#{params[:isbn]}"
}
end
end
@ebay_data=Hash.from_xml(Book.retrieve_ebay(params[:sku]))
unless @ebay_data['results'] == nil
@ebay_data['results']['item'].size.times do |i|
@all_books << { :vendor => 'eBay',
:price => @ebay_data['results']['item'][i]['price'].to_f,
:shipping => @ebay_data['results']['item'][i]['ship'].to_f,
:condition => 'Used',
:total => @ebay_data['results']['item'][i]['price'].to_f + @ebay_data['results']['item'][i]['ship'].to_f,
:availability => 'In Stock',
:link_text => 'Go to eBay',
:link_url => "http://www.amazon.com/gp/offer-listing/#{params[:sku]}"
}
end
end
end
所以,基本上我有什麼是檢索來自eBay和亞馬遜的數據,並在此解析它兩個動作。我該如何讓這兩個動作同時運行?叉子或線程與我想要完成的事情有什麼關係?
這會將API時間縮短一半,但我不知道如何返回結果。在返回API結果之前加載後續視圖....但是,它正在返回數據。當我在代碼
puts @all_books
內線程結果顯示在控制檯。但是,線程之外,結果不會返回。
def search
Thread.new do
@amazon_data=Hash.from_xml(item.retrieve_amazon(params[:sku]))
unless @amazon_data['results'] == nil
@amazon_data['results']['item'].size.times do |i|
@all_books << { :vendor => 'Amazon.com',
:price => @amazon_data['results']['item'][i]['price'].to_f,
:shipping => @amazon_data['results']['item'][i]['ship'].to_f,
:condition => @amazon_data['results']['item'][i]['condition'],
:total => @amazon_data['results']['item'][i]['price'].to_f + @amazon_data['results']['item'][i]['ship'].to_f,
:availability => 'In Stock',
:link_text => 'Go to Amazon.com',
:link_url => "http://www.amazon.com/gp/offer-listing/#{params[:isbn]}"
}
end
end
end
Thread.new do
@ebay_data=Hash.from_xml(Book.retrieve_ebay(params[:sku]))
unless @ebay_data['results'] == nil
@ebay_data['results']['item'].size.times do |i|
@all_books << { :vendor => 'eBay',
:price => @ebay_data['results']['item'][i]['price'].to_f,
:shipping => @ebay_data['results']['item'][i]['ship'].to_f,
:condition => 'Used',
:total => @ebay_data['results']['item'][i]['price'].to_f + @ebay_data['results']['item'][i]['ship'].to_f,
:availability => 'In Stock',
:link_text => 'Go to eBay',
:link_url => "http://www.amazon.com/gp/offer-listing/#{params[:sku]}"
}
end
end
end
end
我在正確的軌道上嗎?我怎樣才能從線程內返回結果?它是隻能在線程中訪問的變量,還是問題在於程序在返回結果之前進行的事實?
不幸的是,應用程序需要實時用戶輸入來查詢API。返回的數據必須是新鮮的,因爲它與市場中的產品定價有關......例如,用戶將進入SKU,並且通過該信息該程序將向適用的網站(亞馬遜和eBay在這種情況下。)目前,它向亞馬遜提出請求,解析數據,格式化數據,然後轉向eBay,分析數據並格式化數據。然後格式化的數據顯示在視圖中。
我的想法是,如果我可以同時進行這些API調用(在不同的線程上),它可以節省Web服務端的時間,因爲所有需要的是解析返回的數據並正確格式化。 (我可能也可以加快......)