How to send a new command to a subprocess

2019-05-11 02:20发布

问题:

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)

回答1:

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