| 1 |
--- |
| 2 |
title: "Advanced future and promises usage" |
| 3 |
output: rmarkdown::html_vignette |
| 4 |
vignette: > |
| 5 |
%\VignetteEngine{knitr::rmarkdown} |
| 6 |
%\VignetteIndexEntry{Advanced future and promises usage} |
| 7 |
%\VignetteEncoding{UTF-8} |
| 8 |
resource_files: |
| 9 |
- future_promise/future.png |
| 10 |
- future_promise/blocked_future_promise.png |
| 11 |
--- |
| 12 |
|
| 13 |
This article discusses the benefits of using `promises::future_promise()` over a combination of `future::future()` + `promises::promise()` to better take advantage of computing resources available to your main R session. To demonstrate these benefits, we'll walk-through a use-case with the `plumber` package. ([See here](https://www.rplumber.io/) to learn more about `plumber` and [the previous article](promises_05a_futures.html) to learn more about `future`.) |
| 14 |
|
| 15 |
## The problem with `future()`+`promise()` |
| 16 |
|
| 17 |
In an ideal situation, the number of available `future` _workers_ (`future::nbrOfFreeWorkers()`) is always **more than** the number of `future::future()` _jobs_. However, if a `future` job is attempted when the number of free workers is `0`, then `future` will block the current R session until one becomes available. |
| 18 |
|
| 19 |
For a concrete example, let's imagine a scenario, where seven `plumber` requests are received at the same time with only two `future` workers available. Also, let's assume the `plumber` route(s) serving the first 6 requests use `future::future()` and take ~10s to compute `slow_calc()`: |
| 20 |
|
| 21 |
```r |
| 22 |
#* @get /slow/<k> |
| 23 |
\() { |
| 24 |
future::future({ |
| 25 |
slow_calc() |
| 26 |
}) |
| 27 |
} |
| 28 |
``` |
| 29 |
|
| 30 |
Let's also assume the `plumber` route serving the last request does not use any form of `future` or `promises` and takes almost no time to compute. |
| 31 |
|
| 32 |
```r |
| 33 |
#* @get /fast/<k> |
| 34 |
\() { |
| 35 |
fast_calc() |
| 36 |
} |
| 37 |
``` |
| 38 |
|
| 39 |
|
| 40 |
The figure below depicts the overall timeline of execution of these 7 requests under the conditions we've outlined above. Note that the y-axis is ordered from first request coming in (`/slow/1`) to the last request (`/fast/7`). |
| 41 |
|
| 42 |
<img src = "future_promise/future.png" width = "75%" alt="Early workers take more time than expected. Main R session is blocked" /> |
| 43 |
|
| 44 |
Note how R has to wait 20s before processing the 7th request (shown in green). This is a big improvement over not using `future`+`promises` at all (in that case, R would have to wait 60s before processing). However, since there are only two `future` workers available R still has to wait longer than necessary to process that last request because the main R session must wait for a `future` worker to become available. The video below animates this behavior: |
| 45 |
|
| 46 |
|
| 47 |
```{r, echo = FALSE} |
| 48 |
library(vembedr) |
| 49 |
embed_vimeo("505287449") |> |
| 50 |
use_align("center") |
| 51 |
``` |
| 52 |
|
| 53 |
|
| 54 |
## The solution: `future_promise()` |
| 55 |
|
| 56 |
The advantage of using `future_promise()` over `future::future()` is that even if there aren't `future` workers available, the `future` is scheduled to be done when workers become available via `promises`. In other words, `future_promise()` ensures the main R thread isn't blocked when a `future` job is requested and can't immediately perform the work (i.e., the number of jobs exceeds the number of workers). |
| 57 |
|
| 58 |
Continuing with the example above, we can swap out the calls to `future::future()` with `future_promise()`. |
| 59 |
|
| 60 |
```r |
| 61 |
#* @get /slow/<k> |
| 62 |
\() { |
| 63 |
promises::future_promise({ |
| 64 |
slow_calc() |
| 65 |
}) |
| 66 |
} |
| 67 |
``` |
| 68 |
|
| 69 |
|
| 70 |
With this change to `future_promise()`, note how the `/fast/7` route now does not have to wait on `future` work to finish processing. Therefore, `plumber` can complete the last requests almost immediately: |
| 71 |
|
| 72 |
<img src = "future_promise/blocked_future_promise.png" width="75%" alt="future_promise() keeps the main R session free" /> |
| 73 |
|
| 74 |
The vertical gray bars in the figure above represent timepoints where the main R session is actually busy. Outside of these gray areas, the R session is free to do other things, for example, executing other `promises` or, more generally, non-`future` work. The video below animates this behavior: |
| 75 |
|
| 76 |
```{r, echo = FALSE} |
| 77 |
library(vembedr) |
| 78 |
embed_vimeo("505286442") |> |
| 79 |
use_align("center") |
| 80 |
``` |
| 81 |
|
| 82 |
<div style="font-size: 20px; margin-top: 40px; text-align: right;"> |
| 83 |
Next: [Using `promises` with Shiny](promises_06_shiny.html) |
| 84 |
</div> |