Python wrapper, problem with subprocess read/write

A.T.Hofkamp hat at se-162.se.wtb.tue.nl
Mon Sep 10 06:31:39 EDT 2007


On 2007-09-07, NeoGregorian <neogregorian at gmail.com> wrote:
> I tried instead to use:
>
> lines = []
> line = proc.stdout.readline()
> while line :
>     lines.append(line)
>     line = proc.stdout.readline()
>
> This prints out everything except the ">" line, which is good. But
> then freezes while waiting for input, which is bad.
>
> Any suggestions on how to solve this in a good way?

'readline()' reads a line, that is, some text ending with a new-line. Since
your last line, the ">" prompt has no ending new-line, the call blocks, waiting
for the new-line character.

So the simple anser is "don't use readline()".

You have to fall back to reading characters, such as "read(1)" (which block
until it receives a character).
In addition, you will have to do analysis on whether the line you are currently
reading is a prompt, and if so, stop reading to prevent blocking.
(and instead, give the program a command by writing to proc.stdin).


In case you don't know, pexpect (Python expect) does all (and more) that you
are trying to do.


Sincerely,
Albert



More information about the Python-list mailing list