Home > Blockchain >  How to write a loop of command line to run asynchronously in python
How to write a loop of command line to run asynchronously in python

Time:08-06

I'm trying to write a series of linux command to run asynchronously in python, i can print out the linux command, but when I try to asyncio.run then it keep throwing error, does anyone know what Im doing wrong?

import asyncio
import subprocess

def get_args():

    parser = argparse.ArgumentParser(
        description='Run bulk generator in Gen server')

    parser.add_argument('--number_of_intercept', default='200',
                help='Number of intercept going into MSOM')

    return parser.parse_args()

async def bulk_generator (get_args):
    outputlist = []

    generator = "/bin/generator/./Generator.exe --direth -w 0000:84:00.1 -- -c 500 -d 62,598,62,1500,62 -r 500 -g eth vlan:vlan_stream-id=" str(get_args) " vlan:vlan_stream-id=" str(get_args) ",span_number_vlan_ids=1 binseqn stream-id=100,eth_src=11:22:33:44:55:66"
    print(generator)
    p = subprocess.Popen([generator], stdout=subprocess.PIPE)
    output = str(p.communicate())
    outputlist.append(output)
    return outputlist


if __name__ == "__main__":

    args = get_args()
    for i in range(1,int(args.number_of_intercept)):
      asyncio.run(bulk_generator(i))


**********************Output******************************
Traceback (most recent call last):
  File "/Users/jasonleung/generator.py", line 32, in <module>
    asyncio.run(bulk_generator(i))
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "/Users/jasonleung/generator.py", line 22, in bulk_generator
    p = subprocess.Popen([generator], stdout=subprocess.PIPE)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/subprocess.py", line 969, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/subprocess.py", line 1845, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/bin/generator/./Generator.exe --direth -w 0000:84:00.1 -- -c 500 -d 62,598,62,1500,62 -r 500 -g eth vlan:vlan_stream-id=1 vlan:vlan_stream-id=1,span_number_vlan_ids=1 binseqn stream-id=100,eth_src=11:22:33:44:55:66'

Process finished with exit code 1```

CodePudding user response:

When the command argument to subprocess.Popen() is a list, the command and arguments must be separate list elements. You put the entire command line in the first list element, so it thinks that's the name of the command. generator should be a list, then you don't need to wrap it in another list.

async def bulk_generator (get_args):
    outputlist = []

    generator = ["/bin/generator/./Generator.exe", "--direth", "-w", "0000:84:00.1", "--", "-c", "500", "-d", "62,598,62,1500,62", "-r", "500", "-g", f"eth vlan:vlan_stream-id={get_args} vlan:vlan_stream-id={get_args},span_number_vlan_ids=1 binseqn", "stream-id=100,eth_src=11:22:33:44:55:66"]
    print(generator)
    p = subprocess.Popen(generator, stdout=subprocess.PIPE)
    output = str(p.communicate())
    outputlist.append(output)
    return outputlist

CodePudding user response:

it’s because you’re passing the whole command with it’s arguments. Only include the executable as the first argument than have the second parameter be a list of all the arguments you want to pass for the executable

  • Related