Thread exec

Hi List,

I’ve a problem with threads using exec, system or similar:



domains = [‘www.bim.com’,‘www.bum.com’,‘www.bam.com’]

def check (host,domain,timeout)
puts “checking… #{host} → #{domain}”
timeout(timeout) do
begin
/usr/bin/nslookup #{domain} #{host}
rescue Timeout::Error
end
end
end

iplist.each { |host|
threads << Thread.new(host) { |host|
domains.each do |domain|
check(host,domain,timeout)
end
}
}

With this code will be check only the first domains of three. Why?

checking… 127.0.0.1 → www.bim.com
checking… 127.0.0.2 → www.bim.com
checking… 127.0.0.3 → www.bim.com
checking… 127.0.0.4 → www.bim.com
checking… 127.0.0.5 → www.bim.com
checking… 127.0.0.6 → www.bim.com
checking… 127.0.0.7 → www.bim.com
checking… 127.0.0.8 → www.bim.com
checking… 127.0.0.9 → www.bim.com

Thank you very much,
Al

I think, your script is missing Thread::join:

end

iplist.each { |host|
threads << Thread.new(host) { |host|
domains.each do |domain|
check(host,domain,timeout)
end
}}

threads.each {|th|
th.join
}

(Not tested.)

-Axel

Axel wrote:

I think, your script is missing Thread::join:

end

iplist.each { |host|
�threads << Thread.new(host) { |host|
� domains.each do |domain|
� �check(host,domain,timeout)
� end
�}}

threads.each {|th|
th.join
}

(Not tested.)

-Axel

ops! :slight_smile:

Thank you very much! It seem works fine now :slight_smile:

Glad to hear!
-Axel