0
votes

I am trying to create a worker subprocess that runs in parallel with my main application. For this I am using subprocess.Popen and the built in PIPE's.

    p = subprocess.Popen( cmd , stdin=subprocess.PIPE, stdout=subprocess.PIPE, bufsize=1, universal_newlines=True)

    time.sleep(.5)

    data = p.stdout.readline()

    for i in range(10): # repeat several times to show that it works
        p.stdin.write("test")
        line = p.stdout.readline()
        print(line) # read output

I am able to receive data on the stdout PIPE into variable data, but the read statement in the loop always returns an empty string.

My subprocess is extremely simple:

print("Its working", flush=True)
while(True):
    r=input()
    print("Its working", flush=True)

It would seem my subprocess is not able to read any data sent through stdin. If I removed the r=input() command, and only constantly print a string, it works and I receive the line multiple times, but the moment I try and check for an incoming string through input(), it never gets it.

Am I trying to grab the data on the incoming PIPE incorrectly, and if so how do I do it? I have tried other options such as:

r = sys.stdin.buffer.read()
r = sys.stdin.buffer.readline()
r = sys.stdin.read()
r = sys.stdin.readline()

but none of them work.

1
You must send a newline (\n) to input in order for it continue. Change top.stdin.write("test\n") and it is likely to work better. - JohanL

1 Answers

0
votes

I was missing the \n from the main program side. Thanks JohanL for pointing that out. Everything is working now.