带有Watir的Ruby线程

时间:2013-03-03 19:14:25

标签: ruby multithreading watir

我写了几个类来管理我想要处理几个网站的方法,两者都有类似的方法(即登录,刷新)。每个类都会打开自己的WATIR浏览器实例。

class Site1
    def initialize
         @ie = Watir::Browser.new
    end
    def login
         @ie.goto "www.blah.com"
    end
end

主要没有线程的代码示例如下

require 'watir'
require_relative 'site1'

agents = []
agents << Site1.new

agents.each{ |agent|
     agent.login
}

这样可以正常工作,但是在当前的代理完成登录之前不会移动到下一个代理。我想结合使用多线程来处理这个问题,但似乎无法使其工作。

require 'watir'
require_relative 'site1'

agents = []; threads = []
agents << Site1.new


agents.each{ |agent|
     threads << Thread.new(agent){ agent.login }
}

threads.each { |t| t.join }

这给了我错误:未知属性或方法:navigate。 HRESULT错误代码:0x8001010e。该应用程序调用了一个为不同线程编组的接口。

有谁知道如何解决这个问题,或者如何实现类似的功能?

1 个答案:

答案 0 :(得分:0)

对此不太确定,但这是使用线程的摆动。

require 'thread'
  threads = []                # Setting an array to store threaded commands
  c_thread = Thread.new do    # Start a new thread
    login                     # Call our command in the thread
  end
  threads << c_thread