Communicate with subprocess without waiting for the subprocess to terminate on windows

瘦欲@ 提交于 2019-12-06 02:15:26

问题


I have a simple echoprocess.py:

import sys

while True:
    data = sys.stdin.read()
    sys.stdout.write("Here is the data: " + str(data))

And a parentprocess.py

from subprocess import Popen, PIPE

proc = Popen(["C:/python27/python.exe", "echoprocess.py"],
             stdin = PIPE,
             sdtout = PIPE)

proc.stdin.write("hello")
print proc.stdout.read()

This just hangs until echoprocess.py is terminated. I want to communicate with this subprocess multiple times without having to restart it again. Is this kind of interprocess communication possible with the Python subprocess module on Windows?


回答1:


The main problem is with the line...

print proc.stdout.read()

The read() method when used with no parameters will read all data until EOF, which will not occur until the subprocess terminates.

You'll probably be okay with line-by-line reading, so you can use...

proc.stdin.write("hello\n")
print proc.stdout.readline()

...otherwise you'll have to work out some others means of delimiting 'messages'.

You'll have to make a similar change to echoprocess.py, i.e. change...

data = sys.stdin.read()

...to...

data = sys.stdin.readline()

You may also have issues with output buffering, so it may be necessary to flush() the buffer after doing a write.


Putting all this together, if you change echoprocess.py to...

import sys

while True:
    data = sys.stdin.readline()
    sys.stdout.write("Here is the data: " + str(data))
    sys.stdout.flush()

...and parentprocess.py to...

from subprocess import Popen, PIPE

proc = Popen(["C:/python27/python.exe", "echoprocess.py"],
             stdin = PIPE,
             stdout = PIPE)

proc.stdin.write("hello\n")
proc.stdin.flush()
print proc.stdout.readline()

...it should work the way you expect it to.



来源:https://stackoverflow.com/questions/16592893/communicate-with-subprocess-without-waiting-for-the-subprocess-to-terminate-on-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!