我应该使用EM :: Synchrony :: Multi或EM :: Synchrony :: FiberIterator和Goliath吗?

时间:2012-09-21 16:19:25

标签: ruby redis eventmachine goliath

也许这是错误的方法,但我trying to parallelize em-hiredis使用EM::Synchrony::MultiEM::Synchrony::FiberIterator在歌利亚中放置和查找。但是,我似乎无法访问基本值initialized in the config。我一直收到method_missing错误。

这是我正在尝试做的基本淡化版本:

/lib/config/try.rb

config['redisUri'] = 'redis://localhost:6379/0'
config['redis_db'] ||= EM::Hiredis.connect
config['user_agent'] = "MyCrawler Mozilla/5.0 Compat etc."

这是基本的歌利亚设置

/try.rb

require "goliath"
require "em-hiredis"
require "em-synchrony/fiber_iterator"
require "em-synchrony/em-hiredis"
require "em-synchrony/em-multi"

class Try < Goliath::API
  use Goliath::Rack::Params
  use Goliath::Rack::DefaultMimeType

  def response(env)
    case env['REQUEST_PATH']
    when "/start" then
      start_crawl()
      body = "STARTING"
      [200, {}, body]
    end 
  end 

  def start_crawl
    urls = ["http://www.example.com/",
      "http://www.example.com/photos/",
      "http://www.example.com/video/",
    ]

    EM::Synchrony::FiberIterator.new(urls, 3).each do |url|
      p "#{user_agent}"
      redis_db.sadd 'test_queue', url
    end

    # multi = EM::Synchrony::Multi.new
    # urls.each_with_index do |url, index|
    #  p "#{user_agent}"
    #  multi.add index, redis_db.sadd('test_queue', url)
    # end
  end
end

然而,我不断收到错误,其中歌利亚不知道在配置中初始化了user_agentredis_db

[936:INFO] 2012-09-21 23:47:10 :: Starting server on 0.0.0.0:9000 in development mode. Watch out for stones.
/Users/ewu/.rvm/gems/ruby-1.9.3-p194@crawler/gems/goliath-1.0.0/lib/goliath/api.rb:143:in `method_missing': undefined local variable or method `user_agent' for #<Try:0x007ff5a431c4e0 @opts={}> (NameError)
from ./lib/try.rb:27:in `block in start_crawl'
from /Users/ewu/.rvm/gems/ruby-1.9.3-p194@crawler/gems/em-synchrony-1.0.2/lib/em-synchrony/fiber_iterator.rb:10:in `call'
from /Users/ewu/.rvm/gems/ruby-1.9.3-p194@crawler/gems/em-synchrony-1.0.2/lib/em-synchrony/fiber_iterator.rb:10:in `block (2 levels) in each'
...
...
...

理想情况下,我可以让FiberIterator正常工作,因为我还有其他条件可供检查:

EM::Synchrony::FiberIterator.new(urls, 3).each do |new_url}
  is_member = redis_db.sismember('crawled_urls', new_url)
  is_member += redis_db.sismember('queued_urls', new_url)
  if is_member == 0
    redis_db.lpush 'crawl_queue', new_url
    redis_db.sadd 'queued_urls', new_url
  end
end

1 个答案:

答案 0 :(得分:1)

我认为你的配置文件没有加载。 try.rb的名称需要与config目录中robojin.rb文件的名称相匹配。