Created
December 19, 2023 11:13
-
-
Save actionjack/ef70853fad990f7cc62abff8c782b790 to your computer and use it in GitHub Desktop.
Bash script with concurrency control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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