# Thread exec

**URL:** <https://www.ruby-forum.com/t/thread-exec/172952>\
**Category:** Ruby\
**Created:** [July 30, 2009, 2:51pm UTC](https://www.ruby-forum.com/t/thread-exec/172952 "2009-07-30T14:51:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![al\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f08c70/32.png) [@al\_S](https://www.ruby-forum.com/u/al_S)\
**Post date:** [July 30, 2009, 2:51pm UTC](https://www.ruby-forum.com/t/thread-exec/172952/1 "2009-07-30T14:51:40Z")

</div>

Hi List,

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

…  
…  
domains = [‘[www.bim.com](http://www.bim.com)’,‘[www.bum.com](http://www.bum.com)’,‘[www.bam.com](http://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](http://www.bim.com)  
checking… 127.0.0.2 → [www.bim.com](http://www.bim.com)  
checking… 127.0.0.3 → [www.bim.com](http://www.bim.com)  
checking… 127.0.0.4 → [www.bim.com](http://www.bim.com)  
checking… 127.0.0.5 → [www.bim.com](http://www.bim.com)  
checking… 127.0.0.6 → [www.bim.com](http://www.bim.com)  
checking… 127.0.0.7 → [www.bim.com](http://www.bim.com)  
checking… 127.0.0.8 → [www.bim.com](http://www.bim.com)  
checking… 127.0.0.9 → [www.bim.com](http://www.bim.com)

Thank you very much,  
Al

---

<div class="post-metadata">

**Author:** ![al\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f08c70/32.png) [@al\_S](https://www.ruby-forum.com/u/al_S)\
**Post date:** [July 30, 2009, 3:16pm UTC](https://www.ruby-forum.com/t/thread-exec/172952/2 "2009-07-30T15:16:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![al\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f08c70/32.png) [@al\_S](https://www.ruby-forum.com/u/al_S)\
**Post date:** [July 30, 2009, 3:29pm UTC](https://www.ruby-forum.com/t/thread-exec/172952/3 "2009-07-30T15:29:53Z")

</div>

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! 🙂

Thank you very much! It seem works fine now 🙂

---

<div class="post-metadata">

**Author:** ![al\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f08c70/32.png) [@al\_S](https://www.ruby-forum.com/u/al_S)\
**Post date:** [July 30, 2009, 3:43pm UTC](https://www.ruby-forum.com/t/thread-exec/172952/4 "2009-07-30T15:43:48Z")

</div>

Glad to hear!  
-Axel
