与进程交互:将数据发送到stdin。从stdout和stderr读取数据,直到到达文件末尾。
因此,communicate()
运行后,该过程已 。
如果您想在不等待过程停止的情况下进行读写:
不要使用shell=True
-它不需要调用外壳程序来依次调用您的程序,因此您和您的程序之间将存在另一个进程。这有很多令人不快的副作用。默认值为,shell=False
因此您应该坚持使用。将Popen
行更改为:
p = subprocess.Popen(["./AO_FelixStrategy_UnitTest",
"--bats", "31441", "--chix", "12467",
"--enxutp", "31884", "--turq", "26372",
"--symbol", "SOGN", "--target_date", '2009-Oct-16'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
使用p.stdin.write
写入过程。用于p.stdout.read
从中读取。
调用p.stdout.read
如果没有什么read将被阻塞。p.stdin.write
如果写缓冲区已满,则调用将阻塞。因此,您必须确保您有一些要读/写的东西-您可以在Unix OS上使用来进行操作select
。在Windows上,不幸的是您必须诉诸线程。至少那是Popen.communicate
内部的。
根据您的描述,这是一些示例代码。它的工作取决于AO_FelixStrategy_UnitTest
开发方式:
p = subprocess.Popen(["./AO_FelixStrategy_UnitTest",
"--bats", "31441", "--chix", "12467",
"--enxutp", "31884", "--turq", "26372",
"--symbol", "SOGN", "--target_date", '2009-Oct-16'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
output = p.communicate('S\nL\n')[0]
print output