I am starting a subprocess
in Python and trying to read each line of output. Unfortunately I can't find a good way of testing if my processes is still alive. The standard method seems to be checking poll()
, but that seems to always return None
. Here is my code.
proc = Popen(sys.argv[1:], stdin=PIPE, stdout=PIPE, stderr=STDOUT)
while True:
process_line(proc.stdout.readline().decode('utf-8'))
if not proc.poll():
break
for line in proc.communicate()[0].splitlines():
process_line(line.decode('utf-8'))
I've also tried using os.kill(proc.pid, 0)
, which works for non-spawned processes, but it seems that Python keeps a handle on processes it starts so os.kill(proc.pid, 0)
always returns.
What am I doing wrong?