我根本不会使用 a 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
。我没有运行它,它可能有错误,但这个想法是为你演示的。
代码加载多3
组 Country XML,然后开始处理该 XML。一旦它处理了一组 XML,它就会加载另一组,直到处理完所有国家的 XML。