futureverse / future.apply

:rocket: R package: future.apply - Apply Function to Elements in Parallel using Futures
https://future.apply.futureverse.org
211 stars 16 forks source link

How to allow printing to the console inside a future? #38

Closed xiaodaigh closed 5 years ago

xiaodaigh commented 5 years ago
library(future)
library(future.apply)

plan(transaprent)

future.apply::future_lapply(1:10, function(x) {
   x  = 1
   browser()
})

when browser is activated, I type x into the console, but it doesn't show up. Is there a way to let me inspect content whilst inside a future_lapply call?

HenrikBengtsson commented 5 years ago

The underlying reason is that browser() outputs to the standard output (stdout)(*) and futures capture stdout (to be relayed when their values are collected) by default. You can tell the future framework to leave the stdout "as is" by specifying future.stdout = NA, e.g.

> library(future.apply)
> y <- future_lapply(1:2, function(x) { browser() }, future.stdout = NA)
Called from: ...future.FUN(...future.X_jj, ...)
Browse[1]> ls()
[1] "x"
Browse[1]> c
Called from: ...future.FUN(...future.X_jj, ...)
Browse[1]> c
> 

(*) By my book, ideally browser() would output to standard error (stderr), which is not captured. Second best would be if one could control this via an argument, e.g. browser(output = "stderr").

HenrikBengtsson commented 5 years ago

For the record, I've added this to the Wishlist-for-R.

I'll assume this resolved the problem for you, so I'm closing. If not, please reopen.

HenrikBengtsson commented 1 year ago

Just a follow-up here: One can use plan(sequential, split = TRUE); it's more convenient, and it avoids having to specify future.stdout = NA. For example,

> library(future.apply)
> plan(sequential, split = TRUE)
> y <- future_lapply(1:2, function(x) { browser() })
Called from: ...future.FUN(...future.X_jj, ...)
Browse[1]> ls()
[1] "x"
Browse[1]> c
Called from: ...future.FUN(...future.X_jj, ...)
Browse[1]> c
Called from: ...future.FUN(...future.X_jj, ...)
[1] "x"
Called from: ...future.FUN(...future.X_jj, ...)
>