Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revisit .env_globals argument #153

Closed
DavisVaughan opened this issue Aug 10, 2020 · 1 comment · Fixed by #155
Closed

Revisit .env_globals argument #153

DavisVaughan opened this issue Aug 10, 2020 · 1 comment · Fixed by #155

Comments

@DavisVaughan
Copy link
Owner

Somewhat related to HenrikBengtsson/future.apply#62, since that also uses this env.

y actually shouldn't be found here.

What happens is that the function env of fn() is the global env. When future_map() searches for globals, it searches in the caller env, which contains y <- 1. So y is identified as a global and is exported to the worker and assigned into the global env on the worker, so it is found in the multisession plan

library(purrr)
library(furrr)

fn <- function(x) {
  y
}

wrapper <- function() {
  y <- 1
  fn(1)
}

wrapper2 <- function() {
  y <- 1
  map(1:5, fn)
}

wrapper3 <- function() {
  y <- 1
  future_map(1:5, fn)
}

wrapper()
#> Error in fn(1): object 'y' not found
wrapper2()
#> Error in .f(.x[[i]], ...): object 'y' not found

plan(sequential)
wrapper3()
#> Error in ...furrr_fn(...): object 'y' not found

plan(multisession, workers = 2)
wrapper3()
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 1
#> 
#> [[3]]
#> [1] 1
#> 
#> [[4]]
#> [1] 1
#> 
#> [[5]]
#> [1] 1

Created on 2020-08-10 by the reprex package (v0.3.0)

@DavisVaughan
Copy link
Owner Author

The more correct solution might be to look up .f globals in the fn_env(.f) and to lookup .x and ... globals in the caller env

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant