Subprocess with a Python Session?
Paul Boddie
paul at boddie.org.uk
Thu Dec 7 07:14:41 EST 2006
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.
Paul
[1] http://pexpect.sourceforge.net/
More information about the Python-list
mailing list