-
Notifications
You must be signed in to change notification settings - Fork 18
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
Can I use the {parallel} package with Shiny and {plumber}? #81
Comments
I'll address the {parallel} package, as {parallely} produces better clusters that use the {parallel} package for computations. The {parallel} package blocks the main R session to compute information with most of its functions (Ex: {future} computations return objects that can be automatically upgraded to a I think we can upgrade {parallel} use of Thought process: library(promises)
parallel_promise <- function(expr, ...) {
# Submit job
job <- parallel::mcparallel(expr, ...)
# Make promise to collect result
p <- promises::promise(function(resolve, reject) {
try_result <- function() {
# Try to collect the result
res <- parallel::mccollect(job, wait = FALSE, timeout = 0)
if (is.null(res)) {
# Try again a little later
later::later(try_result, 0.1) # (Should expo backoff)
} else {
# Return result
resolve(res)
}
}
try_result()
})
# Return promise
p
} Example: Show it is possibleSmall demo wrapped in {
message("Local PID: ", Sys.getpid())
parallel_promise({Sys.getpid()}) %...>% {
message("Parallel PID: ", .)
}
message("Post computation: ", 1 + 1) # Here to prove that promises are working
}
#> Local PID: 11941
#> Post computation: 2
#> Parallel PID: 24335 Example: Perform 10 jobs in parallel
Take home messageAs long as To drive the point home again, the
There are too many backends to support. Rather than picking a "top 5", only {future} is supported due to its explicit submission/collection functions and its ability to generically interface with many cluster setups. |
Correct, the closest we have to > library(parallel)
> f <- mcparallel({ print(1:3); warning("boom"); log("a") })
[1] 1 2 3
> v <- mccollect(f)
> v
$`12609`
[1] "Error in log(\"a\") : non-numeric argument to mathematical function\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError in log("a"): non-numeric argument to mathematical function> Note how the output is not captured (and might even be lost depending on environment/GUI), and the warning is lost, so those need to be handled separately. On the upside, > cond <- attr(v[[1]], "condition")
> if (inherits(cond, "error")) stop(cond)
Error in log("a") : non-numeric argument to mathematical function to relay the error. If you want to do something similar with parallel PSOCK clusters, more work is needed, and you'll also have to handle errors internally (e.g. capture it via a These are some of the things that future encapsulates in a standard, unified manner: > library(future)
> f <- future({ print(1:3); log("a") })
> v <- value(f)
[1] 1 2 3
Error in log("a") : non-numeric argument to mathematical function regardless of parallel backend. Hopefully that helps clarify the role of future here. |
https://www.linkedin.com/feed/update/urn:li:ugcPost:6899412968317280256?commentUrn=urn%3Ali%3Acomment%3A%28ugcPost%3A6899412968317280256%2C6899428857653334016%29
The text was updated successfully, but these errors were encountered: