DavisVaughan / furrr

Apply Mapping Functions in Parallel using Futures
https://furrr.futureverse.org/
Other
695 stars 39 forks source link

Operator '<<-' inside a function invoked by future_map #239

Closed abdellah19jan closed 2 years ago

abdellah19jan commented 2 years ago

Hello,

I'm trying to increment a counter inside a function which is invoked by future_map, it does work with map but not with future_map, see code bellow.

library(tidyverse)
library(furrr)

fn <- function(x) {
  gv <<- gv + 1
  2 * x
}

gv <- 0
df <- tibble(x = 0:9) %>% mutate(y = map_dbl(x, fn))
gv
#> [1] 10

gv <- 0
df <- tibble(x = 0:9) %>% mutate(y = future_map_dbl(x, fn))
gv
#> [1] 0

I do believe that is related to globals, but I don't see how to fix it.

Thanks in advance for your help!

DavisVaughan commented 2 years ago

You can't do something like this with furrr or future in general. future_map_dbl() might be running in a completely different R process than the one where gv originally exists, so there is generally no way for it to update it because it won't have access to the original value. The globals package will probably send a copy of gv over to the worker process, and <<- will be updating that copy at each iteration, but the original version of gv will be left untouched.