Quantcast

Time out for a R Function

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Time out for a R Function

Santosh Srinivas
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?

Thank you.
S

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Time out for a R Function

mbedward
Below is a toy function with one way of doing it. There are bound to
be better ways :)

function(niter = 10, time.out = 3) {
  pretend.task <- function() {
  Sys.sleep(0.5)
  }

  start <- proc.time()

  for (iter in 1:niter) {
    pretend.task()
    cur <- proc.time() - start
    if (cur[3] > time.out) return("timed out")
  }

  return("completed")
}


On 7 December 2010 13:04, Santosh Srinivas <[hidden email]> 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?
>
> Thank you.
> S
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Time out for a R Function

Henrik Bengtsson-3
See also R help thread 'Wait for keystroke or timeout':

http://www.mail-archive.com/r-help@.../msg97742.html

/H

On Mon, Dec 6, 2010 at 9:11 PM, Michael Bedward
<[hidden email]> wrote:

> Below is a toy function with one way of doing it. There are bound to
> be better ways :)
>
> function(niter = 10, time.out = 3) {
>  pretend.task <- function() {
>        Sys.sleep(0.5)
>  }
>
>  start <- proc.time()
>
>  for (iter in 1:niter) {
>    pretend.task()
>    cur <- proc.time() - start
>    if (cur[3] > time.out) return("timed out")
>  }
>
>  return("completed")
> }
>
>
> On 7 December 2010 13:04, Santosh Srinivas <[hidden email]> 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?
>>
>> Thank you.
>> S
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Time out for a R Function

Prof Brian Ripley
In reply to this post by Santosh Srinivas
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,                  [hidden email]
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Time out for a R Function

Mike Marchywka
In reply to this post by mbedward





----------------------------------------
> Date: Tue, 7 Dec 2010 16:11:42 +1100
> From: [hidden email]
> To: [hidden email]; [hidden email]
> Subject: Re: [R] Time out for a R Function
>
> Below is a toy function with one way of doing it. There are bound to
> be better ways :)

This seems to just check total time on each iteration.
Presumably for this you would be better off setting an
ieration limit, not a timeout. A watchdog timer to insure
that pretend task doesn't hang would need another thread
and you would either need the target task to check
a shared variable that you set from the watchdog or
you just kill the target thread from watchdog but
this may be hard to do in a way that lets you continue
in any meaningful way.



>
> function(niter = 10, time.out = 3) {
> pretend.task <- function() {
> Sys.sleep(0.5)
> }
>
> start <- proc.time()
>
> for (iter in 1:niter) {
> pretend.task()
> cur <- proc.time() - start
> if (cur[3] > time.out) return("timed out")
> }
>
> return("completed")
> }
>
>
> On 7 December 2010 13:04, 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?
> >
> > Thank you.
> > S
> >
> > ______________________________________________
> > [hidden email] 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.
> >
>
> ______________________________________________
> [hidden email] 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.
     
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Time out for a R Function

Santosh Srinivas
In reply to this post by Prof Brian Ripley
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:[hidden email]]
Sent: 07 December 2010 13:23
To: Santosh Srinivas
Cc: [hidden email]
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,                  [hidden email]
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Time out for a R Function

Henrik Bengtsson-3
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
<[hidden email]> 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:[hidden email]]
> Sent: 07 December 2010 13:23
> To: Santosh Srinivas
> Cc: [hidden email]
> 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,                  [hidden email]
> 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
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Loading...