I want to run a program from python and find its memory usage. To do so I am using:
l=[\'./a.out\',\'<\',\'in.txt\',\'>\',\'out.txt\']
p=subprocess.Popen(l
You are using shell redirection characters in your call, but when you use subprocess
, and set shell=False
, you have to handle those pipes manually.
You seem to be passing those redirection characters directly as arguments to the a.out
program.
Try running this in your shell:
./a.out '<' in.txt '>' out.txt
See if a.out
terminates then as well.
There are two issues (at least):
<
, >
redirection is handled by a shell. subprocess
doesn't spawn a shell by default (you shouldn't either)stdout=PIPE
, stderr=PIPE
then you must read from the pipes otherwise the process may block foreverTo make a subprocess read from a file and write to a file:
from subprocess import check_call, STDOUT
with open('in.txt') as file, open('out.txt', 'w') as outfile:
check_call(["./a.out"], stdin=file, stdout=outfile, stderr=STDOUT)
stderr=STDOUT
merges stdout, stderr.