Skip to content

Instantly share code, notes, and snippets.

@DavisVaughan
Created May 7, 2020 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavisVaughan/8c312948b2c5943662312017ab54357e to your computer and use it in GitHub Desktop.
Save DavisVaughan/8c312948b2c5943662312017ab54357e to your computer and use it in GitHub Desktop.
fn <- function() {
FUN <- function(x) {
data.table::getDTthreads()
}
parallel::mclapply(1:2, FUN, mc.cores = 2)
}
# Call `getDTthreads()` from the workers
# Should return 1 because we forked
fn()
# Now start it up in a separate process,
# then do the mclapply call which creates
# two workers to run getDTthreads() in.
# The two workers should be forks of the process, so
# why is this 6?
callr::r(fn)
@DavisVaughan
Copy link
Author

DavisVaughan commented May 7, 2020

Result when run in rstudio, locally, one line at a time. Not using reprex here because it runs everything in a separate R session

> fn <- function() {
+   FUN <- function(x) {
+     data.table::getDTthreads()
+   }
+   parallel::mclapply(1:2, FUN, mc.cores = 2)
+ }
> 
> # Call `getDTthreads()` from the workers
> # Should return 1 because we forked
> fn()
[[1]]
[1] 1

[[2]]
[1] 1

> 
> # Now start it up in a separate process,
> # then do the mclapply call which creates
> # two workers to run getDTthreads() in.
> # The two workers should be forks of the process, so
> # why is this 6?
> callr::r(fn)
[[1]]
[1] 6

[[2]]
[1] 6

@DavisVaughan
Copy link
Author

See ?getDTthreads for documentation about data.table switching to use 1 thread in forked workers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment