Skip to content

Instantly share code, notes, and snippets.

@georgegu1997
Created June 14, 2023 03:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save georgegu1997/48188a422a7a812fb0ffa5e174430a29 to your computer and use it in GitHub Desktop.
Save georgegu1997/48188a422a7a812fb0ffa5e174430a29 to your computer and use it in GitHub Desktop.
Run multiple jobs with a max number of parallel processes

This is a function to run multiple jobs in parallel, but with a max number of parallel processes. New jobs will be run only if some previous jobs end and free up resources.

Credit to GPT-4.

First declare the following function in bash

function wait_for_jobs() {
  local num_jobs=$1
  while true; do
    current_jobs=$(jobs -p | wc -l)
    if [ "$current_jobs" -lt "$num_jobs" ]; then
      break
    fi
    sleep 1
  done
}

Then run command as follows

# Maximum number of parallel processes
max_jobs=12

# Change the arguments and actual program to your own
for ARG in 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1
do
  wait_for_jobs $max_jobs # This line will block if there are already $max_jobs processes
  echo $ARG
  python train.py --arg $ARG & # Don't forget the "&" sign. 
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment