Search code examples
pythonsubprocessstdoutstdinpopen

using Python's subprocess for reading the output of a blocking command sent through stdin


I have the following code:

proc = subprocess.Popen(["adb", "shell"], stdout.subprocess.PIPE, subprocess.PIPE)
proc.stdin.write("cat /proc/kmsg")

The command keeps outputting data to the stdout, and I'm interested in stopping the command (CTRL-C) at a certain time, and reading the whole outputted data.
Right now the only thing I'm able to do is proc.stdout.readline() and hoping the next line is not empty.
I'm interested in stopping the command and reading whatever lines I have consumed up until this point.


Solution

  • Just call proc.kill() then proc.communicate() to finish reading.