From: Robert Klemme Date: 2009-09-07T22:38:24+09:00 Subject: Re: Executing system commands in threads under Ruby 1.8.6 2009/9/7 geo ssscripting : > The commands executed in the script at the SO link was simply pinging > the host on an ip range from 1 to 254 like in the following line : > > `ping #{ip_addr}` > > This happened from inside each individual thread. How could I replace > the backticks so that they won't block ? You can use one of the popen methods, e.g. IO.popen ["ping", ip_addr] do |io| io.each {|l| puts l} end Example 15:37:19 ~$ ruby < threads = (1..2).map do |i| > Thread.new i do |ii| > IO.popen 'ping 192.168.110.74' do |io| > io.each do |line| > printf "%2d %s", ii, line > end > end > end > end > threads.map do |th| > th.value > end > EOF 1 1 Pinging 192.168.110.74 with 32 bytes of data: 1 1 Reply from 192.168.110.74: bytes=32 time<1ms TTL=128 2 2 Pinging 192.168.110.74 with 32 bytes of data: 2 2 Reply from 192.168.110.74: bytes=32 time<1ms TTL=128 1 Reply from 192.168.110.74: bytes=32 time<1ms TTL=128 2 Reply from 192.168.110.74: bytes=32 time<1ms TTL=128 1 Reply from 192.168.110.74: bytes=32 time<1ms TTL=128 2 Reply from 192.168.110.74: bytes=32 time<1ms TTL=128 1 Reply from 192.168.110.74: bytes=32 time<1ms TTL=128 1 1 Ping statistics for 192.168.110.74: 1 Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), 1 Approximate round trip times in milli-seconds: 1 Minimum = 0ms, Maximum = 0ms, Average = 0ms 2 Reply from 192.168.110.74: bytes=32 time<1ms TTL=128 2 2 Ping statistics for 192.168.110.74: 2 Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), 2 Approximate round trip times in milli-seconds: 2 Minimum = 0ms, Maximum = 0ms, Average = 0ms 15:37:38 ~$ ruby -version ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin] -e:1: undefined local variable or method `rsion' for main:Object (NameError) 15:37:53 ~$ Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/