logo       

how many thread?!: msg#02356

ruby-talk

Subject: how many thread?!

Hi list,

I've a question about threads counting... :)

------------------------------------------------------------------
#!/usr/bin/ruby

require 'timeout'

timeout = 2
server = %w(127.0.0.1 127.0.0.2 127.0.0.3 127.0.0.4)

def check (host,timeout)
puts "checking... #{host}"
timeout(timeout) do
begin
`/usr/bin/nslookup www.test.org #{host}`
rescue Timeout::Error
end
end
end

threads = []

server.each { |host|
puts Thread.list.size
threads << Thread.new(host) { |host|
check(host,timeout)
}
}

threads.each {|th| th.join }
------------------------------------------------------------------

It will produces:

1
checking... 127.0.0.1
3
checking... 127.0.0.2
5
checking... 127.0.0.3
7
checking... 127.0.0.4

7 threads for 4 requests?!

Without timeout:

1
checking... 127.0.0.1
2
checking... 127.0.0.2
3
checking... 127.0.0.3
4
checking... 127.0.0.4

4 threads for 4 requests!

Why? Timeout produces a new threads?

Thank you,
Al
--
Posted via http://www.ruby-forum.com/.

<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | Mail Home | sitemap | FAQ | advertise