2

我正在开发一个 Python 应用程序,它需要不时产生一个子进程(用 C 编写),向它提供一些二进制数据并获得回复。子进程只会在需要时生成,并且只会服务一个请求。我在这里有什么选择?使用标准输入/标准输出是否安全?

4

1 回答 1

3
from subprocess import Popen,PIPE

# Example with output only
p = Popen(["echo", "This is a test"], stdout=PIPE)
out, err = p.communicate()
print out.rstrip()

# Example with input and output
p = Popen("./TestProgram", stdin=PIPE, stdout=PIPE)
out, err = p.communicate("This is the input\n")
print out.rstrip()

程序TestProgram从中读取一行stdin并将其写入stdout. 我已经.rstrip()在输出中添加了删除尾随的换行符,对于您的二进制数据,您可能不想这样做。

于 2013-09-17T06:41:23.740 回答