# Multithread e Ruby

**URL:** <https://www.ruby-forum.com/t/multithread-e-ruby/210794>\
**Category:** IT\
**Created:** [August 17, 2011, 7:24pm UTC](https://www.ruby-forum.com/t/multithread-e-ruby/210794 "2011-08-17T19:24:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [August 17, 2011, 7:24pm UTC](https://www.ruby-forum.com/t/multithread-e-ruby/210794/1 "2011-08-17T19:24:37Z")

</div>

Ciao a tutti,  
dovrei scandire un lunghissimo elenco di domini per verificarne la  
validità.  
In rete ho trovato questo script e vorrei modificarlo in modo tale che  
riesca a gestire anche un elenco di 100.000 domini.  
Qualcuno potrebbe aiutarmi.  
Ho fatto vari tentativi ma ho notato che mettendo un ciclo e ciclando su  
tutti i domini alla fine l’output è errato perché penso che non vengano  
finiti tutti i thread con l’istruzione EM.stop.

require “rubygems”  
require “resolv”  
require “eventmachine”

class Dns  
include EM::Deferrable  
def resolve\_hostname(hostname)  
begin  
ip = Resolv.getaddress(hostname)  
set\_deferred\_status :succeeded, ip  
rescue Exception =\> ex  
set\_deferred\_status :failed, ex.to\_s  
end  
end  
end

start = Time.now  
EventMachine.run {  
p “Requesting DNS info for yahoo”  
dns0 = Dns.new  
dns0.callback {|response| p “For yahoo #{response}”}  
dns0.errback {|response| p “For yahoo #{response}”}  
Thread.new { dns0.resolve\_hostname “[www.yahoo.com](http://www.yahoo.com)”}

```
p "Requesting DNS info for google"
dns1 = Dns.new
dns1.callback {|response| p "For google #{response}"}
dns1.errback {|response| p "For google #{response}"}
Thread.new { dns1.resolve_hostname "www.google.com"}

p "Requesting DNS info for twitter"
dns2 = Dns.new
dns2.callback {|response| p "For twitter #{response}"}
dns2.errback {|response| p "For twitter #{response}"}
Thread.new { dns2.resolve_hostname "twitter.com"}

p "Requesting DNS info for github"
dns3 = Dns.new
dns3.callback {|response| p "For github #{response}"}
dns3.errback {|response| p "For github #{response}"}
Thread.new {dns3.resolve_hostname "github.com"; EM.stop }

```

}  
finish = Time.now

p “Time take for querying #{finish - start} seconds”
