@@ -427,43 +427,57 @@ run.Future <- function(future, ...) {
427427 makeFuture <- plan(" next" )
428428 if (debug ) mdebug(" - Future backend: " , commaq(class(makeFuture )))
429429
430- # # Use new FutureBackend approach?
431- if (getOption(" future.backend.version" , 2L ) == 2L ) {
432- # # Implements a FutureBackend?
433- backend <- attr(makeFuture , " backend" )
434- if (is.function(backend )) {
435- if (debug ) mdebug(" Using FutureBackend ..." )
436- mdebug(" - state: " , sQuote(future [[" state" ]]))
437- on.exit(mdebug(" run() for " , sQuote(class(future )[1 ]), " ... done" ), add = TRUE )
438-
439- if (debug ) mprint(backend )
440-
441- # # Apply future plan tweaks
442- args <- attr(makeFuture , " tweaks" )
443- if (is.null(args )) args <- list ()
444-
445- args2 <- formals(makeFuture )
446- args2 $ `...` <- NULL
447- args2 $ envir <- NULL
448- args2 $ lazy <- NULL # # bc multisession; should be removed
449-
450- for (name in names(args2 )) {
451- args [[name ]] <- args2 [[name ]]
452- }
453- backend <- do.call(backend , args = args )
454- if (debug ) mdebug(" - FutureBackend: " , commaq(class(backend )))
455- stop_if_not(inherits(backend , " FutureBackend" ))
430+ # # Implements a FutureBackend?
431+ backend <- attr(makeFuture , " backend" )
432+ if (is.function(backend )) {
433+ if (debug ) mdebug(" Using FutureBackend ..." )
434+ mdebug(" - state: " , sQuote(future [[" state" ]]))
435+ on.exit(mdebug(" run() for " , sQuote(class(future )[1 ]), " ... done" ), add = TRUE )
456436
437+ if (debug ) mprint(backend )
457438
458- if (debug ) mdebug(" - Launching futures ..." )
459- future2 <- launchFuture(backend , future = future )
460- if (debug ) mdebug(" - Launching futures ... done" )
461- if (debug ) mdebug(" - Future launched: " , commaq(class(future2 )))
462- stop_if_not(inherits(future2 , " Future" ))
463- if (debug ) mdebug(" Using FutureBackend ... DONE" )
464-
465- return (future2 )
439+ # # Apply future plan tweaks
440+ args <- attr(makeFuture , " tweaks" )
441+ if (is.null(args )) args <- list ()
442+
443+ args2 <- formals(makeFuture )
444+ args2 $ `...` <- NULL
445+ args2 $ envir <- NULL
446+ args2 $ lazy <- NULL # # bc multisession; should be removed
447+
448+ for (name in names(args2 )) {
449+ args [[name ]] <- args2 [[name ]]
466450 }
451+ backend <- do.call(backend , args = args )
452+ if (debug ) mdebug(" - FutureBackend: " , commaq(class(backend )))
453+ stop_if_not(inherits(backend , " FutureBackend" ))
454+ } else {
455+ backend <- NULL
456+ }
457+
458+ # # Use new FutureBackend approach?
459+ if (inherits(backend , " FutureBackend" ) && getOption(" future.backend.version" , 2L ) == 2L ) {
460+ if (future [[" state" ]] != " created" ) {
461+ label <- future [[" label" ]]
462+ if (is.null(label )) label <- " <none>"
463+ stop(FutureError(sprintf(" A future ('%s') can only be launched once" , label ), future = future ))
464+ }
465+
466+ # # Assert that the process that created the future is
467+ # # also the one that evaluates/resolves/queries it.
468+ assertOwner(future )
469+
470+ # # Coerce to target Future class
471+ class(future ) <- backend [[" futureClasses" ]]
472+
473+ if (debug ) mdebug(" - Launching futures ..." )
474+ future2 <- launchFuture(backend , future = future )
475+ if (debug ) mdebug(" - Launching futures ... done" )
476+ if (debug ) mdebug(" - Future launched: " , commaq(class(future2 )))
477+ stop_if_not(inherits(future2 , " Future" ))
478+ if (debug ) mdebug(" Using FutureBackend ... DONE" )
479+
480+ return (future2 )
467481 }
468482
469483
@@ -533,6 +547,9 @@ run.Future <- function(future, ...) {
533547 future <- run(future )
534548 if (debug ) mdebug(" - Launch lazy future ... done" )
535549 }
550+
551+ # # Set FutureBackend, if it exists
552+ future [[" backend" ]] <- backend
536553
537554 # # Sanity check: This method was only called for lazy futures
538555 stop_if_not(future [[" state" ]] != " created" , future [[" lazy" ]])
0 commit comments