0
也許這是錯誤的方法,但我是trying to parallelizeem-hiredis在歌利亞放置和查找與EM::Synchrony::Multi
或EM::Synchrony::FiberIterator
。但是,我似乎無法訪問基本值initialized in the config。我不斷收到method_missing
錯誤。我應該在Goliath中使用EM :: Synchrony :: Multi還是EM :: Synchrony :: FiberIterator?
這裏是我想要做的基本淡化版本:
/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_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/[email protected]/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/[email protected]/gems/em-synchrony-1.0.2/lib/em-synchrony/fiber_iterator.rb:10:in `call'
from /Users/ewu/.rvm/gems/[email protected]/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。我錯誤地在問題中輸入了它。我相當確信配置文件正在被加載,因爲我在redis服務器中看到了一個連接,當我啓動goliath時。但我仍然得到缺少錯誤的方法---------------------------- [614] 9月21日23:41:14 - 接受127.0.0.1:49353 [614] 9月21日23:41:19 - 1個客戶端已連接(0個從屬),930960個字節正在使用中 [614] 9月21日23:41:24 - 1個客戶端連接(0個從屬),930960正在使用的字節 – eywu
如果我將迭代器之外的打印語句(p「#{user_agent}」)拉出,它會輸出很好。應該從FiberIterator中訪問配置變量嗎? – eywu
當使用Fiber迭代器時,綁定結果可能會有所不同。我建議將你想要在本地使用的配置分配給局部變量。 – dj2