safe_mclapply: Fork-safe parallel lapply

View source: R/Utils.R

safe_mclapplyR Documentation

Fork-safe parallel lapply

Description

Drop-in replacement for parallel::mclapply that prevents deadlocks from nested fork calls. Uses an R option (.DiceView_parallel_depth) inherited by forked children to detect nesting and fall back to lapply in that case. This avoids the classic pipe deadlock where a forked worker tries to fork grandchildren while the parent is still waiting on pipes.

Usage

safe_mclapply(X, FUN, ..., mc.cores = getOption("mc.cores", maxWorkers()))

Arguments

X

a vector (atomic or list)

FUN

the function to be applied to each element of X

...

optional arguments to FUN

mc.cores

number of cores (default: getOption("mc.cores", parallel::detectCores()))

Value

list of results, like lapply

Examples

safe_mclapply(1:4, function(i) i^2)
# nested call falls back to lapply automatically:
outer <- safe_mclapply(1:2, function(i) safe_mclapply(1:3, function(j) i*j))

DiceView documentation built on June 17, 2026, 5:07 p.m.