如何同时获取多个提要

时间:2010-08-31 16:14:05

标签: ruby-on-rails parsing concurrency feedparser feedzirra

我是ruby on rails的新手,我刚刚开始观看rails casts教程。

要解析Feed,我已开始使用Feed zirra。

要一次获取多个Feed,feedzirra具有此功能

feed_urls = ["http://feeds.feedburner.com/PaulDixExplainsNothing",
"http://feeds.feedburner.com/trottercashion"]
feeds = Feedzirra::Feed.fetch_and_parse(feed_urls)

如果我有100个Feed,则此过程需要一些时间来索引第100个Feed,

如何解析所有这些让我们同时说100个提要?

期待您的帮助和支持

1 个答案:

答案 0 :(得分:6)

您可以尝试合并Feedzirra进行Feed解析,将Typhoeus结合起来以获得可靠性,例如:

#!/usr/bin/env ruby
require 'rubygems'
require 'typhoeus'
require 'feedzirra'

feed_urls = ["http://feeds.feedburner.com/PaulDixExplainsNothing", "http://feeds.feedburner.com/trottercashion"]

hydra = Typhoeus::Hydra.new
feeds = {}

feed_urls.each do |feed|
        r = Typhoeus::Request.new(feed)
        r.on_complete do |response|
                feeds[r.url] = Feedzirra::Feed.parse(response.body)
        end
        hydra.queue r
end
hydra.run
puts feeds.inspect

这个想法是使用hydra来保持稳定。然后由你做其他事情而不是检查或填充feed变量。