Skip to content

Commit 1326204

Browse files
authored
Stable daemons() logical return value (#412)
* Change `daemons()` return value to logical * Update news * Tweak one
1 parent 05d5ffe commit 1326204

File tree

7 files changed

+138
-140
lines changed

7 files changed

+138
-140
lines changed

NEWS.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,35 @@
22

33
#### Behavioural Changes
44

5-
* `daemons()` creating new daemons returns invisibly the character compute profile created.
6-
`daemons(0)` or `daemons(NULL)` resetting daemons returns invisible NULL (thanks @eliocamp, #384).
7-
* Calling `daemons()` will now reset any existing daemons for the same compute profile rather than error.
8-
This means that an explicit `daemons(0)` is no longer required before applying new settings (#383).
5+
* `daemons()` now returns invisibly logical `TRUE` when creating daemons and `FALSE` when resetting them, for simplicity and consistency (#384).
6+
* `daemons()` creating new daemons now resets any existing daemons for the compute profile rather than error.
7+
This means that an explicit `daemons(0)` is no longer required before applying new settings (thanks @eliocamp, #383).
98

109
#### New Features
1110

12-
* New `with_daemons()` and `local_daemons()` helper functions for using a particular compute profile.
11+
* Adds `with_daemons()` and `local_daemons()` helper functions for using a particular compute profile.
1312
This works with daemons that are already set up unlike the existing `with.miraiDaemons()` method, which creates a new scope and tears it down when finished (#360).
14-
* Each mirai now has an attribute `id`, which is a monotonically increasing integer identifier in each session.
13+
* A mirai now has an attribute `id`, which is a monotonically increasing integer identifier unique to each session.
1514

1615
#### Updates
1716

18-
* Improved behaviour when using `serial_config()` custom serialization.
17+
* `miraiError` enhancements:
18+
+ Preserves the original condition classes and may be re-thrown as the original condition (thanks @sebffischer, #400).
19+
+ The print method includes the customary additional line break (thanks @sebffischer, #399).
20+
* Fixes `daemons(n)` failing to launch local daemons if mirai was installed in a custom user library set by an explicit `.libPaths()` call in '.Rprofile' (thanks @erydit and @dpastoor, #390).
21+
* Improved behaviour for `serial_config()` custom serialization.
1922
If the serialization hook function errors or otherwise fails to return a raw vector, this will error out rather than be silently ignored (thanks @dipterix, #378).
23+
* `as.promise()` method for mirai made robust for high-throughput scenarios (#377).
24+
* `mirai_map()` now supports Arrow Tables and Polars DataFrames (#366).
2025
* `require_daemons()` arguments are swapped so that `.compute` comes before `call` for ease of use.
2126
Previous usage will work for the time being, although is deprecated and will be defunct in a future version.
22-
* Fixes `daemons(n)` failing to launch local daemons if mirai was installed in a custom user library set by an explicit `.libPaths()` call in '.Rprofile' (thanks @erydit and @dpastoor, #390).
2327
* Enhancements to `everywhere()`:
2428
+ Consecutive `everywhere()` calls are permissible again when using dispatcher (behaviour update in v2.4.1) (#354).
2529
+ No longer has any effect on the RNG stream when using a reproducible `seed` value at `daemons()` (#356).
26-
* `mirai_map()` now supports Arrow Tables and Polars DataFrames (#366).
30+
* A `mirai()` evaluated on an ephemeral daemon returns invisibly, consistent with other cases (#351).
2731
* `daemon()` gains a `tlscert` argument for custom TLS certificates.
2832
The change in argument name lets this be passed when making a `daemons()` call (#344).
2933
* The `tls` argument at `daemon()`, `launch_local()` and `launch_remote()` is deprecated.
30-
* `as.promise()` method for mirai made robust for high-throughput scenarios (#377).
31-
* `miraiError` enhancements:
32-
+ Preserves the original condition classes and may be re-thrown as the original condition (thanks @sebffischer, #400).
33-
+ The print method includes the customary additional line break (thanks @sebffischer, #399).
34-
* A `mirai()` evaluated on an ephemeral daemon returns invisibly, consistent with other cases (#351).
3534
* Requires nanonext >= [1.6.2.9002].
3635

3736
# mirai 2.4.1

R/daemons.R

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@
7272
#' `tls` is encrypted with a password) For security, should be provided
7373
#' through a function that returns this value, rather than directly.
7474
#'
75-
#' @return Invisibly, the character compute profile created, or else NULL when
76-
#' resetting daemons.
75+
#' @return Invisibly, logical `TRUE` when creating daemons and `FALSE` when
76+
#' resetting.
7777
#'
7878
#' @section Local Daemons:
7979
#'
@@ -249,7 +249,7 @@ daemons <- function(
249249
launch_remote(n = n, remote = remote, .compute = .compute)
250250
on.exit()
251251
}
252-
TRUE
252+
url
253253
}
254254
} else {
255255
signal <- is.null(n)
@@ -258,12 +258,12 @@ daemons <- function(
258258
n <- as.integer(n)
259259

260260
n == 0L && {
261-
is.null(envir) && return(invisible())
261+
is.null(envir) && return(invisible(FALSE))
262262

263263
if (signal) send_signal(envir)
264264
reap(envir[["sock"]])
265265
..[[.compute]] <- NULL -> envir
266-
return(invisible())
266+
return(invisible(FALSE))
267267
}
268268
res <- if (is.null(envir)) {
269269
n > 0L || stop(._[["n_zero"]])
@@ -282,7 +282,7 @@ daemons <- function(
282282
}
283283

284284
is.null(res) && return({
285-
daemons(0, .compute = .compute)
285+
daemons(0L, .compute = .compute)
286286
daemons(
287287
n = n,
288288
url = url,
@@ -297,7 +297,7 @@ daemons <- function(
297297
)
298298
})
299299

300-
invisible(`class<-`(.compute, "miraiDaemons"))
300+
invisible(`class<-`(TRUE, c("miraiDaemons", .compute)))
301301
}
302302

303303
#' @export
@@ -336,7 +336,7 @@ print.miraiDaemons <- function(x, ...) print(unclass(x))
336336
#'
337337
with.miraiDaemons <- function(data, expr, ...) {
338338
prev_profile <- .[["cp"]]
339-
`[[<-`(., "cp", data)
339+
`[[<-`(., "cp", class(data)[2L])
340340
on.exit({
341341
daemons(0L)
342342
`[[<-`(., "cp", prev_profile)
@@ -445,7 +445,6 @@ require_daemons <- function(.compute = NULL, call = environment()) {
445445
daemons_set(.compute = .compute) || .[["require_daemons"]](.compute, call)
446446
}
447447

448-
449448
#' With Daemons
450449
#'
451450
#' Evaluate an expression using a specific compute profile.
@@ -459,19 +458,19 @@ require_daemons <- function(.compute = NULL, call = environment()) {
459458
#' For **local_daemons**: invisible NULL.
460459
#'
461460
#' @examplesIf interactive()
462-
#' d1 <- daemons(1, dispatcher = FALSE, .compute = "cpu")
463-
#' d2 <- daemons(1, dispatcher = FALSE, .compute = "gpu")
461+
#' daemons(1, dispatcher = FALSE, .compute = "cpu")
462+
#' daemons(1, dispatcher = FALSE, .compute = "gpu")
464463
#'
465-
#' with_daemons(d1, {
464+
#' with_daemons("cpu", {
466465
#' s1 <- status()
467466
#' m1 <- mirai(Sys.getpid())
468467
#' })
469468
#'
470-
#' with_daemons(d2, {
469+
#' with_daemons("gpu", {
471470
#' s2 <- status()
472471
#' m2 <- mirai(Sys.getpid())
473472
#' m3 <- mirai(Sys.getpid(), .compute = "cpu")
474-
#' local_daemons(d1)
473+
#' local_daemons("cpu")
475474
#' m4 <- mirai(Sys.getpid())
476475
#' })
477476
#'

dev/vignettes/_mirai.Rmd

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -600,23 +600,23 @@ Similarly, functions such as `status()`, `launch_local()` or `launch_remote()` s
600600

601601
#### `with_daemons()` and `local_daemons()`
602602

603-
`daemons()` returns (invisibly) the compute profile of daemons created as a character string. Supplying this to `with_daemons()` or `local_daemons()` automatically sets the default compute profile of all package functions within the relevant scope.
603+
Supplying a character compute profile to `with_daemons()` or `local_daemons()` automatically sets the default compute profile for all package functions within the relevant scope.
604604

605605
```{r}
606606
#| label: withdaemons
607-
d1 <- daemons(1, .compute = "cpu")
608-
d2 <- daemons(1, .compute = "gpu")
607+
daemons(1, .compute = "cpu")
608+
daemons(1, .compute = "gpu")
609609
610-
with_daemons(d1, {
610+
with_daemons("cpu", {
611611
s1 <- status()
612612
m1 <- mirai(Sys.getpid())
613613
})
614614
615-
with_daemons(d2, {
615+
with_daemons("gpu", {
616616
s2 <- status()
617617
m2 <- mirai(Sys.getpid())
618618
m3 <- mirai(Sys.getpid(), .compute = "cpu")
619-
local_daemons(d1)
619+
local_daemons("cpu")
620620
m4 <- mirai(Sys.getpid())
621621
})
622622
@@ -635,7 +635,7 @@ with_daemons("gpu", daemons(0))
635635

636636
#### With Method
637637

638-
`daemons()` also has a `with()` method, which evaluates an expression with daemons created for the duration of the expression and automatically torn down upon completion. All package functions within the `with()` scope default to using the compute profile of the daemons created.
638+
`daemons()` also has a `with()` method, which evaluates an expression with daemons created for the duration of the expression and automatically reset upon completion. All package functions within the `with()` scope default to using the compute profile of the daemons created.
639639

640640
It was originally designed for running a Shiny app with the desired number of daemons, as in the example below:
641641

@@ -651,4 +651,4 @@ with(daemons(4, .compute = "shiny"), shiny::runApp(app))
651651
Wrapping a call to `shiny::shinyApp()` would not work as `runApp()` is implicitly called when the app is printed, however printing occurs only after `with()` has returned, hence the app would run outside of the scope of the `with()` statement.
652652

653653
In the case of a Shiny app, all mirai calls will be executed before the app returns as the app itself is blocking.
654-
In the case of other expressions, be sure to call or collect the values of all mirai within the expression to ensure that they all complete before the daemons are torn down.
654+
In the case of other expressions, be sure to call or collect the values of all mirai within the expression to ensure that they all complete before the daemons are reset.

man/daemons.Rd

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

man/with_daemons.Rd

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

0 commit comments

Comments
 (0)