I have this simple example:
require 'watir-webdriver'
arr = []
sites = [
"www.google.com",
"www.bbc.com",
"www.cnn.com",
"www.gmail.com"
]
sites.each do |site|
arr << Thread.new {
b = Watir::Browser.new :chrome
b.goto site
puts b.url
b.close
}
end
arr.each {|t| t.join}
Every time i run this script, I get
ruby/2.1.0/net/http.rb:879:in `initialize': Connection refused - connect(2) for "127.0.0.1" port 9517 (Errno::ECONNREFUSED)
Or one of the browsers closes unexpectedly on atleast one of the threads.
on the other hand, if i set sleep 2
at the end of every loop cycle, everything runs smoothly!
Any idea why is that?
Must be something related to understanding how threads work...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…