irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'statarray'
=> true
irb(main):003:0> a = [1,2,3,4,6]
=> [1, 2, 3, 4, 6]
irb(main):004:0> sa = a.to_statarray
=> [1, 2, 3, 4, 6]
irb(main):005:0> sa.mean
=> 3.2
irb(main):007:0> sa.median
=> 3
Showing posts with label OffRails. Show all posts
Showing posts with label OffRails. Show all posts
Saturday, August 25, 2007
Ruby Statistics Functions
The StatArray class provides some basic statistical operations to be used on arrays:
Saturday, June 23, 2007
Timing out Child Processes

I finally ran across a short Ruby powerpoint lecture by a CMU grad student to get me on the right track. Basically I wanted to spawn a process that me eventually killed (or disappears magically) after a certain amount of time. The answer is to use Timeout.
Here is my code. Notice I got rid of the fork.
#!/usr/bin/env ruby
require 'time'
require 'timeout'
require 'pp'
def time_cmd(command,timeout)
cmd_output = []
puts "Entering time_cmd at #{Time.now.tv_sec}"
begin
status = Timeout.timeout(timeout) do
p = IO.popen(command) do |f|
puts "pid: #{$$}"
f.each_line do |g|
cmd_output << g
system("ps aux | grep netstat")
end
system("ps aux | grep netstat")
end
end
pp status
rescue Timeout::Error
puts "\n#{command} completed at #{Time.now.tv_sec}"
return cmd_output
end
end
puts "pid: #{$$}"
puts time_cmd("netstat -dwh 1",3)
system("ps aux | grep netstat")
With the following output
franz-g4:~/dev/playin/ruby mdfranz$ ./angrytimeout.rb
pid: 3658
Entering time_cmd at 1182709241
pid: 3658
root 3659 2.2 0.0 27332 368 p2 S+ 1:20PM 0:00.01 netstat -d
root 3659 1.3 0.0 27332 368 p2 S+ 1:20PM 0:00.01 netstat -d
root 3659 0.1 0.0 27332 368 p2 S+ 1:20PM 0:00.01 netstat -d
root 3659 0.0 0.0 27332 368 p2 S+ 1:20PM 0:00.01 netstat -d
netstat -dwh 1 completed at 1182709244
input (Total) output
packets errs bytes packets errs bytes colls drops
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
What I still haven't figured out how to do is get the process id of what is spawned by the popen so that I could Process.kill it if I needed to. I know I have been able to do this but it must have been with the Python subprocess module. which of course is more full featured.
Subscribe to:
Posts (Atom)
