gpt4 book ai didi

ruby - 我应该在 Goliath 中使用 EM::Synchrony::Multi 还是 EM::Synchrony::FiberIterator?

转载 作者:可可西里 更新时间:2023-11-01 11:22:45 26 4
gpt4 key购买 nike

也许这是错误的方法,但我是 trying to parallelize em-hiredis使用 EM::Synchrony::MultiEM::Synchrony::FiberIterator 在 Goliath 中放置和查找。但是,我似乎无法访问基本值 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

但是,我不断收到错误消息,其中 Goliath 不知道 user_agent 是什么,或者 redis_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

最佳答案

我认为您的配置文件没有加载。 try.rb的名字需要和config目录下的robojin.rb文件的名字一致。

关于ruby - 我应该在 Goliath 中使用 EM::Synchrony::Multi 还是 EM::Synchrony::FiberIterator?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12534300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com