Debian Codegraph @

man/future_promise.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/future_promise.R
3 \name{future_promise_queue}
4 \alias{future_promise_queue}
5 \alias{future_promise}
6 \title{\pkg{future} promise}
7 \usage{
8 future_promise_queue()
9
10 future_promise(
11 expr = NULL,
12 envir = parent.frame(),
13 ...,
14 substitute = TRUE,
15 queue = future_promise_queue()
16 )
17 }
18 \arguments{
19 \item{expr}{An R expression. While the \code{expr} is eventually sent to \code{\link[future:future]{future::future()}}, please use the same precautions that you would use with regular \code{promises::promise()} expressions. \code{future_promise()} may have to hold the \code{expr} in a \code{\link[=promise]{promise()}} while waiting for a \pkg{future} worker to become available.}
20
21 \item{envir}{The \link{environment} from where global objects should be
22 identified.}
23
24 \item{...}{extra parameters provided to \code{\link[future:future]{future::future()}}}
25
26 \item{substitute}{If TRUE, argument \code{expr} is
27 \code{\link[base]{substitute}()}:ed, otherwise not.}
28
29 \item{queue}{A queue that is used to schedule work to be done using \code{\link[future:future]{future::future()}}. This queue defaults to \code{\link[=future_promise_queue]{future_promise_queue()}} and requires that method \code{queue$schedule_work(fn)} exist. This method should take in a function that will execute the promised \pkg{future} work.}
30 }
31 \value{
32 Unlike \code{\link[future:future]{future::future()}}, \code{future_promise()} returns a \code{\link[=promise]{promise()}} object that will eventually resolve the \pkg{future} \code{expr}.
33 }
34 \description{
35 When submitting \pkg{future} work, \pkg{future} (by design) will block the main R session until a worker becomes available.
36 This occurs when there is more submitted \pkg{future} work than there are available \pkg{future} workers.
37 To counter this situation, we can create a promise to execute work using future (using \code{future_promise()}) and only begin the work if a \pkg{future} worker is available.
38 }
39 \details{
40 Using \code{future_promise()} is recommended whenever a continuous runtime is used, such as with \pkg{plumber} or \pkg{shiny}.
41
42 For more details and examples, please see the \href{https://rstudio.github.io/promises/articles/promises_05b_future_promise.html}{\code{vignette("future_promise", "promises")}} vignette.
43 }
44 \section{Functions}{
45 \itemize{
46 \item \code{future_promise_queue()}: Default \code{future_promise()} work queue to use. This function returns a \code{\link{WorkQueue}} that is cached per R session.
47
48 \item \code{future_promise()}: Creates a \code{\link[=promise]{promise()}} that will execute the \code{expr} using \code{\link[future:future]{future::future()}}.
49
50 }}
51 \examples{
52 \dontrun{# Relative start time
53 start <- Sys.time()
54 # Helper to force two `future` workers
55 with_two_workers <- function(expr) {
56 if (!require("future")) {
57 message("`future` not installed")
58 return()
59 }
60 old_plan <- future::plan(future::multisession, workers = 2)
61 on.exit({future::plan(old_plan)}, add = TRUE)
62 start <<- Sys.time()
63 force(expr)
64 while(!later::loop_empty()) {Sys.sleep(0.1); later::run_now()}
65 invisible()
66 }
67 # Print a status message. Ex: `"PID: XXX; 2.5s promise done"`
68 print_msg <- function(pid, msg) {
69 message(
70 "PID: ", pid, "; ",
71 round(difftime(Sys.time(), start, units = "secs"), digits = 1), "s " ,
72 msg
73 )
74 }
75
76 # `"promise done"` will appear after four workers are done and the main R session is not blocked
77 # The important thing to note is the first four times will be roughly the same
78 with_two_workers({
79 promise_resolve(Sys.getpid()) |>
80 then(\(x) {print_msg("promise done")})
81 for (i in 1:6) {
82 future::future({Sys.sleep(1); Sys.getpid()}) |>
83 then(\(x) {print_msg("future done")})
84 }
85 })
86 {
87 #> PID: XXX; 2.5s promise done
88 #> PID: YYY; 2.6s future done
89 #> PID: ZZZ; 2.6s future done
90 #> PID: YYY; 2.6s future done
91 #> PID: ZZZ; 2.6s future done
92 #> PID: YYY; 3.4s future done
93 #> PID: ZZZ; 3.6s future done
94 }
95
96 # `"promise done"` will almost immediately, before any workers have completed
97 # The first two `"future done"` comments appear earlier the example above
98 with_two_workers({
99 promise_resolve(Sys.getpid()) |>
100 then(\(x) {print_msg("promise")})
101 for (i in 1:6) {
102 future_promise({Sys.sleep(1); Sys.getpid()}) |>
103 then(\(x) {print_msg("future done")})
104 }
105 })
106 {
107 #> PID: XXX; 0.2s promise done
108 #> PID: YYY; 1.3s future done
109 #> PID: ZZZ; 1.4s future done
110 #> PID: YYY; 2.5s future done
111 #> PID: ZZZ; 2.6s future done
112 #> PID: YYY; 3.4s future done
113 #> PID: ZZZ; 3.6s future done
114 }}
115 }
116 \seealso{
117 \code{\link{WorkQueue}}
118 }