Skip to content

Instantly share code, notes, and snippets.

@actionjack
Created December 19, 2023 11:13
Show Gist options
  • Save actionjack/ef70853fad990f7cc62abff8c782b790 to your computer and use it in GitHub Desktop.
Save actionjack/ef70853fad990f7cc62abff8c782b790 to your computer and use it in GitHub Desktop.
Bash script with concurrency control
#!/usr/bin/env bash
MAX_CONCURRENCY=10 # max task slots
sub_task() {
sleep $(( ( RANDOM % 10 ) + 1 ))
echo "Task $1 finished at $(date)"
}
mkfifo testfifo
exec 10<>testfifo && rm -f testfifo
for _ in $(seq 1 ${MAX_CONCURRENCY}); do { echo >&10; } done
for T in {01..30}; do
read -r -u10
{
### do something
sub_task $T
echo "finish: $T with $?"
echo >&10
} &
done
wait
exec 10>&-
exec 10<&-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment