0

ファイルから URL をチェックするスクリプトを作成しました (Ruby gem Typhoeus を使用)。コードを実行するとメモリ使用量が増加する理由がわかりません。通常、10000 URL スクリプトがクラッシュした後。それに対する解決策はありますか?よろしくお願いします。私のコード:

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
4

2 に答える 2

0

バッチで hydra への URL を処理していることを示唆しているように、コードを改善しました。通常のメモリ使用量で動作しますが、約 1000 個の URL の後、新しい URL の取得を停止する理由がわかりません。これは非常に奇妙です。エラーはありません。スクリプトはまだ実行されていますが、新しいリクエストを送信/取得しません。私のコード:

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
于 2012-04-06T15:24:43.250 に答える
0

1 つの方法として、ファイル処理を適応させることが考えられます。ファイルから行を読み取ってすぐに要求オブジェクトを作成するのではなく、それらをバッチで処理し (一度に 5000 など)、要求レート/メモリ消費を抑制します。

于 2012-04-04T13:31:51.407 に答える