Skip to content

Instantly share code, notes, and snippets.

@henriquemoody
Last active September 26, 2022 10:52
Show Gist options
  • Save henriquemoody/01895b219073b40f3912a8584ade66fd to your computer and use it in GitHub Desktop.
Save henriquemoody/01895b219073b40f3912a8584ade66fd to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
declare -r IFS=$'\n'
declare -r SPAWNED_LIST=$(mktemp)
declare -r SPAWNED_LIMIT=20
task() {
sleep $[RANDOM % 5]
}
task-in-parallel() {
local -r number=${number}
echo "Spawning task ${number}"
task &
echo "${!}" >> "${SPAWNED_LIST}"
}
can-spawn() {
while read pid; do
kill -0 ${pid} 2> /dev/null || sed --in-place "/${pid}/d" "${SPAWNED_LIST}"
done < "${SPAWNED_LIST}"
test $(wc --lines < "${SPAWNED_LIST}") -lt ${SPAWNED_LIMIT}
}
wait-for-spawn-slot() {
while true; do
can-spawn && break || continue
done
}
for number in {1..100}; do
wait-for-spawn-slot
task-in-parallel ${number}
done
wait
echo "All done!"
@karniemi
Copy link

@henriquemoody : Quite nice! wait-for-spawn-slot() & can-spawn() seems like a as-fast-as-i-can-busy-loop, though?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment