Subprocess read and write
I used the examples from the documentation. I was able to get it to write to the process, but I'm having trouble getting it to read.
I'm having it call an executable I wrote in C++. Here's the source:
Code:
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[]){
string pizza;
[B]cout<<"hello, type something"<<endl;[/B]
cin>>pizza;
cout<<"You typed "<<pizza<<endl;
}
When I just run the executable it works as expected.
Here's my python script:
Code:
#!/usr/bin/env python
import subprocess
pizza = subprocess.Popen("./hello",stdout=subprocess.PIPE,stdin=subprocess.PIPE,stderr=subprocess.PIPE)
print "opened"
[B]print pizza.stdout.read()[/B]
print "writing 'hello' to ./hello subprocess"
pizza.stdin.write("hello")
print "wrote 'hello' to ./hello subprocess"
print pizza.stdout.read()
So, when I run ./subprocesstest.py it should run the executable, read from its stdout, write 'hello' to its stdin, then read its stdout again. Unfortunately it gets stuck when it tries to read. The Bold portion of the two code segments are portions I tried commenting out and trying it. This makes it so it writes to the ./hello subprocess before reading from it. That appears to have worked, as it then outputs:
opened
writing 'hello' to ./hello subprocess
wrote 'hello' to ./hello subprocess
whereas with them uncommented it outputs:
opened
So, it's getting stuck every time it tries to read. Any ideas why?
Thanks,
Brett Bretterson