1

I have a problem where I want to call multiple .exe files from Python at once and one .exe has like 5 arguments. How can I call it? I tried os.system("process.exe arg1 arg2 arg3 arg4") which works but it can only launch one process at time and I also tried subprocess.run, however while it can call process like subprocess.run("process.exe") I can't seem to find solution how to send arguments like subprocess.run("process.exe arg1 arg2 arg3"). I need it to also open commandline as stdout which os.system command can do.

1 Answer 1

2

If you want to spawn multiple processes simultaneously ("asynchronously", "in parallel"), use

p = subprocess.Popen("<command> <args>", shell=True, stdout=subprocess.PIPE)

which instantly returns the Popen instance (object representing the spawned process).

shell=True allows you to pass the command as a "simple string" (e.g. "myprog arg1 arg2"). Without shell=True you must pass the list of arguments, including the name of a program as a first arg (e.g. ["myprog", "arg1", "arg2"]).

stdout=PIPE ensures stdout capturing in a pipe (a file-like object in Python terms), which may be accessed via p.stdout. Use can read the output of your program via p.stdout.read().decode(), but note that this call is blocking, i.e. it awaits the program to stop, so in your case make sure that you have spawned all necessary processes before reading their outputs.

In order to await the program without reading its output, use p.wait().

More information in Python docs: subprocess.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! This is exactly what I have been looking for...is there any chance that it an also open cmd and print stdout into that command line?
Do you want to pass some text into the stdin of the program? Then pass stdin=subprocess.PIPE option to the Popen constructor, and use p.stdin as a file-like object, e.g. p.stdin.write("some text input".encode()). Also, take a look at Popen.communicate method, but note this is blocking, so in your case it is better to stick to the solution with pipes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.