Skip to content

Commit 6b26d51

Browse files
Make sure nbrOfWorkers() still works for third-party backends; it'll only produce an error if 'free=TRUE' is asked [#264]
1 parent a450407 commit 6b26d51

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

R/nbrOfWorkers.R

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ nbrOfWorkers <- function(evaluator = NULL, free = FALSE) {
2727

2828
#' @export
2929
nbrOfWorkers.cluster <- function(evaluator, free = FALSE) {
30+
assert_no_positional_args_but_first()
31+
3032
expr <- formals(evaluator)$workers
3133
workers <- eval(expr, enclos = baseenv())
3234
if (is.function(workers)) workers <- workers()
@@ -58,11 +60,15 @@ nbrOfWorkers.cluster <- function(evaluator, free = FALSE) {
5860

5961
#' @export
6062
nbrOfWorkers.uniprocess <- function(evaluator, free = FALSE) {
63+
assert_no_positional_args_but_first()
64+
6165
1L
6266
}
6367

6468
#' @export
6569
nbrOfWorkers.multicore <- function(evaluator, free = FALSE) {
70+
assert_no_positional_args_but_first()
71+
6672
workers <- NextMethod(free = FALSE)
6773
if (isTRUE(free)) {
6874
workers <- workers - usedCores()
@@ -73,6 +79,8 @@ nbrOfWorkers.multicore <- function(evaluator, free = FALSE) {
7379

7480
#' @export
7581
nbrOfWorkers.multiprocess <- function(evaluator, free = FALSE) {
82+
assert_no_positional_args_but_first()
83+
7684
if (isTRUE(free)) {
7785
stop("nbrOfWorkers(free = TRUE) is not implemented for this type of future backend (please contacts the maintainer of that backend): ", paste(sQuote(class(evaluator)), collapse = ", "))
7886
}
@@ -91,6 +99,8 @@ nbrOfWorkers.multiprocess <- function(evaluator, free = FALSE) {
9199

92100
#' @export
93101
nbrOfWorkers.future <- function(evaluator, free = FALSE) {
102+
assert_no_positional_args_but_first()
103+
94104
if (isTRUE(free)) {
95105
stop("nbrOfWorkers(free = TRUE) is not implemented for this type of future backend (please contacts the maintainer of that backend): ", paste(sQuote(class(evaluator)), collapse = ", "))
96106
}
@@ -105,17 +115,34 @@ nbrOfWorkers.future <- function(evaluator, free = FALSE) {
105115
stop(sprintf("Unsupported type of 'workers' for evaluator of class %s: %s", paste(sQuote(class(evaluator)), collapse = ", "), class(workers)[1]))
106116
}
107117
stop_if_not(length(workers) == 1L, !is.na(workers), workers >= 1L)
118+
119+
if (isTRUE(free)) {
120+
## If we reach this point, we have failed to infer how many free workers
121+
## there are. Unless there is an infinite number of workers available,
122+
## we have no option but producing an error here.
123+
if (is.finite(workers)) {
124+
stop("nbrOfWorkers(free = TRUE) is not implemented for this type of future backend (please contacts the maintainer of that backend): ", paste(sQuote(class(evaluator)), collapse = ", "))
125+
}
126+
}
127+
108128
workers
109129
}
110130

111131
#' @export
112132
nbrOfWorkers.NULL <- function(evaluator, free = FALSE) {
113-
nbrOfWorkers(plan("next"), free = free)
133+
assert_no_positional_args_but_first()
134+
135+
free <- force(free)
136+
if (!isTRUE(free)) return(nbrOfWorkers(plan("next")))
137+
138+
## This will produce an error on "unused argument (free = TRUE)"
139+
## for backends that does not yet support a 'free' argument
140+
nbrOfWorkers(plan("next"), free = TRUE)
114141
}
115142

116143

117144
#' @export
118145
nbrOfWorkers.logical <- function(evaluator, free = FALSE) {
119-
if (missing(free) && isTRUE(evaluator)) free <- TRUE
120-
nbrOfWorkers(plan("next"), free = free)
146+
assert_no_positional_args_but_first()
147+
nbrOfWorkers(NULL, free = force(free))
121148
}

0 commit comments

Comments
 (0)