Skip to content

Commit 4041e30

Browse files
committed
Update mirai docs and cheatsheet
1 parent 08ef85a commit 4041e30

3 files changed

Lines changed: 38 additions & 30 deletions

File tree

R/mirai.R

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@
3636
#' own independent set of daemons. `NULL` (default) uses the 'default'
3737
#' profile.
3838
#'
39-
#' @return A 'mirai' object.
39+
#' @return For [mirai()]: a 'mirai' object.
40+
#'
41+
#' For [try_mirai()]: a 'mirai' object, or `NULL` (invisibly) if the
42+
#' dispatcher's `capacity` budget is exhausted at the time of submission.
4043
#'
4144
#' @section Evaluation:
4245
#'
@@ -151,24 +154,20 @@ mirai <- function(.expr, ..., .args = list(), .timeout = NULL, .compute = NULL)
151154

152155
#' @rdname mirai
153156
#'
154-
#' @details
157+
#' @section Capacity:
155158
#'
156-
#' [try_mirai()] is a non-blocking variant of [mirai()] for use in event-loop
157-
#' contexts (Shiny, promises) where the host R thread cannot afford to wait
158-
#' for the dispatcher's `capacity` budget to drain. It returns `NULL`
159-
#' (invisibly) immediately if the queue is at capacity at the time of
160-
#' submission, instead of blocking. Where there is no gate to consult (no
161-
#' dispatcher, or `capacity` unset), it always returns a 'mirai' — the
162-
#' contract is "don't block on queue pressure", not "fail when the queue is
163-
#' empty".
159+
#' The `capacity` argument to [daemons()] caps the queued task payload at
160+
#' dispatcher (in MB), preventing host out-of-memory. [mirai()] blocks the
161+
#' calling R thread on submission until queued bytes drop below this budget.
164162
#'
165-
#' Pair with a backpressure policy of your choice — drop, retry, signal
166-
#' upstream — by checking for `NULL`.
163+
#' [try_mirai()] is a non-blocking variant for event-loop contexts (Shiny,
164+
#' promises) where the host R thread cannot afford to wait. It returns `NULL`
165+
#' (invisibly) immediately if the queue is at capacity, instead of blocking.
166+
#' With no dispatcher, or `capacity` unset, [try_mirai()] always returns a
167+
#' 'mirai' — the contract is "don't block on queue pressure". Check for a `NULL`
168+
#' return value and handle accordingly — drop, retry, or signal upstream.
167169
#'
168-
#' @return For [mirai()]: a 'mirai' object.
169-
#'
170-
#' For [try_mirai()]: a 'mirai' object, or `NULL` (invisibly) if the
171-
#' dispatcher's `capacity` budget is exhausted at the time of submission.
170+
#' Use [capacity()] to inspect current and peak queue usage.
172171
#'
173172
#' @examplesIf interactive()
174173
#' # non-blocking submission - caller handles backpressure

man/mirai.Rd

Lines changed: 17 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/mirai.Rmd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ m$data # Returns value (NA if unresolved)
4646
m[] # Wait and return value
4747
collect_mirai(m) # Wait and return value
4848
call_mirai(m) # Wait and return mirai object
49+
50+
# Non-blocking variant (returns NULL if dispatcher capacity exhausted)
51+
m <- try_mirai(task())
4952
```
5053

5154
### Passing Data
@@ -87,6 +90,7 @@ info()
8790
daemons(
8891
n = 4,
8992
dispatcher = TRUE, # Use dispatcher for optimal FIFO scheduling
93+
capacity = NULL, # Memory budget (MB) for queued tasks; NULL = unbounded
9094
cleanup = TRUE, # Clean env between tasks
9195
output = FALSE, # Capture stdout/stderr
9296
maxtasks = Inf, # Task limit per daemon
@@ -377,6 +381,7 @@ m$data$message # Error message
377381

378382
```r
379383
info() # Connection and task statistics
384+
capacity() # Queue memory: used / peak / capacity (MB)
380385

381386
daemons_set() # Check if daemons exist
382387
require_daemons() # Error if not set
@@ -508,6 +513,7 @@ mirai(func(x), func = my_func, x = data)
508513
# Dispatcher Required For
509514
stop_mirai(m) # Cancellation
510515
mirai(task(), .timeout = 1000) # Timeout cancellation
516+
daemons(4, capacity = 100) # Memory backpressure (queue budget in MB)
511517
daemons(4, serial = serial_config(...)) # Custom serialization
512518

513519
# SSH Tunnelling

0 commit comments

Comments
 (0)