eval.within.time(expr, secs, dsecs = c(0.001, 0.1), time = as.difftime(secs, units = "secs"), verbose = FALSE, write.obj = saveRDS, read.obj = readRDS, make.con = tempfile, cleanup.con = function(con) if (file.exists(con)) unlink(con), touch.con = function(con) writeLines(character(), con), con.touched = file.exists, make.signal.con = make.con, cleanup.signal.con = cleanup.con)
time
is
provied.difftime
object giving the timeout interval.
Default: as.difftime(secs, units = "secs")
, which simply
means to build a difftime
object from the secs
argument.
If this argument is provided then secs
is ignored.saveRDS
.readRDS
.tempfile
.function(con) if(file.exists(con)) unlink(con)
.function(con) writeLines(character(), con)
. This is used
to signal through the signal file.file.exists
.make.con
, but for the signal file.
Default: make.con
.cleanup.con
, but for the signal file.
Default: cleanup.con
.expr
tryCatch
block to handle the error gracefully.)The implementation uses the fork
package, which is loaded---an
error is thrown if unavailable. In fact, the parent sleeps for short
intervals (controlled by dsecs
param), each time waking up to
check if either the time has elapsed or the child has finished, then
acting accordingly.
The way the child signals to the parent is via the filesystem. There are two such files: the result file and the signal file. The child writes the result of the calculation to disk as a serialized R object. Usually you should try to keep this small. Then the child touches a second file, called the "signal" file, which signals that it is finished. Both of these are temporary files. While in loop, the parent checks for existence of the signal file. After exiting the loop, the parent reads the result file. An attempt is made to delete both files before returning or throwing an error.
The child process evalutes your expression within a try block. If this evaluation results in an error, then the captured error object is passed to the parent, which then throws it again.
It is possible that the child would *start* writing the result but not finish before the time elapses. That would be considered a timeout. The thing which the parent checks is if the signal file exists.
Don't be intimidated by the large number of arguments. Typically usage involves only the first two.