View source: R/core_api-cancel.R
| cancel | R Documentation |
Cancels futures, with the option to interrupt running ones.
cancel() is vectorized, i.e. accepts a collection of futures
to be canceled.
cancel(x, interrupt = TRUE, ...)
x |
A Future, a list, an environment, or a list environment (listenv). Lists and environments may hold a mix of futures and non-future elements, and may be nested, e.g. a list of lists of futures; every future found, at any depth, is canceled. |
interrupt |
If TRUE, running futures are interrupted, if the future backend supports it. |
... |
All arguments used by the S3 methods. |
cancel() returns (invisibly) x after canceling every Future
therein, that is, after flagging each one as "canceled" and
possibly interrupting it as well.
Canceling a lazy or a finished future has no effect. Elements of
x that are neither futures nor collections of futures are left
untouched.
A canceled future can be reset() to a lazy, vanilla future
such that it can be relaunched, possibly on another future backend.
## Set up two parallel workers
plan(multisession, workers = 2)
## Launch two long running futures
fs <- lapply(c(1, 2), function(duration) {
future({
Sys.sleep(duration)
42
})
})
## Wait until at least one of the futures is resolved
while (!any(resolved(fs))) Sys.sleep(0.1)
## Cancel futures that are not yet resolved
r <- resolved(fs)
cancel(fs[!r])
## Get the value of the resolved future
f <- fs[r]
v <- value(f)
message("Result: ", v)
## The value of the canceled future is an error
try(v <- value(fs[!r]))
## Shut down parallel workers
plan(sequential)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.