1

たくさんの XML を取得して解析したいと考えています。それらはやや大きいです。だから私はこのような将来的にそれらを取得して解析できると思っていました:(私は現在セルロイドを使用しています)

country_xml = {}
country_pool = GetAndParseXML.pool size: 4, args: [@connection]
countries.each do |country|
   country_xml[country] = country_pool.future.fetch_xml country
end
countries.each do |country|
xml = country_xml[country]
# Do stuff with the XML!
end

実際に必要になる前に大量のメモリを消費することがなければ、これは問題ありません。理想的には、3 つの XML ファイルをバッファリングして停止し、少なくとも 1 つが処理されるまで待ってから続行することを望んでいます。どうすればいいですか?

4

2 に答える 2

0

私はまったく使用しませんPool。あなたはそれから恩恵を受けていません。多くの人は aFutureと aPoolを一緒に使用するのは良い考えだと感じているようですが、通常はどちらか一方を使用するよりも悪いです。

あなたの場合はFuture... を使用しますが、今後のMultiplexer機能も利用できます。それまでは、これを行ってください...これまでに試みられた、または提案されたものとはまったく異なる戦略を使用してください。

class HandleXML
    include Celluloid
    def initialize(fetcher)
        @fetcher = fetcher
    end
    def get_xml(country)
        @fetcher.fetch_xml(country)
    end
    def process_xml(country, xml)
        #de Do whatever you need to do with the data.
    end
 end

 def begin_processor(handler, countries, index)
     data = handler.future.get_xml(countries[index])
     index += 1
     data
 end

 limiter = 3 #de This sets your desired limit.
 country_index = 0
 data_index = 0
 data = {}
 processing = []
 handler = HandleXML.new(@connection)

 #de Load up your initial futures.
 limiter.times {
     processing << begin_processor(handler, countries, country_index)
 }

 while data_index < countries.length
     data[countries[data_index]] = processor.shift.value
     handler.process_xml(countries[data_index],data[countries[data_index]])
     #de Once you've taken out one XML set above, load up another.
     if country_index < countries.length
         processing << begin_processor(handler, countries, country_index)
     end
 end

上記は、一度Futureに処理するだけでそれを行う方法の例にすぎません3。私はそれを実行しておらず、エラーが発生する可能性がありますが、アイデアはあなたのために示されています.

このコードは Country XML のセットをロードし3、その XML の処理を​​開始します。XML の 1 つのセットを処理すると、すべての国の XML が処理されるまで、別のセットをロードします。

于 2015-06-16T13:18:50.737 に答える