Python's subprocess with interactive child process

6 Jun 2017

I misunderstood how stdin worked.

  • I thought it would be difficult, for python, to run an interactive child process because we would need to proxy stdin and stdout.
  • I thought that stdin was only read when a child process demanded an input.

It turns out that a process is always reading stdin. We can send an input to a process at any time. The process will read an input when it needs the input. This means we can type yes before we are being prompted.

Python seems to proxy stdin for us already. Therefore, we don't need to assign stdin to sys.stdin. Here's an example:

import sys import subprocess proc = subprocess.Popen('gdb', shell=True) print proc.communicate() print print print 'Run another gdb process. This proves that we can just re-use sys.stdin.' print print proc = subprocess.Popen('gdb', shell=True) print proc.communicate()

gdb is an interactive debugger. This code seems to work fine.