On Mon, 13 Sep 2004, Christoph Neubauer wrote:

> 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
===============================================================================