Created
May 1, 2025 21:22
-
-
Save QinMing/b0da6cf4a61773948b013f2fbad01969 to your computer and use it in GitHub Desktop.
run lots of commands in a process pool
This file contains hidden or 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 | |
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