Skip to content

Instantly share code, notes, and snippets.

@QinMing
Created May 1, 2025 21:22
Show Gist options
  • Save QinMing/b0da6cf4a61773948b013f2fbad01969 to your computer and use it in GitHub Desktop.
Save QinMing/b0da6cf4a61773948b013f2fbad01969 to your computer and use it in GitHub Desktop.
run lots of commands in a process pool
#!/bin/bash
set -x
set -e
# Add parallel execution logic
MAX_JOBS=6
run_parallel() {
# Check current job count and wait if the limit is reached
while (( $(jobs -p | wc -l) >= MAX_JOBS )); do
# Wait for any single job to finish
wait -n
done
# Launch the job in the background
"$@" &
}
run_parallel python -m something input1
run_parallel python -m something input2
#######################################################
# Wait for all background jobs to finish before exiting
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment