[SOLVED] Optimal way to run a python script multiple time with different argument values


I have a python script that takes as input ~20 arguments.
I want to run this script multiple times with different values for the arguments each time.
At the moment I use a basic bash script like the following (with more parameters and more different values for each parameter)

for com_adv_par18 in 0.288 0.289
  for com_adv_par19 in 0.288 0.289
    for com_adv_par20 in 0.288 0.289
    python alpha2.py $com_adv_par18 $com_adv_par19 $com_adv_par20

I am worrying though that this is not the most optimal way to do it. Both coding and computing time wise .
Could you propose any alternative method to insert the parameters and run the program more efficiently?

Thanks in advance.


The answer to your question depends on a lot of things – a significant factor is the length of time each execution takes.

If you can refactor the alpha2.py script so that you can import it, then you could use a python wrapper script along these lines:

from alpha2 import do_something
from itertools import product

# define argument lists here, e.g. list1 = [0.288, 0.289], etc.

for args in product(list1, list2, list3):

Each execution will still be sequential but the advantage of this approach is that you don’t suffer the overhead of loading a new python instance for every combination of parameters.

Answered By – Tom Fenech

Answer Checked By – Pedro (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.