user1605665
user1605665

Reputation: 4151

Make asynchronous function calls on in R

I have a function that that takes a while to run and it needs to be run multiple times but they can be run asynchronously. In the below example I have three input values to the function and at the moment they run in sequence. How could they be run asynchronously?

vector <- c("1", "2", "3")
longRunningFunction <- function(x) {
  ## Do a bunch of things that takes some time
}
sapply(vector, longRunningFunction)

Upvotes: 2

Views: 440

Answers (1)

jimmyb
jimmyb

Reputation: 4387

As @jenesaisquoi points out, it's pretty much as easy as:

require(parallel)
longRunningFunction <- function(x) {
  ## Do a bunch of things that takes some time
}
mclapply(vector, longRunningFunction, mc.cores = detectCores())

Upvotes: 3

Related Questions