0

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?

Alex
  • 978
  • 1
  • 9
  • 22
  • 1
    Use `"$varname"` to expand a variable (replacing it with its value): `python script.py -f "$i"` – Charles Duffy Jun 17 '19 at 01:32
  • 1
    ...this is not in any way specific to Python, or to argparse; you simply weren't passing the value as an argument at all. It's the same as how `for ((i=1000;i<1500;i++)); do echo i; done` prints `i` over and over unless it's changed to `echo "$i"`. – Charles Duffy Jun 17 '19 at 01:37
  • You commandline does not match the help. The coma separated lists should not have spaces. Display `sys.argv` so you have a clear idea what you getting from the shell. Peint `args` so you have a clear idea what the parser produces. – hpaulj Jun 17 '19 at 01:42
  • @hpaulj thanks, I have fixed it here. It was me not typing it correctly here, but in the original code there are no spaces. – Alex Jun 17 '19 at 01:45
  • @CharlesDuffy Thank you for your answer and time! I am trying your suggestion (the other duplicate question seems very complicated right now) – Alex Jun 17 '19 at 01:48
  • 1
    With `nargs` and `type` parameters you can accept '-f 1000 1500' without the extra split step. – hpaulj Jun 17 '19 at 01:50

0 Answers0