Time out for a R Function
----------------------------------------
Date: Tue, 7 Dec 2010 16:11:42 +1100 From: michael.bedward at gmail.com To: santosh.srinivas at gmail.com; r-help at r-project.org 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
______________________________________________ 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.
______________________________________________ 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.