首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Typhoeus Hydra内存不足

Typhoeus Hydra内存不足
EN

Stack Overflow用户
提问于 2012-04-04 21:08:44
回答 2查看 1K关注 0票数 0

我写了一个脚本来检查文件中的urls (使用ruby gem Typhoeus)。我不知道为什么当我运行我的代码时,内存使用量会增长。通常在10000个urls脚本崩溃之后。有什么解决方案吗?提前感谢您的帮助。我的代码:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2012-04-04 21:31:51

一种方法可能是调整您的文件处理-而不是从文件中读取一行并立即创建请求对象,尝试批量处理它们(比方说一次处理5000个),并限制请求速率/内存消耗。

票数 0
EN

Stack Overflow用户

发布于 2012-04-06 23:24:43

我已经对我的代码进行了改进,因为您建议我批量处理hydra的urls。它在正常的内存使用情况下工作,但我不知道为什么在大约1000个urls之后,它就停止获取新的urls。这非常奇怪,没有错误,脚本仍然在运行,但它不发送/获取新的请求。我的代码:

代码语言:javascript
复制
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 ***'
    end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10011762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档