Skip to content

Instantly share code, notes, and snippets.

@justmiles
Created April 10, 2022 16:06
Show Gist options
  • Save justmiles/44a3df8690a17b6021335e4a6b7c73a1 to your computer and use it in GitHub Desktop.
Save justmiles/44a3df8690a17b6021335e4a6b7c73a1 to your computer and use it in GitHub Desktop.
Example concurrency with bash
#!/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