Joel VanderWerf
11/20/2006 3:39:00 AM
Jason Mayer wrote:
> Sorry, reading this email reminded me of my project. Say I wanted to do a
> bunch of pings using backticks (you may have asked my stupid n00b questin
> about ``). If I wanted to do a bunch of pings, which would take some small
> amount of time, would it be better(faster) to start new threads before
> doing
> the `ping whatever`, or is there a better/different way?
Sure, why not use threads:
t0 = Time.now
threads = (1..10).map do |i|
Thread.new do
addr = "192.168.0.#{i}"
[addr, `ping -w3 -nq #{addr}`]
end
end
values = threads.map {|thread| thread.value}
t1 = Time.now
puts "Finished in #{t1 - t0} seconds"
require 'yaml'
y values.select {|addr, out| out !~ / 0 received/}
__END__
Output:
Finished in 3.011614 seconds
---
- - 192.168.0.1
- |
PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
--- 192.168.0.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 2.032/3.052/4.750/1.210 ms
...
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407