What is the best way to timeout

R

rj-cole

Hi,

I'm trying to write a function that will spawn a process, and after a
timeout kill the process if it hasn't terminated yet.

def spawn(inputString, cmd, outputFileName)
inp = IO.popen("(" + cmd + ") > #{outputFileName} ", "w")
inp << inputString
inp.close_write
end

What is the best way to change this so that (i) it works, and (ii) it
gives the process some number of seconds to complete, and if it hasn't
completed then it kills it and returns an error?

This code only needs to run under linux.

regards,

Richard.
 
D

Dave Burt

rj-cole said:
I'm trying to write a function that will spawn a process, and after a
timeout kill the process if it hasn't terminated yet.

def spawn(inputString, cmd, outputFileName)
inp = IO.popen("(" + cmd + ") > #{outputFileName} ", "w")
inp << inputString
inp.close_write
end

What is the best way to change this so that (i) it works, and (ii) it
gives the process some number of seconds to complete, and if it hasn't
completed then it kills it and returns an error?

Use Timeout from the Standard Library.

require 'timeout'
Timeout.timeout(60) do
spawn foo, bar, baz
end

Cheers,
Dave

Cheers,
Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top