Skip to content

Commit eb10ffc

Browse files
Re-add support for plan(..., gc = TRUE) for cluster futures
1 parent 3495f5b commit eb10ffc

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

R/backend_api-ClusterFutureBackend-class.R

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#' @keywords internal
1414
#' @rdname FutureBackend
1515
#' @export
16-
ClusterFutureBackend <- function(workers = availableWorkers(), persistent = FALSE, earlySignal = TRUE, ...) {
16+
ClusterFutureBackend <- function(workers = availableWorkers(), persistent = FALSE, earlySignal = TRUE, gc = FALSE, ...) {
1717
if (is.function(workers)) workers <- workers()
1818
if (is.null(workers)) {
1919
getDefaultCluster <- importParallel("getDefaultCluster")
@@ -50,7 +50,7 @@ ClusterFutureBackend <- function(workers = availableWorkers(), persistent = FALS
5050
## Name of the FutureRegistry
5151
reg <- sprintf("workers-%s", name)
5252

53-
core <- FutureBackend(workers = workers, persistent = persistent, reg = reg, ...)
53+
core <- FutureBackend(workers = workers, persistent = persistent, reg = reg, earlySignal = earlySignal, gc = gc, ...)
5454
core[["futureClasses"]] <- c("ClusterFuture", core[["futureClasses"]])
5555
core <- structure(core, class = c("ClusterFutureBackend", "FutureBackend", class(core)))
5656
core
@@ -175,7 +175,14 @@ launchFuture.ClusterFutureBackend <- function(backend, future, ...) {
175175
if (debug) mdebug("eraseGlobalEnvironment() ... done")
176176
}
177177

178-
## (3) Launch future
178+
179+
## (3) Garbage collection
180+
if (isTRUE(backend[["gc"]])) {
181+
cluster_call_blocking(cl, fun = gc, future = future, when = "call gc() on")
182+
}
183+
184+
185+
## (4) Launch future
179186
if (debug) mdebug("launchFuture() ...")
180187
worker <- future[["node"]]
181188
stop_if_not(

tests/plan.R

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,56 @@ stopifnot(identical(v, 42L))
115115
plan(sequential)
116116

117117

118+
message("*** plan(sequential, gc = TRUE)")
119+
oplan <- plan(cluster, gc = TRUE)
120+
f <- future(42L)
121+
v <- value(f)
122+
print(v)
123+
stopifnot(identical(v, 42L))
124+
plan(sequential)
125+
126+
message("*** plan(cluster, gc = TRUE)")
127+
oplan <- plan(cluster, gc = TRUE)
128+
f <- future(42L)
129+
v <- value(f)
130+
print(v)
131+
stopifnot(identical(v, 42L))
132+
plan(sequential)
133+
134+
message("*** plan(multisession, gc = TRUE)")
135+
oplan <- plan(cluster, gc = TRUE)
136+
f <- future(42L)
137+
v <- value(f)
138+
print(v)
139+
stopifnot(identical(v, 42L))
140+
plan(sequential)
141+
142+
143+
message("*** plan(sequential, earlySignal = TRUE)")
144+
oplan <- plan(cluster, earlySignal = TRUE)
145+
f <- future(42L)
146+
v <- value(f)
147+
print(v)
148+
stopifnot(identical(v, 42L))
149+
plan(sequential)
150+
151+
message("*** plan(cluster, earlySignal = TRUE)")
152+
oplan <- plan(cluster, earlySignal = TRUE)
153+
f <- future(42L)
154+
v <- value(f)
155+
print(v)
156+
stopifnot(identical(v, 42L))
157+
plan(sequential)
158+
159+
message("*** plan(multisession, earlySignal = TRUE)")
160+
oplan <- plan(cluster, earlySignal = TRUE)
161+
f <- future(42L)
162+
v <- value(f)
163+
print(v)
164+
stopifnot(identical(v, 42L))
165+
plan(sequential)
166+
167+
118168

119169
message("*** old <- plan(new)")
120170
truth <- plan("list")

0 commit comments

Comments
 (0)