How to perform a nonblocking read from a process

rdabane at rdabane at
Wed Jun 4 03:20:59 CEST 2008

I'm trying to perform following operation from inside the python
1. Open a shell ( start a process )
2. Send command1 to the process
3. Get output from the process
4. Send command2 to the process
5. Get output from the process

Following is sample code :

from subprocess import *
p2 = Popen('python',stdin=PIPE,stdout=PIPE,universal_newlines=True)
for i in range(10):
    p2.stdin.write('print 10'+'\n')
    o,e = p2.stdout.readline()
    print o,e

It seems that stdout.readline() is a blocking read and it just gets
stuck their..
How to fix this ..

All the help is appreciated ..


More information about the Python-list mailing list