Home > Blockchain >  Is there a way to make the below code run faster? -dealing with running and extracting data from the
Is there a way to make the below code run faster? -dealing with running and extracting data from the

Time:07-29

I ran the below code to essentially read CMD commands from A txt file, and then extract the results in another txt file. the command looks like this:

ping "Hostname here" and there are A LOT of these.

the code I have looks like this:

cmds_file = pathlib.Path(r" path to file").joinpath("Newfile.txt")

output_file = pathlib.Path(r"path to file").joinpath("HostName_Results.txt")

with open(cmds_file, encoding="utf-8") as commands, open(output_file, "w", encoding="utf-8") 
as output:
for command in commands:
    command = shlex.split(command)
    output.write(f"\n# {shlex.join(command)}\n")
    output.flush()
    subprocess.run(command, stdout=output, encoding="utf-8")

The run time for the code for only 1000 rows of commands is slower than what I would like it to be. is there anything in the code I can augment to increase the speed while maintaining functionality? I am open to changing small things or having results extracted differently - I just need the results.

CodePudding user response:

it is slow mainly for 2 reasons:

First, every time you call an external process it is like starting a new program and this is a good part of the delay.

Second, subprocess.run do some operation before call the command passed, like checks and open pipes.

You could try to use a library like pyping to do pings

  • Related