I am using a Bash script to execute a Python script multiple times. In order to speed up the execution, I would like to execute these (independent) processes in parallel. The code below does so:
#!/usr/bin/env bash
script="path_to_python_script"
N=16 # number of processors
mkdir -p data/
for i in `seq 1 1 100`; do
for j in {1..100}; do
((q=q%N)); ((q++==0)) && wait
if [ -e data/file_$i-$j.txt ]
then
echo "data/file_$i-$j.txt exists"
else
($script -args_1 $i > data/file_$i-$j.txt ;
$script -args_1 $i -args_2 value -args_3 value >> data/file_$i-$j.txt) &
fi
done
done
However, I am wondering if this code follow common best practices of parallelization of for loops in Bash? Are there ways to improve the efficiency of this code?