Subprocess with a Python Session?

Shane Hathaway shane at hathawaymix.org
Tue Dec 5 14:49:03 EST 2006


Calvin Spealman wrote:
> No matter what I do I cant get the following code to do what I expect.
> I hadn't used subprocess t o read and write to pipes of a
> still-running app, and I just can't seem to get it right. What gives?
> 
> import subprocess
> 
> p = subprocess.Popen("python", stdout=subprocess.PIPE, stdin=subprocess.PIPE)
> p.stdin.write('print 10\n')
> assert p.stdout.readline() == '10\n'

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.)

Shane




More information about the Python-list mailing list