Skip to content

Instantly share code, notes, and snippets.

@mped-oticon
Created August 21, 2023 11:55
Show Gist options
  • Save mped-oticon/b11dafa937e694ce4fa6fbf2e160988a to your computer and use it in GitHub Desktop.
Save mped-oticon/b11dafa937e694ce4fa6fbf2e160988a to your computer and use it in GitHub Desktop.
Classic Fork-join parallelism in BASH, blocking and nestable
# Classic Fork-join parallelism. bash_parallel's can be nested arbitrarily
# Silent by default; set BASH_PARALLEL_VERBOSE=1 for verbose output on stderr
function bash_parallel
{
function bash_parallel_echo
{
if [[ $BASH_PARALLEL_VERBOSE == 1 ]] ; then
echo "$@" 1>&2
fi
}
# In subshell to allow nesting
(
i=0
unset BASH_PARALLEL_PIDS # allow nesting
unset BASH_PARALLEL_CMDS # allow nesting
for cmd in "$@"
do
( $cmd ) & # in subshell to avoid environmental pollution
BASH_PARALLEL_PIDS[$i]=$!
BASH_PARALLEL_CMDS[$i]="$cmd"
bash_parallel_echo "bash_parallel: started PID ${BASH_PARALLEL_PIDS[$i]}: $cmd"
i=$(($i + 1))
done
bash_parallel_echo "bash_parallel: waiting for PIDs ${BASH_PARALLEL_PIDS[@]}"
# bash_parallel_wait: builtin wait does not take OR of return values
function bash_parallel_wait
{
local bash_parallel_ret=0
for bash_parallel_pid in ${BASH_PARALLEL_PIDS[@]}
do
# Wait for completion, obtain return code and OR together
wait $bash_parallel_pid
bash_parallel_ret_tmp=$?
bash_parallel_ret=$(( $bash_parallel_ret | $bash_parallel_ret_tmp ))
bash_parallel_echo "bash_parallel: $bash_parallel_pid returned with exit code $bash_parallel_ret_tmp"
done
( exit $bash_parallel_ret )
}
bash_parallel_wait
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment