Skip to content
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

Enable optional parallel site building #383

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
#' option \code{getOption('blogdown.method')} when it is set.
#' @param run_hugo Whether to run \code{hugo_build()} after R Markdown files are
#' compiled.
#' @param build_fun Function used to build the Rmd files.
#' @note This function recompiles all R Markdown files by default, even if the
#' output files are newer than the source files. If you want to build the site
#' without rebuilding all R Markdown files, you should use
#' \code{\link{hugo_build}()} instead.
#' @export
build_site = function(
local = FALSE, method = c('html', 'custom'), run_hugo = TRUE
local = FALSE, method = c('html', 'custom'), run_hugo = TRUE,
build_fun = getOption('blogdown.build_rmds', build_rmds)
) {
if (missing(method)) method = getOption('blogdown.method', method)
method = match.arg(method)
Expand All @@ -44,7 +46,7 @@ build_site = function(
if (local && length(files)) {
files = getOption('blogdown.files_filter', timestamp_filter)(files)
}
build_rmds(files)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought a while about it and I tend to let users decide how to parallelize it because there are multiple ways of parallelization. Here is how I'd implement it:

build_fun = getOption('blogdown.build_rmds', function(files, build) {
  build(files)
})
build_fun(files, build_rmds)

Then users can set their own functions (in .Rprofile), e.g.

options(blogdown.build_rmds = function(files, build) {
  parallel::mclapply(files, build)
})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Updated the approach to hopefully be in line with your though process, albeit still exposing the build_fun as a parameter, not just retrieving it from options within the function. We can also remove the parameter if you like the other approach better.

  • Would suggest to keep the build_rmds_parallel(), as it gives users an easy way to get parallelization without having to implement themselves, using either

    options("blogdown.build_rmds" = blogdown:::build_rmds_parallel)

    or (assuming we keep the parameter) calling directly

    blogdown::build_site(build_fun = blogdown:::build_rmds_parallel)

build_fun(files)
if (run_hugo) on.exit(hugo_build(local), add = TRUE)
invisible()
}
Expand Down Expand Up @@ -163,3 +165,18 @@ encode_paths = function(x, deps, parent, base = '/', to_md = FALSE) {
dirs_rename(libs, to, clean = TRUE)
x
}

build_rmds_parallel = function(files) {
on.exit(if (exists("cl")) parallel::stopCluster(cl), add = TRUE)
no_cores = getOption('blogdown.parallelcores', parallel::detectCores())
# revert to legacy where parallelization irrelevant
valid_parallel = isTRUE(as.integer(no_cores) > 1L) && length(files) > 1L
if (!valid_parallel) build_rmds(files)
cl = parallel::makeCluster(no_cores)
# propagate blogdown-relevant options to the nodes
opts = options()
blogdown_opts = opts[grepl('^blogdown\\.', names(opts))]
if (length(blogdown_opts) > 0L) parallel::clusterCall(cl, options, blogdown_opts)
files = parallel::clusterSplit(cl = cl, seq = files)
parallel::parLapply(cl = cl, X = files, fun = build_rmds)
}
5 changes: 4 additions & 1 deletion man/build_site.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.