1

我有一个从 url 读取并检索一些数据的工作人员。所以我使用池来实现 10 个并发工作者的并发:

require 'nokogiri'
require 'open-uri'
require 'celluloid/autostart'

 class WebWorker
    include Celluloid
    def get_data
      url = "http://example.com/"
      doc = Nokogiri::HTML( open( url ), nil, "UTF-8" )
      { data: doc.css("body h1")[0].content.strip }
    end
  end

pool = WebWorker.pool(:size => 10)
10.times do |t|
   futures = 10.times.map{ |t| pool.future(:get_data) }
   results = futures.map(&:value)
end

将上面的代码放在一个名为 some_name.rb 的文件中,运行它,你会得到:

D, [2014-05-09T10:15:06.061071 #6588] DEBUG -- : 终止 15 个演员... W, [2014-05-09T10:15:06.063755 #6588] WARN -- : 终止任务: type=:终结器,元={:方法名称=>:关闭},状态=:调用等待

我的问题是如何暂停这些消息?包装在开始..救援没有帮助。而且,为什么会出现警告,代码有什么问题吗?

谢谢!

4

2 回答 2

1

需要补充:

# Use the logger of your Rails application.
Celluloid.logger = Rails.logger
于 2014-05-18T06:21:14.627 回答
0

在程序退出之前终止池的链接:

pool.links.each(&:terminate)
于 2015-05-13T09:05:29.863 回答