[英]Printing to stdout in subprocess
I have a script which runs a subprocess as follows: 我有一个脚本,该脚本按如下方式运行子流程:
child_process = subprocess.Popen(["python", testset['dir'] + testname, \
output_spec_file, plugin_directory],\
stderr=subprocess.PIPE, stdout=subprocess.PIPE)
In that process, I am trying to insert print statements but they are not appearing to stdout. 在此过程中,我尝试插入打印语句,但它们似乎没有显示在stdout中。 I tried using
sys.stdout.write()
in that subprocess and then sys.stduout.read()
right after child_process
but it is not capturing the output. 我尝试在该子
sys.stduout.read()
使用sys.stdout.write()
,然后在child_process
之后child_process
sys.stduout.read()
,但它没有捕获输出。
I am new to Python and I haven't gotten to that level of complexity in Python. 我是Python的新手,我还没有达到Python的那种复杂程度。 I am actually working low level in C and there are some Python test scripts and I'm not sure how to print out from the subprocess.
我实际上在C语言中工作较低,并且有一些Python测试脚本,而且我不确定如何从子进程中打印出来。
Any suggestions? 有什么建议么?
sys.stdout.read
(and write
) are for standard input/output of the current process (not the subprocess). sys.stdout.read
(和write
)用于当前进程 (而不是子进程 )的标准输入/输出。 If you want to write to stdin of the child process, you need to use: 如果要写入子进程的stdin,则需要使用:
child_process.stdin.write("this goes to child") #Popen(..., stdin=subprocess.PIPE)
and similar for reading from the child's stdout stream: 从孩子的stdout流中读取内容类似:
child_process = subprocess.Popen( ... , stdout=subprocess.PIPE)
child_process.stdout.read("This is the data that comes back")
Of course, it is generally more idiomatic to use: 当然,通常使用起来更惯用:
stdoutdata, stderrdata = child_process.communicate(stdindata)
(taking care to pass subprocess.PIPE
to the Popen constructor where appropriate) provided that your input data can be passed all at once. (注意通过
subprocess.PIPE
到POPEN构造适当的地方)提供的输入数据,可以一次全部通过。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.