如何在Rails环境中运行多个线程?

时间:2015-07-24 19:51:54

标签: ruby-on-rails ruby

我花了一整天的时间在这上面,我无法弄清楚如何在rails中使用多线程,而不会导致以下错误:

:100644 100644 b960510... 0000000... M  MyProject/app.config

代码在没有线程的情况下工作,但是一旦我引入线程,就会出现“循环依赖”错误。

以下是有问题的代码:

Uncaught exception: Circular dependency detected while autoloading constant Docket::System::ExecutableFactory
    /Users/myuser/.rvm/gems/ruby-2.1.2@core/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:478:in `load_missing_constant'
    /Users/myuser/.rvm/gems/ruby-2.1.2@core/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:180:in `const_missing'

Docket :: System :: ExecutableFactory是我的一个类,但我已经正确嵌套了。目录结构:

def run
  records.in_groups_of(4) do |records|
    workers = records.map do |record|
      Thread.new do
        begin
          options[:record] = record
          county = create_county options
          county.prepare
          if county.ready
            county.crawl
          end
        rescue ThreadError => e
          puts "THREAD ERROR: #{e}"
        end
      end
    end
    workers.map(&:join)
  end
end

...

  def crawl
    cmd = "#{executable_factory.path} #{crawler_path}"
    Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
      stdout_is = stdout.read
      stderr_is = stderr.read
    end
  end

我有正确的名字空间:

\docket
    \system
        executable_factory.rb

那么为什么线程会出现这种错误,我该如何修复呢?

0 个答案:

没有答案