How to perform a nonblocking read from a process

rdabane at gmail.com rdabane at gmail.com
Wed Jun 4 03:20:59 CEST 2008


Hi,
I'm trying to perform following operation from inside the python
script
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 ..

Thanks,
-Rahul.






More information about the Python-list mailing list