Skip to content

Commit 5ff3da1

Browse files
nbrOfWorkers() could still return a value with class attribute set to 'integer', e.g. plan(multicore)
1 parent 3fa36b7 commit 5ff3da1

5 files changed

Lines changed: 22 additions & 5 deletions

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: future
2-
Version: 1.70.0-9010
2+
Version: 1.70.0-9011
33
Title: Unified Parallel and Distributed Processing in R for Everyone
44
Depends:
55
R (>= 3.2.0)

R/backend_api-11.ClusterFutureBackend-class.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ ClusterFutureBackend <- local({
3838
if (is.numeric(workers)) {
3939
if (debug) mdebugf("workers: %g", workers)
4040
## Preserve class attributes, especially "AsIs"
41-
clazz <- class(workers)
41+
clazz <- setdiff(class(workers), c("integer", "numeric"))
4242
workers <- as.integer(workers)
43-
class(workers) <- clazz
43+
if (length(clazz) > 0) class(workers) <- clazz
4444
stop_if_not(length(workers) == 1, is.finite(workers))
4545
} else {
4646
stop_if_not(length(workers) >= 1, !anyNA(workers))

R/backend_api-11.MulticoreFutureBackend-class.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ MulticoreFutureBackend <- function(workers = availableCores(constraints = "multi
142142
default_workers <- missing(workers)
143143
if (is.function(workers)) workers <- workers()
144144
stop_if_not(is.numeric(workers))
145-
workers <- structure(as.integer(workers), class = setdiff(class(workers), mode(workers)))
145+
workers <- structure(as.integer(workers), class = setdiff(class(workers), c("integer", "numeric")))
146146
stop_if_not(length(workers) == 1, is.finite(workers), workers >= 1)
147147

148148
## Fall back to sequential futures if only a single additional R process

R/backend_api-13.MultisessionFutureBackend-class.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ MultisessionFutureBackend <- function(workers = availableCores(constraints = "co
2626
default_workers <- missing(workers)
2727
if (is.function(workers)) workers <- workers()
2828
stop_if_not(is.numeric(workers))
29-
workers <- structure(as.integer(workers), class = class(workers))
29+
workers <- structure(as.integer(workers), class = setdiff(class(workers), c("integer", "numeric")))
3030
stop_if_not(length(workers) == 1, is.finite(workers), workers >= 1)
3131

3232
## Fall back to sequential futures if only a single additional R process

inst/testme/test-nbrOfWorkers.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ for (strategy in strategies) {
1818
n <- nbrOfWorkers()
1919
message(sprintf("nbrOfWorkers: %d", n))
2020
stopifnot(n == 1L)
21+
stopifnot(is.null(attr(n, "class")))
2122

2223
n <- nbrOfFreeWorkers()
2324
message(sprintf("nbrOfFreeWorkers: %d", n))
@@ -55,6 +56,7 @@ for (strategy in strategies) {
5556
n <- nbrOfWorkers()
5657
message(sprintf("nbrOfWorkers: %d", n))
5758
stopifnot(n == nworkers)
59+
stopifnot(is.null(attr(n, "class")))
5860

5961
n <- nbrOfFreeWorkers()
6062
message(sprintf("nbrOfFreeWorkers: %d", n))
@@ -120,4 +122,19 @@ message(sprintf("nbrOfWorkers: %g", n))
120122
stopifnot(n == length(workers))
121123
parallel::stopCluster(workers)
122124

125+
message("Type of future: multicore/multisession with workers = I(1L)")
126+
for (strategy in c("multicore", "multisession")) {
127+
if (strategy %in% supportedStrategies()) {
128+
plan(strategy, workers = I(1L))
129+
n <- nbrOfWorkers()
130+
message(sprintf("nbrOfWorkers (%s): %g", strategy, n))
131+
stopifnot(n == 1L)
132+
if (strategy == "multicore") {
133+
stopifnot(inherits(n, "AsIs"))
134+
} else {
135+
stopifnot(is.null(attr(n, "class")))
136+
}
137+
}
138+
}
139+
123140
message("*** nbrOfWorkers() ... DONE")

0 commit comments

Comments
 (0)