Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby、Mongodb、Anemone:可能存在内存泄漏的网络爬虫?_Ruby_Mongodb_Memory Leaks_Web Crawler_Anemone - Fatal编程技术网

Ruby、Mongodb、Anemone:可能存在内存泄漏的网络爬虫?

Ruby、Mongodb、Anemone:可能存在内存泄漏的网络爬虫?,ruby,mongodb,memory-leaks,web-crawler,anemone,Ruby,Mongodb,Memory Leaks,Web Crawler,Anemone,我最近开始学习网络爬虫,并用Ruby构建了一个用于存储的示例爬虫。我正在一个可能有数十亿链接的大型公共网站上测试爬虫程序 crawler.rb正在为正确的信息编制索引,尽管当我在活动监视器中检查内存使用情况时,它显示内存在不断增长。我只运行了6-7个小时,mongod的内存显示为1.38GB,Ruby进程的内存显示为1.37GB。它似乎每小时大约增长100MB 看来我可能有内存泄漏?它们是否是我实现相同爬网的最佳方式,而不会使内存升级失控,从而使其运行更长时间 #带有海葵、Mongodb和Rub

我最近开始学习网络爬虫,并用Ruby构建了一个用于存储的示例爬虫。我正在一个可能有数十亿链接的大型公共网站上测试爬虫程序

crawler.rb正在为正确的信息编制索引,尽管当我在活动监视器中检查内存使用情况时,它显示内存在不断增长。我只运行了6-7个小时,mongod的内存显示为1.38GB,Ruby进程的内存显示为1.37GB。它似乎每小时大约增长100MB

看来我可能有内存泄漏?它们是否是我实现相同爬网的最佳方式,而不会使内存升级失控,从而使其运行更长时间

#带有海葵、Mongodb和Ruby的示例web_crawler.rb。
需要‘海葵’
#不要存储页面的正文。
海葵模块
类页
def to_散列
{'url'=>@url.to_,
'links'=>links.map(&:to_s),
'code'=>@code,
“已访问”=>@已访问,
“深度”=>@depth,
“referer'=>@referer.to_,
'fetched'=>@fetched}
结束
定义自_散列(散列)
page=self.new(URI(散列['url']))
{'@links'=>hash['links'].map{{| link | URI(link)},
“@code'=>散列['code']”。到,
“@visted'=>hash['visted'],
“@depth'=>散列['depth']”。到_i,
“@referer'=>hash['referer'],
'@fetched'=>hash['fetched']
}.每个do | var,值|
page.instance\u variable\u set(变量、值)
结束
页
结束
结束
结束
银莲花http://www.example.com/“,:discard_page_body=>true,:threads=>1,:obe_robots_txt=>true,:user_agent=>Example-webcrawler”,:large_scale_crawl=>true)do |海葵|
anemone.storage=anemone::storage.MongoDB
#仅在url中包含/示例的爬网页面
银莲花。聚焦?爬行做?第页|
links=page.links.delete_如果做| link|
(link.to_=~/example/).nil?
结束
结束
#仅处理/example目录中的页面
银莲花。在像(/example/)这样的页面上|
regex=/某种类型的regex/
example=page.doc.css('#example_div')。inner_html.gsub(regex'))下一步
#保存到文本文件
如果!例如,零?还有例子!=""
open('example.txt','a'){| f | f.puts“{example}”
结束
page.discard\u doc!
结束
结束

我也在做类似的事情,我想你可能只是在创建大量的数据

您保存的不是正文,因此应该有助于满足内存要求

我能想到的另一个改进是使用Redis而不是Mongo,因为我发现它对Anemone的存储更具可扩展性


检查mongo中的数据大小-我发现我保存了大量行

我在这方面也有问题,但我使用redis作为数据存储

这是我的爬虫:

require "rubygems"

require "anemone"

urls = File.open("urls.csv")
opts = {discard_page_bodies: true, skip_query_strings: true, depth_limit:2000, read_timeout: 10} 

File.open("results.csv", "a") do |result_file|

  while row = urls.gets

    row_ = row.strip.split(',')
    if row_[1].start_with?("http://")
      url = row_[1]
    else
      url = "http://#{row_[1]}"
    end 
    Anemone.crawl(url, options = opts) do |anemone|
      anemone.storage = Anemone::Storage.Redis
      puts "crawling #{url}"    
      anemone.on_every_page do |page| 

        next if page.body == nil 

        if page.body.downcase.include?("sometext")
          puts "found one at #{url}"     
          result_file.puts "#{row_[0]},#{row_[1]}"
          next

        end # end if 

      end # end on_every_page

    end # end crawl

  end # end while

  # we're done
  puts "We're done."

end # end File.open
我在海葵宝石中的core.rb文件中应用了来自的修补程序:

35       # Prevent page_queue from using excessive RAM. Can indirectly limit ra    te of crawling. You'll additionally want to use discard_page_bodies and/or a     non-memory 'storage' option
36       :max_page_queue_size => 100,

(以下内容过去位于第155行)

我每小时做一次cron工作:

#!/usr/bin/env python
import redis
r = redis.Redis()
r.flushall()
尝试降低redis的内存使用率。我正在重新开始一个巨大的爬行,所以我们来看看它是如何进行的


我会报告结果…

你找到泄漏的原因了吗?如果你认为这是海葵的bug,你有没有在他们的相关问题上报告过呢?海葵问题跟踪上提到的相关问题包括:,我在这里发布的同时报告了。通过添加建议的修复程序,我能够抓取我的任务所需的内容,这使我的抓取持续时间更长,尽管ram的使用量在稳步增长,只是没有以前那么快。我仍然不确定是什么导致内存泄漏。
#!/usr/bin/env python
import redis
r = redis.Redis()
r.flushall()