DavisVaughan / furrr

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

How to locate errors and debug when using furrr? #233

Closed melonki closed 2 years ago

melonki commented 2 years ago

Dear Contributors and Members,

Thank you for your great efforts in developing furrr! It has accelerated my work a lot. However, I found it's hard to locate errors and debug. Are there any functions working like possibly or safely from purrr? I would appreciate it if you would provide any suggestions!

Best, Melonki

DavisVaughan commented 2 years ago

You can just use safely() to wrap the function that you'd send to the worker, like how you'd use it with map(). i.e.

library(furrr)
#> Loading required package: future
library(purrr)

safe_log <- safely(log)
safe_log(10)
#> $result
#> [1] 2.302585
#> 
#> $error
#> NULL
safe_log("a")
#> $result
#> NULL
#> 
#> $error
#> <simpleError in .Primitive("log")(x, base): non-numeric argument to mathematical function>

list("a", 10, 100) %>%
  map(safe_log)
#> [[1]]
#> [[1]]$result
#> NULL
#> 
#> [[1]]$error
#> <simpleError in .Primitive("log")(x, base): non-numeric argument to mathematical function>
#> 
#> 
#> [[2]]
#> [[2]]$result
#> [1] 2.302585
#> 
#> [[2]]$error
#> NULL
#> 
#> 
#> [[3]]
#> [[3]]$result
#> [1] 4.60517
#> 
#> [[3]]$error
#> NULL

plan(multisession, workers = 2)

list("a", 10, 100) %>%
  future_map(safe_log)
#> [[1]]
#> [[1]]$result
#> NULL
#> 
#> [[1]]$error
#> <simpleError in .Primitive("log")(x, base): non-numeric argument to mathematical function>
#> 
#> 
#> [[2]]
#> [[2]]$result
#> [1] 2.302585
#> 
#> [[2]]$error
#> NULL
#> 
#> 
#> [[3]]
#> [[3]]$result
#> [1] 4.60517
#> 
#> [[3]]$error
#> NULL

Created on 2022-05-06 by the reprex package (v2.0.1)

melonki commented 2 years ago

You can just use safely() to wrap the function that you'd send to the worker, like how you'd use it with map(). i.e.

library(furrr)
#> Loading required package: future
library(purrr)

safe_log <- safely(log)
safe_log(10)
#> $result
#> [1] 2.302585
#> 
#> $error
#> NULL
safe_log("a")
#> $result
#> NULL
#> 
#> $error
#> <simpleError in .Primitive("log")(x, base): non-numeric argument to mathematical function>

list("a", 10, 100) %>%
  map(safe_log)
#> [[1]]
#> [[1]]$result
#> NULL
#> 
#> [[1]]$error
#> <simpleError in .Primitive("log")(x, base): non-numeric argument to mathematical function>
#> 
#> 
#> [[2]]
#> [[2]]$result
#> [1] 2.302585
#> 
#> [[2]]$error
#> NULL
#> 
#> 
#> [[3]]
#> [[3]]$result
#> [1] 4.60517
#> 
#> [[3]]$error
#> NULL

plan(multisession, workers = 2)

list("a", 10, 100) %>%
  future_map(safe_log)
#> [[1]]
#> [[1]]$result
#> NULL
#> 
#> [[1]]$error
#> <simpleError in .Primitive("log")(x, base): non-numeric argument to mathematical function>
#> 
#> 
#> [[2]]
#> [[2]]$result
#> [1] 2.302585
#> 
#> [[2]]$error
#> NULL
#> 
#> 
#> [[3]]
#> [[3]]$result
#> [1] 4.60517
#> 
#> [[3]]$error
#> NULL

Created on 2022-05-06 by the reprex package (v2.0.1)

Thank you so much for your quick response!