Reputation: 910
From what I can tell, execv overtakes the current process, and once the called executable finishes, the program terminates. I want to call execv multiple times within the same script, but because of this, that cannot be done.
Is there an alternative to execv that runs within the current process (i.e. prints to same stdout) and won't terminate my program? If so, what is it?
Upvotes: 0
Views: 158
Reputation: 19318
Yes, use subprocess
.
os.execv*
is not approporiate for your task, from doc:
These functions all execute a new program, replacing the current process; they do not return. On Unix, the new executable is loaded into the current process, and will have the same process id as the caller.
So, as you want the external exe to print to the same output, this is what you might do:
import subprocess
output = subprocess.check_output(['your_exe', 'arg1'])
By default, check_output()
only returns output written to standard output. If you want both standard output and error collected, use the stderr
argument.
output = subprocess.check_output(['your_exe', 'arg1'], stderr=subprocess.STDOUT)
Upvotes: 1
Reputation: 375564
The subprocess module in the stdlib is the best way to create processes.
Upvotes: 0