Skip to content

Commit 674a998

Browse files
* print() on a Future outpus a description of the current state
* Using state = "failed" for more backends
1 parent d2f0631 commit 674a998

File tree

7 files changed

+109
-14
lines changed

7 files changed

+109
-14
lines changed

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.68.0-9004
2+
Version: 1.68.0-9005
33
Title: Unified Parallel and Distributed Processing in R for Everyone
44
Depends:
55
R (>= 3.2.0)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
which session (including UUID, hostname, and PID) and when the
1010
condition was created.
1111

12+
* `print()` on a Future outpus a description of the current state.
13+
1214
## Bug Fixes
1315

1416
* `plan(..., interrupts = ...)` would produce a warning on "Detected

R/backend_api-11.MulticoreFutureBackend-class.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ result.MulticoreFuture <- local({
480480
future[["state"]] <- "interrupted"
481481
} else if (inherits(result, "FutureLaunchError")) {
482482
ex <- result
483-
future[["state"]] <- "interrupted"
483+
future[["state"]] <- "failed"
484484
} else if (inherits(result, "FutureError")) {
485485
## FIXME: Add more details
486486
hint <- sprintf("parallel::mccollect() did return a FutureResult but a %s object: %s", sQuote(class(result)[1]), paste(deparse(result), collapse = "; "))

R/backend_api-11.SequentialFutureBackend-class.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,13 @@ launchFuture.SequentialFutureBackend <- function(backend, future, ...) {
5050

5151
## Launch future
5252
future[["state"]] <- "running"
53-
future[["result"]] <- evalFuture(data)
54-
future[["state"]] <- "finished"
53+
result <- evalFuture(data)
54+
if (inherits(result, "FutureLaunchError")) {
55+
future[["state"]] <- "failed"
56+
} else {
57+
future[["state"]] <- "finished"
58+
}
59+
future[["result"]] <- result
5560

5661
## Register run (used to collect statistics)
5762
reg <- backend[["reg"]]

R/backend_api-Future-class.R

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,55 @@ print.Future <- function(x, ...) {
337337
cat(sprintf("Early signaling: %s\n", isTRUE(future[["earlySignal"]])))
338338
cat(sprintf("Environment: %s\n", envname(future[["envir"]])))
339339

340+
result <- future[["result"]]
340341
state <- future[["state"]]
341-
cat(sprintf("State: %s\n", commaq(state)))
342+
description <- switch(state,
343+
created = {
344+
"Future was created, but is yet to be submitted"
345+
},
346+
submitted = {
347+
"Future has been submitted, but it is not known if it has been started"
348+
},
349+
running = {
350+
"Future is being evaluated"
351+
},
352+
finished = {
353+
stop_if_not(inherits(result, "FutureResult"))
354+
conditions <- result[["conditions"]]
355+
n <- length(conditions)
356+
if (n == 0L) {
357+
cond <- NULL
358+
} else {
359+
condition <- conditions[[n]]
360+
cond <- condition[["condition"]]
361+
}
362+
if (inherits(cond, "error")) {
363+
if (inherits(cond, "FutureInterruptError")) {
364+
sprintf("Future was interrupted during evaluation, resulting in a %s", sQuote(class(cond)[1]))
365+
} else if (inherits(cond, "FutureError")) {
366+
sprintf("Future was resolved, but produced a %s", sQuote(class(cond)[1]))
367+
} else {
368+
"Future was resolved, but produced a run-time error"
369+
}
370+
} else if (inherits(cond, "interrupt")) {
371+
"Future was interrupted during evaluation"
372+
} else {
373+
"Future was resolved successfully"
374+
}
375+
},
376+
interrupted = {
377+
"Future was interrupted during evaluation"
378+
},
379+
canceled = {
380+
"Future was canceled"
381+
},
382+
failed = {
383+
"Future failed due to an unexpected, internal error during launch, evaluation, or while returning the results"
384+
},
385+
NA_character_
386+
)
387+
cat(sprintf("State: %s (\"%s\")\n", commaq(state), description))
342388

343-
result <- future[["result"]]
344389
hasResult <- inherits(result, "FutureResult")
345390
## BACKWARD COMPATIBILITY
346391
hasResult <- hasResult || exists("value", envir = future, inherits = FALSE)
@@ -477,16 +522,18 @@ run.Future <- function(future, ...) {
477522
if (debug) mdebug_push("Launching futures ...")
478523
future[["backend"]] <- backend
479524
future[["start"]] <- proc.time()[[3]]
480-
future2 <- tryCatch(
525+
future2 <- tryCatch({
481526
launchFuture(backend, future = future)
482-
, FutureError = function(ex) {
527+
}, FutureError = function(ex) {
528+
future[["state"]] <- "failed"
483529
## Known error caught by the future backend
484530
stop(ex)
485531
}, error = function(ex) {
486532
## Unexpected error
487533
msg <- conditionMessage(ex)
488534
label <- sQuoteLabel(future)
489535
msg <- sprintf("Caught an unexpected error of class %s when trying to launch future (%s) on backend of class %s. The reason was: %s", class(ex)[1], label, class(backend)[1], msg)
536+
future[["state"]] <- "failed"
490537
stop(FutureLaunchError(msg, future = future))
491538
})
492539
if (debug) mdebug_pop()
@@ -861,6 +908,48 @@ getExpression.Future <- local({
861908
}) ## getExpression()
862909

863910

911+
# Future states:
912+
#
913+
# 1. `created`:
914+
# The future has been created, but not yet been submitted.
915+
# Examples:
916+
# A future created with `lazy = TRUE` is in this state.
917+
#
918+
# 2. `submitted`:
919+
# The future has been submitted for evaluation.
920+
# There is no evidence yet that the future is being evaluated.
921+
# Examples:
922+
# A future submitted to a queue may be in this state.
923+
#
924+
# 3. `running`:
925+
# The future is currently being evaluated.
926+
# There is no evidence yet that the evaluation of the future has finished.
927+
# Examples:
928+
# Future being processed by a worker is typically in this state.
929+
#
930+
# 4. `finished`:
931+
# The evaluation of the future has completed.
932+
# Examples:
933+
# A future submitted to a queue may be in this state.
934+
#
935+
# 5. `failed`:
936+
# The evaluation of the future has terminated, but failed before being
937+
# completed.
938+
# Examples:
939+
# A future that failed to launch.
940+
# An internal error occured while evaluating the future.
941+
# An internal error occured after finishing future evaluation, but
942+
# before return the results.
943+
#
944+
# 6. `interrupted`:
945+
# The evaluation of the future has terminated, but was interrupted before
946+
# being completed.
947+
# Examples:
948+
#
949+
# 7. `canceled`:
950+
# The evaluation of the future has terminated, but was canceled.
951+
# Examples:
952+
#
864953
#' @export
865954
`$<-.Future` <- function(x, name, value) {
866955
if (name == "state") {

man/resolved.Rd

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

man/result.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)