[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

What is the best way to timeout

rj-cole

4/17/2006 8:22:00 PM

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.

1 Answer

Dave Burt

4/17/2006 10:03:00 PM

0

rj-cole wrote:
> 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