带有Watir的Ruby线程

cub*_*nyc 6 ruby multithreading watir

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

class Site1
    def initialize
         @ie = Watir::Browser.new
    end
    def login
         @ie.goto "www.blah.com"
    end
end
Run Code Online (Sandbox Code Playgroud)

main中没有线程的代码示例如下

require 'watir'
require_relative 'site1'

agents = []
agents << Site1.new

agents.each{ |agent|
     agent.login
}
Run Code Online (Sandbox Code Playgroud)

这工作正常,但没有移动到下一个代理,直到当前的一个完成登录.我想结合多线程来处理这个,但似乎无法让它工作.

require 'watir'
require_relative 'site1'

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


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

threads.each { |t| t.join }
Run Code Online (Sandbox Code Playgroud)

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

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

Sin*_*ity 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
Run Code Online (Sandbox Code Playgroud)