Subprocess with a Python Session?

Giovanni Bajo noway at ask.me
Thu Dec 7 06:21:24 EST 2006


Fredrik Lundh 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')
> 
> + p.stdin.close()
> 
>> assert p.stdout.readline() == '10\n'

Yeah, but WHY was the API designed like this? Why can't I read and write 
freely from a pipe connected to a process as many times as I want?
-- 
Giovanni Bajo



More information about the Python-list mailing list