How to Control Time Behaviour of System Call ?

C

Christoph Neubauer

Hi Rubyists !

Using Ruby 1.6.8 on Windows.

What I'd like to do is something like:

timeout (n) do
system ("Call some complex C(++) code, that (maybe) does (not)
return to Ruby")
end

As far as I understand, "system" steps out of the Ruby sandbox,
such bypassing the timeout control. If the "Call" remains hanging,
the ruby script will 'never' get back control.

That's no good behaviour for the controller of a regression test.

Any ideas for solution or workaround welcomed !
Chris
 
R

Robert Klemme

Christoph Neubauer said:
Hi Rubyists !

Using Ruby 1.6.8 on Windows.

What I'd like to do is something like:

timeout (n) do
system ("Call some complex C(++) code, that (maybe) does (not)
return to Ruby")
end

As far as I understand, "system" steps out of the Ruby sandbox,
such bypassing the timeout control. If the "Call" remains hanging,
the ruby script will 'never' get back control.

That's no good behaviour for the controller of a regression test.

Any ideas for solution or workaround welcomed !

Use popen in a different thread.

Regards

robert
 
A

Ara.T.Howard

Hi Rubyists !

Using Ruby 1.6.8 on Windows.

What I'd like to do is something like:

timeout (n) do
system ("Call some complex C(++) code, that (maybe) does (not)
return to Ruby")
end

As far as I understand, "system" steps out of the Ruby sandbox,
such bypassing the timeout control. If the "Call" remains hanging,
the ruby script will 'never' get back control.

That's no good behaviour for the controller of a regression test.

Any ideas for solution or workaround welcomed !
Chris


you may be able to use this unmodfified using the windows-process package from

http://raa.ruby-lang.org/search.rhtml?search=win+fork


module ForkTimeout
#{{{
class TimeoutError < StandardError; end
def timeout n, sig = 'SIGUSR1'
#{{{
ret = nil
cid = fork
unless cid
trap('SIGQUIT'){ exit! }
sleep(n)
Process.kill(sig, Process.ppid) rescue nil
exit!
else
begin
handler = trap(sig){ raise(TimeoutError, "timedout <#{ n }>") }
ret = yield
ensure
trap(sig, handler)
Process.kill('SIGQUIT', cid) rescue nil
Process.wait rescue nil
end
end
ret
#}}}
end
module_function 'timeout'
public 'timeout'
#}}}
end

if $0 == __FILE__
ForkTimeout::timeout(0.42){ sleep(0.042) and puts(42) }
ForkTimeout::timeout(0.042){ sleep(0.42) }
end


jib:~ > ruby fork_timeout.rb
42
fork_timeout.rb:15:in `timeout': timedout <0.042> (ForkTimeout::TimeoutError)
from fork_timeout.rb:15:in `call'
from fork_timeout.rb:34:in `sleep'
from fork_timeout.rb:34
from fork_timeout.rb:34:in `timeout'
from fork_timeout.rb:34


let me know if it works for you - i don't have a windows box to test on.

cheers.

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================
 

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

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,883
Members
47,414
Latest member
djangoframe

Latest Threads

Top