How Does Settimelimit Work in R

setTimeLimit fails to terminate idle call in R

The question was answered by Simon Urbanek on the r-devel mailing list. Below a copy of his response for future reference:

What causes this difference?

The time limit can only be checked in R_ProcessEvents() so for all practical purposes it can be only triggered by interruptible code that calls R_CheckUserInterrupt().
Now, it is entirely up to the front-end to decide how it will the the event loop. For example the terminal version of R has no other interrupts to worry about other than input handlers which trigger asynchronously, so it doesn't need to do any polling. Sys.sleep() only triggers on input handlers, so if you don't have any external event source hook as input handler, there is no reason to process any events so Sys.sleep() won't see any reason to check the time limit.

Is there something I can set in my terminal R session such that the time limit is triggered?

On OS X it's actually very easy: quartz(); dev.off() will do the trick. The reason is that Quartz needs to force the event loop in order to process events from the window asynchronously. It does so by installing a timer-based input handler. This handler will make sure that Sys.sleep() will wake up every 100ms (you can change the value using QuartzCocoa_SetLatency) so it will timeout with that resolution:

> testlimit <- function(){
+ setTimeLimit(elapsed=3, transient=TRUE);
+ Sys.sleep(10);
+ }
> system.time(testlimit());
Error in Sys.sleep(10) : reached elapsed time limit
Timing stopped at: 0 0.001 10.001
> quartz(); dev.off()
null device
1
> testlimit <- function(){
+ setTimeLimit(elapsed=3, transient=TRUE);
+ Sys.sleep(10);
+ }
> system.time(testlimit());
Error in Sys.sleep(10) : reached elapsed time limit
Timing stopped at: 0.002 0.003 3.019

On Linux, there is no built-in timer, so you'd have to add an input handler that will pre-empt Sys.sleep(). If you want a constant timer, you can simply borrow the code from Quartz (have a look at QuartzCocoa_SetupEventLoop in src/library/grDevices/src/qdCocoa.m) or the CarbonEL package. It's really just a pipe that is added as an input handler into which you write asynchronously when you want to wake up the event loop. On top of my head I can't think of a built-in solution in R at this point (even though it could be argued that R might install a handler itself when the limit is set ...).

But note that this is really just a special case of of Sys.sleep(). If you actually run R code, then ProcessEvents is triggered automatically during the evaluation (or in interruptible C code).

Cheers,
Simon

R - Set execution time limit in loop

The function evalWithTimeout of package R.utils does this.

evalWithTimeout(Sys.sleep(10), timeout = 1)

(times are in seconds).

Note: I have not used this function a lot, I liked your question so I did some googling around and found this.

setTimeLimit persists after function completes

I get the same error message (running R-2.16 devel under Win 7).

The help page ?setTimeLimit states:

‘setTimeLimit’ sets limits which apply to each top-level computation,
that is a command line (including any continuation lines) entered at
the console or from a file.

So it isn't really designed to be run from inside a function. If you want to do that, then try this alternative:

timed_computation <- function(expr, time_limit = 1)
{
setTimeLimit(elapsed = time_limit, transient = TRUE)
ans <- expr
setTimeLimit(elapsed = Inf, transient = TRUE)
ans
}

timed_computation(1 + 1) #should return 2
timed_computation(Sys.sleep(2)) #should throw an error

Interrupting readline() after a time interval in R

i think you can use fucntion "setTimeLimit" from library base. so...

record.events <- function(duration = 30, first.event = "a"){
# Initial settings
time.start <- proc.time()[3]
events<-first.event
stroke.time<-c(0)

# Timed data collection
while(proc.time()[3] - time.start < duration){
temp <- tryCatch({setTimeLimit(elapsed=(time.start + duration - proc.time()[3]),
transient = TRUE);readline("record events...")},
error = function(e) { return("NULL")})
#you need to set up back this function... (but why i dont know????)
setTimeLimit(elapsed = Inf, transient = TRUE)

events[length(events)+1] <- temp
stroke.time[length(stroke.time)+1]<-round(proc.time()[3],3)
}

# Format recorded data for post-processing

events<-data.frame(events, stroke.time)
return(events)
}

But setTimeLimit inst great for use in user functions.. My results is:

  events stroke.time
1 a 0.00
2 s 1539.12
3 s 1539.52
4 ass 1539.96
5 s 1540.49
6 asd 1540.94
7 fed 1541.27
8 NULL 1541.55

For more info see:

https://stackoverflow.com/a/7891479

https://stat.ethz.ch/R-manual/R-devel/library/base/html/setTimeLimit.html

How does setTimeLimit work in R?

setTimeLimit fails to terminate idle call in R

How to stop a function in R that is taking too long and give it an alternative?

The R package R.utils has a function evalWithTimeout that's pretty much exactly what you're describing. If you don't want to install a package, evalWithTimeout relies on the less user friendly R base function setTimeLimit

Your code would look something like this:

library(R.utils)

slow.func <- function(x){
Sys.sleep(10)
return(x^2)
}

fast.func <- function(x){
Sys.sleep(2)
return(x*x)
}
interruptor = function(FUN,args, time.limit, ALTFUN){
results <- NULL
results <- evalWithTimeout({FUN(args)},timeout=time.limit,onTimeout="warning")
if(results==NULL){
results <- ALTFUN(args)
}
return(results)
}
interruptor(slow.func,args=2,time.limit=3,fast.func)

Skipping slow tasks in a loop in R

Using https://www.rdocumentation.org/packages/R.utils/versions/2.5.0/topics/withTimeout as a source. Here's a test unit that works as expected.

foo = function() {
print("Tic");
x <- ceiling(runif(1) * 100)+1;
for (kk in 1:x) {
print(kk);
Sys.sleep(runif(1));
}
print("Tac");
}

bar = function() {
for (i in 1:100) {
tryCatch({
res <- withTimeout({
foo();
}, timeout=1.08);
}, TimeoutException=function(ex) {
cat("Timeout. Skipping.\n");
});
print(i);
}
}

So the question is, is there an error that is thrown by interrupting by sim.rateshift.taxa that is not being caught, use error as thc mentions in order to catch that but use the TimeoutException to skip over proper timeouts

There is also a problem with setting too low a time limit:

https://github.com/mhahsler/arules/issues/22

You may want to simply use setTimeLimit yourself and ensure that transient is set to TRUE that way you have finer control.

Here's an example taken from http://blog.revolutionanalytics.com/2014/10/r-in-production-controlling-runtime.html

system.time(Sys.sleep(5))

##user system elapsed
## 0.000 0.000 5.005

system.time(local({
setTimeLimit(elapsed = 1, transient = TRUE)
Sys.sleep(5)
}))

## Error in Sys.sleep(5): reached elapsed time limit

## Timing stopped at: 0 0 5.006

How I can set time limit for R code executing by python (rpy2)?

The evaluation of R code does not release the Python GIL. The only way to get a Python script to monitor the execution time of R code is to have two processes.

You could check the unit test for rpy2 "testInterruptR()", although there are much more elegant ways to implement that in an application. There a SIGINT is sent to an R process running an infinite loop.



Related Topics



Leave a reply



Submit