添加图像以获取队列然后抓取它们

时间:2013-01-04 23:54:11

标签: ruby multithreading

我对ruby mutli-threading相当新,并对如何开始感到困惑。我目前正在构建一个应用程序,它需要获取大量图像,所以我想在不同的线程中进行。我希望程序执行如下面的代码所示。

问题:我在这里看到的问题是bar_method将更快地获取并且线程将结束,因此事情将继续添加到队列但不会被处理。是否有任何同步方式可以提醒bar_method线程已将新项目添加到队列中,如果bar_method先前完成,它应该进入休眠状态并等待将新项目添加到队列中?

def foo_method 
  queue created - consists of url to fetch and a callback method
  synch = Mutex.new
  Thread.new do
    bar_method synch, queue 
  end
  100000.times do
    synch.synchronize do
      queue << {url => img_url, method_callback => the_callback}
    end
  end
end
def bar_method synch_obj, queue
  synch_obj.synchronize do
    while queue isn't empty
        pop the queue. fetch image and call the callback
    end   
  end
end 

1 个答案:

答案 0 :(得分:2)

如果您需要从互联网上检索文件并使用并行请求,我强烈推荐Typhoeus and Hydra

来自文档:

hydra = Typhoeus::Hydra.new
10.times.map{ hydra.queue(Typhoeus::Request.new("www.example.com", followlocation: true)) }
hydra.run

您可以在Hydra中设置并发连接数:

  

:max_concurrency(Integer) - 要创建的最大并发连接数。默认值为200。

作为第二个建议,请查看Curb。再次,从其文档:

# make multiple GET requests
easy_options = {:follow_location => true}
multi_options = {:pipeline => true}

Curl::Multi.get('url1','url2','url3','url4','url5', easy_options, multi_options) do|easy|
  # do something interesting with the easy response
  puts easy.last_effective_url
end

两者都建立在Curl之上,因此它们的底层技术或其稳健性没有真正的区别。区别在于您可以使用的命令。

另一个引起很多关注的宝石是EventMachine。它有EM-HTTP-Request,允许并发请求:

EventMachine.run {
  http1 = EventMachine::HttpRequest.new('http://google.com/').get
  http2 = EventMachine::HttpRequest.new('http://yahoo.com/').get

  http1.callback { }
  http2.callback { } 
end