I have a script that uses argsparse
to modify its inputs. For the example below, it runs a series of simulations, one after the other, based on the given arguments:
def main():
parser = argparse.ArgumentParser(description="Simulation of drivers' behavior")
parser.add_argument('-f', '--fleet',
help='Fleet sizes to simulate, formatted as comma-separated list (i.e. "-f 250,275,300")')
parser.add_argument('-m', '--multiplier',
help='Surge multiplier, formatted as comma-separated list (i.e. "-m 1,1.5,2")')
args = parser.parse_args()
if args.fleet:
fleet_sizes = [int(x) for x in args.fleet.split(',')]
else:
fleet_sizes = FLEET_SIZE
if args.multiplier:
surges = [float(x) for x in args.multiplier.split(',')]
else:
surges = [SURGE_MULTIPLIER]
and from command line I typically run it as:
python script.py -f 1000,1500 -m 1,2
Now, I want to use my linux bash to run this scripts in parallel. The typical approach seems to be something like this:
for i in {1000, 1500}; do python script.py -f i & done; done
But this doesn't work, because it can't parse i
properly:
ValueError: could not convert string to float: 'i'
(I have tried changing my argparse handling function, but that didn't seem to solve it). Some solutions suggest using sys.argv[1]
but 1) I don't want to change from argparse to that 2) it seems like argparse is the superior way in general.
My question is this: How can I use bash to run my python script in parallel, given that it uses argparse to collect input for each variable in the code?