Skip to content

Commit 5902136

Browse files
Add support for relaying immediateCondition:s in near real-time via the local file system [#29]
1 parent 19ba09a commit 5902136

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: future.callr
2-
Version: 0.8.2-9206
2+
Version: 0.8.2-9208
33
Depends:
44
R (>= 3.4.0),
55
future (>= 1.40.0)

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method(getFutureBackendConfigs,CallrFutureBackend)
34
S3method(interruptFuture,CallrFutureBackend)
45
S3method(launchFuture,CallrFutureBackend)
56
S3method(nbrOfFreeWorkers,CallrFutureBackend)

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
terminated is now considerred interrupted, which for instance
1010
means that it can be `reset()`.
1111

12+
* Now 'callr' futures can relay `immediateCondition`:s in near
13+
real-time, e.g. `progression` contdions signals by the
14+
**progressr** package.
15+
1216

1317
# Version 0.8.2 [2023-08-08]
1418

R/CallrFutureBackend-class.R

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,34 @@ nbrOfFreeWorkers.CallrFutureBackend <- function(evaluator = NULL, background = F
187187
}
188188

189189

190+
191+
#' @exportS3Method getFutureBackendConfigs CallrFutureBackend
192+
getFutureBackendConfigs.CallrFutureBackend <- local({
193+
immediateConditionsPath <- import_future("immediateConditionsPath")
194+
fileImmediateConditionHandler <- import_future("fileImmediateConditionHandler")
195+
196+
function(future, ..., debug = isTRUE(getOption("future.debug"))) {
197+
conditionClasses <- future[["conditions"]]
198+
if (is.null(conditionClasses)) {
199+
capture <- list()
200+
} else {
201+
path <- immediateConditionsPath(rootPath = tempdir())
202+
capture <- list(
203+
immediateConditionHandlers = list(
204+
immediateCondition = function(cond) {
205+
fileImmediateConditionHandler(cond, path = path)
206+
}
207+
)
208+
)
209+
}
210+
211+
list(
212+
capture = capture
213+
)
214+
}
215+
})
216+
217+
190218
#' Prints a callr future
191219
#'
192220
#' @param x An CallrFuture object
@@ -233,6 +261,15 @@ resolved.CallrFuture <- function(x, .signalEarly = TRUE, ...) {
233261
if (!inherits(process, "r_process")) return(FALSE)
234262
resolved <- !process$is_alive()
235263

264+
## Collect and relay immediateCondition if they exists
265+
conditions <- readImmediateConditions(signal = TRUE)
266+
## Record conditions as signaled
267+
signaled <- c(x[[".signaledConditions"]], conditions)
268+
x[[".signaledConditions"]] <- signaled
269+
270+
## Signal conditions early? (happens only iff requested)
271+
if (.signalEarly) signalEarly(x, ...)
272+
236273
## Signal errors early?
237274
if (.signalEarly && resolved) {
238275
## Trigger a FutureError already here, if exit code != 0
@@ -257,6 +294,12 @@ result.CallrFuture <- function(future, ...) {
257294

258295
result <- await(future, cleanup = FALSE)
259296

297+
## Collect and relay immediateCondition if they exists
298+
conditions <- readImmediateConditions()
299+
## Record conditions as signaled
300+
signaled <- c(future[[".signaledConditions"]], conditions)
301+
future[[".signaledConditions"]] <- signaled
302+
260303
if (!inherits(result, "FutureResult")) {
261304
if (inherits(result, "FutureLaunchError")) {
262305
} else {

R/zzz.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
## To be cached by .onLoad()
22
FutureRegistry <- NULL
33
assertOwner <- NULL
4+
readImmediateConditions <- NULL
5+
signalEarly <- NULL
6+
getFutureBackendConfigs <- NULL
47

58
.onLoad <- function(libname, pkgname) {
69
## Import private functions from 'future'
710
FutureRegistry <<- import_future("FutureRegistry")
811
assertOwner <<- import_future("assertOwner")
12+
readImmediateConditions <<- import_future("readImmediateConditions")
13+
signalEarly <<- import_future("signalEarly")
14+
getFutureBackendConfigs <<- import_future("getFutureBackendConfigs")
915
}
1016

0 commit comments

Comments
 (0)