Help need with subprocess communicate

Daniel Klein danielk at
Thu Jun 5 20:11:14 CEST 2008

On Tue, 3 Jun 2008 23:48:38 -0700 (PDT), rdabane at wrote:

>On Jun 3, 11:23 pm, Dennis Lee Bieber <wlfr... at> wrote:
>> On Tue, 3 Jun 2008 18:04:40 -0700 (PDT), rdab... at declaimed the
>> following in comp.lang.python:
>> > Hi Daniel,
>> > Thanks for your reply..
>> > I've done exactly as you suggested...but I'm still having problem with
>> > the just gets stuck in
>> > the read ( I think because its a blocking read...)
>>         And it is likely blocking because the subprocess is doing buffered
>> output -- ie, nothing is available to be read because the output has not
>> been flushed.
>>         This is a problem with most programs when run as a subprocess -- it
>> is common for stdout, when routed to a pipe or file, to behave as a
>> buffered stream that only flushes when some x-bytes have been written;
>> unlike stdout to a console which gets flushed on each new-line.
>> --
>>         Wulfraed        Dennis Lee Bieber               KD6MOG
>>         wlfr... at              wulfr... at
>>                 HTTP://
>>         (Bestiaria Support Staff:               web-a... at
>>                 HTTP://
>Is there way to configure the stdout buffer size so that it flushes
>Is there a way to make above mentioned piece code working?

I'm not so sure it is a buffer problem. To test this out I first created a
'' script...

from subprocess import *
import os
print p.stdout.readline()[:-1] # strip \n from end of line
p.stdin.write('hi' + os.linesep)
print p.stdout.readline()[:-1]
p.stdin.write('bye' + os.linesep)
print p.stdout.readline()[:-1]

I then created the following VB console application (this is the 'process'
that is being 'Popen'd and is in my %PATH%)...

Module Module1
    Dim x As String
    Sub Main()
        Console.WriteLine("Process started...")
        x = Console.ReadLine()
        x = Console.ReadLine()
    End Sub
End Module

Here is the output when I run it...

Process started...

Note that I didn't have to 'flush()' anything.

I got the same thing working with a C program. I don't know why it won't
work with a similar python script...

import sys
sys.stdout.write('process started...\n')
r = sys.stdin.readline()
sys.stdout.write(r + '\n')
s = sys.stdin.readline()
sys.stdout.write(s + '\n')

I called this ''. When I plug this into the '' script I get
nothing, it just hangs. So maybe there is something else I am missing.

I normally don't do things this way cos there are os size limits to what you
can send/recv, so I use my own protocol (similar to netstrings)  for

Daniel Klein

More information about the Python-list mailing list