我写了一个脚本来检查文件中的urls (使用ruby gem Typhoeus)。我不知道为什么当我运行我的代码时,内存使用量会增长。通常在10000个urls脚本崩溃之后。有什么解决方案吗?提前感谢您的帮助。我的代码:
require 'rubygems'
require 'typhoeus'
def run file
log = Logger.new('log')
hydra = Typhoeus::Hydra.new(:max_concurrency => 30)
hydra.disable_memoization
File.open(file).each do |url|
begin
request = Typhoeus::Request.new(url.strip, :method => :get, :follow_location => true)
request.on_complete do |resp|
check_website(url, resp.body)
end
puts "queuing #{ url }"
hydra.queue(request)
request.destroy
rescue Exception => e
log.error e
end
end
hydra.run
end发布于 2012-04-04 21:31:51
一种方法可能是调整您的文件处理-而不是从文件中读取一行并立即创建请求对象,尝试批量处理它们(比方说一次处理5000个),并限制请求速率/内存消耗。
发布于 2012-04-06 23:24:43
我已经对我的代码进行了改进,因为您建议我批量处理hydra的urls。它在正常的内存使用情况下工作,但我不知道为什么在大约1000个urls之后,它就停止获取新的urls。这非常奇怪,没有错误,脚本仍然在运行,但它不发送/获取新的请求。我的代码:
def run file, concurrency
log = Logger.new('log')
log.info '*** Hydra started ***'
queue = []
File.open(file).each do |uri|
queue << uri
if queue.size == concurrency * 5
hydra = Typhoeus::Hydra.new(:max_concurrency => concurrency)
hydra.disable_memoization
queue.each do |url|
request = Typhoeus::Request.new(url.strip, :method => :get, :follow_location => true, :max_redirections => 2, :timeout => 5000)
request.on_complete do |resp|
check_website(url, resp.body)
puts "#{url} code: #{resp.code} curl_msg #{resp.curl_error_message}"
end
puts "queuing #{url}"
hydra.queue(request)
end
puts 'hydra run'
hydra.run
queue = []
end
end
log.info '*** Hydra finished work ***'
endhttps://stackoverflow.com/questions/10011762
复制相似问题