0

The subprocess will output several characters, and I want to send some response via stdin. The length of the output is predictable, so I wrote code like this:

p = Popen("myproc", shell = True, stdin = PIPE, stdout = PIPE)
p.stdout.read(1)

However, p.stdout.read(1) doesn't return even if the process had already output more than a character. How can I make it stop blocking after reading a byte sequence of the expected length?

173210
  • 117
  • 10
  • If you can, use select and poll. Also try using `flush()`. – Torxed Feb 23 '16 at 09:41
  • This can be difficult to get right. You might be better off using [`pexpect`](http://pexpect.readthedocs.org) for reading/writing pipes in this manner. – mhawke Feb 23 '16 at 11:16

1 Answers1

1

myproc may use a block-buffering mode when the standard output is redirected i.e., your parent Python won't see anything until the corresponding buffer in the child is flushed. See several ways to workaround it in this answer (and follow the corresponding links there).

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670