[R] Time out for a R Function

Henrik Bengtsson hb at biostat.ucsf.edu
Tue Dec 14 03:27:46 CET 2010


FYI,

I've added evalWithTimeout() to R.utils v1.6.0 (now on CRAN).  It
utilizes the built-in setTimeLimit() feature of R.

You can either have it throw an error, which you then have to catch,
or you can have it just give a warning that your evaluation timed out
and then silently continue.  From example(evalWithTimeout):

# - - - - - - - - - - - - - - - - - - - - - - - - -
# Function that takes "a long" time to run
# - - - - - - - - - - - - - - - - - - - - - - - - -
foo <- function() {
  print("Tic");
  for (kk in 1:100) {
    print(kk);
    Sys.sleep(0.1);
  }
  print("Tac");
}


# - - - - - - - - - - - - - - - - - - - - - - - - -
# Evaluate code, if it takes too long, generate
# a timeout by throwing a TimeoutException.
# - - - - - - - - - - - - - - - - - - - - - - - - -
res <- NULL;
tryCatch({
  res <- evalWithTimeout({
    foo();
  }, timeout=1.08);
}, TimeoutException=function(ex) {
  cat("Timeout. Skipping.\n");
})


# - - - - - - - - - - - - - - - - - - - - - - - - -
# Evaluate code, if it takes too long, generate
# a timeout returning NULL and generate a warning.
# - - - - - - - - - - - - - - - - - - - - - - - - -
res <- evalWithTimeout({
  foo();
}, timeout=1.08, onTimeout="warning");


See if that helps

/Henrik

On Wed, Dec 8, 2010 at 7:14 AM, Santosh Srinivas
<santosh.srinivas at gmail.com> wrote:
> Thank you.
>
> I tried this but not sure if I have implemented this correctly ... basically
> if function1 hangs .. I need the timeout to be triggered and then the
> process moves to the next function call.
>
> I have this:
>
> function1 <- function(x){
> setTimeLimit(elapsed = 5*60, transient = FALSE)
> step 1
> step 2
> step 3
> }
>
>
> #Code to execute about function repeatedly. It moves to a new input variable
> when I call it back through a parameter
>
> repeat{
>        function1(x)
> }
>
>
>
>
> -----Original Message-----
> From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk]
> Sent: 07 December 2010 13:23
> To: Santosh Srinivas
> Cc: r-help at r-project.org
> Subject: Re: [R] Time out for a R Function
>
> On Tue, 7 Dec 2010, Santosh Srinivas wrote:
>
>> Hello Group,
>>
>> I have an R-function that works fine for most part but sometime runs into
> a
>> long loop! (I'm lazy and short on time to debug right now so want to do
>> something easy)
>> For my purpose, it is ok to make few errors .... is there a way I can put
> a
>> timeout on a function and the r-process needes to move on to the next
> step?
>
> See the help on setTimeLimit(transient = FALSE) .  Unlike other
> suggestions, that does not need you to change the code to check the
> time.
>
> --
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list