
Reset a finished, failed, or interrupted future to a lazy future
Source:R/core_api-reset.R
reset.Rd
A future that has successfully completed, has been interrupted, or failed due to an error, can be relaunched after resetting it.
Value
reset()
returns a lazy, vanilla Future that can be relaunched.
Resetting a running future results in a FutureError.
Details
A lazy, vanilla Future can be reused in another R session. For instance, if we do:
Then we can read and evaluate the future in another R session using:
Examples
## Like mean(), but fails 90% of the time
shaky_mean <- function(x) {
if (as.double(Sys.time()) %% 1 < 0.90) stop("boom")
mean(x)
}
x <- rnorm(100)
## Calculate the mean of 'x' with a risk of failing randomly
f <- future({ shaky_mean(x) })
## Relaunch until success
repeat({
v <- tryCatch(value(f), error = identity)
if (!inherits(v, "error")) break
message("Resetting failed future, and retry in 0.1 seconds")
f <- reset(f)
Sys.sleep(0.1)
})
#> Resetting failed future, and retry in 0.1 seconds
#> Resetting failed future, and retry in 0.1 seconds
#> Resetting failed future, and retry in 0.1 seconds
#> Resetting failed future, and retry in 0.1 seconds
#> Resetting failed future, and retry in 0.1 seconds
cat("mean:", v, "\n")
#> mean: -0.0300474