3

I previously asked a question about how to set up a tkinter gui to recieve lines from a subprocess without the entire program hanging. That is now functional.

Now, I can't figure out how to send new lines to a subprocess. I've tried using process.communicate, but I might have been using it wrong. I also tried this question's solution, but self.process.stdin.write('stop\n'.encode()) doesn't seem to work. How do I send new commands to the child python subprocess?

Relevant code:

self.process = subprocess.Popen([ "python", "-u", dir + "start.py" ], 
    stdout=subprocess.PIPE, 
    stdin=subprocess.PIPE, 
    stderr=subprocess.PIPE, 
    cwd=dir)
Community
  • 1
  • 1
Chiri Vulpes
  • 478
  • 4
  • 18
  • That should work.... assuming `start.py` is reading stdin. Does start.py produce a lot of output (it could be hanging on its output pipes) or is it doing something that "reaches around" stdin for the controlling terminal (e.g., prompt for password). Is this linux or windows? – tdelaney Dec 06 '14 at 19:07
  • You could add `self.process.stdin.flush()` but I think its line buffered. Worth a shot though. – tdelaney Dec 06 '14 at 19:08
  • That works! Thank you so much! Do you want to write that as the answer to this question, or should I? – Chiri Vulpes Dec 06 '14 at 19:44
  • I like the fact that I got the right answer by guessing wrong. I'll write it up. – tdelaney Dec 06 '14 at 19:47

1 Answers1

3

The data may be stuck in the pipe. Add self.process.stdin.flush() after the write.

tdelaney
  • 73,364
  • 6
  • 83
  • 116