Created
April 10, 2022 16:06
-
-
Save justmiles/44a3df8690a17b6021335e4a6b7c73a1 to your computer and use it in GitHub Desktop.
Example concurrency with bash
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
#!/bin/bash | |
# wait_for_workers waits until a worker is available | |
wait_for_workers() { | |
while test $(jobs -p | wc -w) -ge "$1"; do wait -n; done | |
} | |
cat data.txt | while read line; do | |
ensure_max_workers $(nproc --all) # use nproc to get number of CPUs | |
( | |
echo "Do work with $line" | |
sleep 10 | |
) & | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment