Subprocess with a Python Session?

El Pitonero pitonero at gmail.com
Thu Dec 7 10:16:25 EST 2006


Paul Boddie wrote:
> Shane Hathaway wrote:
> >
> > Make sure the pipes are unbuffered.  Launch the process with "python -u"
> > and flush() the streams after writing.  (That's the issue I've
> > encountered when doing this before.)
>
> The -u option is critical, yes. I wrote some code recently which
> communicated with a subprocess where the input/output exchanges aren't
> known in advance, and my technique involved using socket.poll and one
> character reads from the subprocess. I note that Pexpect is also
> conservative about communicating with subprocesses (see the "$ regex
> pattern is useless" section on the Pexpect site [1]).
>
> Generally, any work with asynchronous communications, or even
> socket/pipe programming in a wider sense, seems to involve ending up
> with working code that looks a lot like the code you started out with,
> but only after a period of intense frustration and with a few minor
> adjustments being made to separate the two.

Is there something equivalent to the "-u" option for a shell like
"bash"? In general (whether the subprocess is bash or python), how can
one make sure that once something is written into the subprocess'
stdin, the output from its stdout is fully completed and the subprocess
is ready to receive another command? Is there some kind of signal or
return status code one can capture?

-- P.




More information about the Python-list mailing list