[英]Printing to stdout in subprocess
我有一个脚本,该脚本按如下方式运行子流程:
child_process = subprocess.Popen(["python", testset['dir'] + testname, \
output_spec_file, plugin_directory],\
stderr=subprocess.PIPE, stdout=subprocess.PIPE)
在此过程中,我尝试插入打印语句,但它们似乎没有显示在stdout中。 我尝试在该子sys.stduout.read()
使用sys.stdout.write()
,然后在child_process
之后child_process
sys.stduout.read()
,但它没有捕获输出。
我是Python的新手,我还没有达到Python的那种复杂程度。 我实际上在C语言中工作较低,并且有一些Python测试脚本,而且我不确定如何从子进程中打印出来。
有什么建议么?
sys.stdout.read
(和write
)用于当前进程 (而不是子进程 )的标准输入/输出。 如果要写入子进程的stdin,则需要使用:
child_process.stdin.write("this goes to child") #Popen(..., stdin=subprocess.PIPE)
从孩子的stdout流中读取内容类似:
child_process = subprocess.Popen( ... , stdout=subprocess.PIPE)
child_process.stdout.read("This is the data that comes back")
当然,通常使用起来更惯用:
stdoutdata, stderrdata = child_process.communicate(stdindata)
(注意通过subprocess.PIPE
到POPEN构造适当的地方)提供的输入数据,可以一次全部通过。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.