Skip to content

Instantly share code, notes, and snippets.

@lhns
Last active June 28, 2022 15:12
Show Gist options
  • Save lhns/28ffb548d26ada33b7361a9ad811621b to your computer and use it in GitHub Desktop.
Save lhns/28ffb548d26ada33b7361a9ad811621b to your computer and use it in GitHub Desktop.
runParallel() {
local batchSize=1
if [ "$1" == "--batch" ]; then
batchSize="$2"
shift 2
fi
local concurrency="$1"
shift
local tasks=()
local exitCode=0
local elemExitCode=0
[[ $- = *e* ]]; local setE=$?
set +e
while IFS= read -r elem; do
while (( ${#tasks[@]} >= $concurrency )); do
for i in ${!tasks[@]}; do
if ! [ -d /proc/"${tasks[$i]}"/ ]; then
wait "${tasks[$i]}" || exitCode="$?"
unset tasks[$i]
fi
done
if (( ${#tasks[@]} >= $concurrency )); then
sleep 0.01
fi
done
local elems=( "$elem" )
if (( $batchSize > 1 )); then
mapfile -t -O 1 -n "$(( $batchSize - 1 ))" elems
fi
{
exitCode=0
for elem in "${elems[@]}"; do
(
(( $setE )) && set -e
"$@" "$elem"
)
elemExitCode="$?"
(( $elemExitCode )) && exitCode="$elemExitCode"
done
exit "$exitCode"
} &
tasks=( "${tasks[@]}" "${!}" )
done
for task in "${tasks[@]}"; do
wait "$task" || exitCode="$?"
done
(( $setE )) && set -e
return "$exitCode"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment