python 如何重定向子进程的标准输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2161409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to redirect stdout for a subprocess?
提问by MA1
def StartProc(dir, parm):
global proc
proc_log = open(dir + os.sep + "MyLog.txt","w") #new path for each file
if parm:
proc = subprocess.Popen(path, 0, None, subprocess.PIPE, proc_log, None)
else:
MyReset(proc) #reset the process(proc) to its default values
proc.stdout = proc_log #no effect
print "fptr ", proc.stdout
#endif
#enddef
prm = True
for i in range(0, 5):
StartProc(i, prm)
prm = False
#endfor
What I want to do is to start an executable only once, but on each iteration I want to redirect the process output to a different file. What is happening, is that files are created in the different path, but output is redirected to the file that is created first time.
我想要做的是只启动一次可执行文件,但在每次迭代时,我想将进程输出重定向到不同的文件。发生的情况是,文件是在不同的路径中创建的,但输出被重定向到第一次创建的文件。
Note: MyReset()
initializes the process (executable) to its default values after the first iteration.
注意:MyReset()
在第一次迭代后将进程(可执行)初始化为其默认值。
Will the following line change the process stdout to new file?
以下行会将进程标准输出更改为新文件吗?
proc.stdout = proc_log
采纳答案by liwp
Like unwind said, you cannot change the file descriptor that the child process is writing its output to.
就像 unwind 所说的那样,您不能更改子进程将其输出写入的文件描述符。
What you can do, is read the output from the child process in your python script and then write it back to whatever file you want to. E.g.:
您可以做的是在您的python 脚本中读取子进程的输出,然后将其写回您想要的任何文件。例如:
proc = subprocess.Popen(path, 0, None, subprocess.PIPE, subprocess.PIPE, None)
for l in proc.stdout.readlines():
output_file.write(l)
Obivously, you'll need to figure out how control should behave in your app, i.e. can you do the writing from the main thread, when should the main thread return from StartProc()
in that case, or do you have to do the writing from another thread so that the main thread can return from StartProc()
immediately.
显然,您需要弄清楚控制在您的应用程序中的行为方式,即您可以从主线程进行写入,StartProc()
在这种情况下主线程应该何时返回,还是必须从另一个线程进行写入以便主线程可以StartProc()
立即返回。
回答by unwind
You can't. Once the process is started, its stdout is nothing you can change from the outside. You need to get inside that process' space to muck with its file descriptors.
你不能。一旦进程开始,它的标准输出就不是你可以从外部改变的了。您需要进入该进程的空间来处理其文件描述符。
If you have a way (in MyReset()
I guess) to talk to the running process, perhaps you can engineer a way to pass a new filename for it to (re)open as its stdout, that way.
如果你有办法(MyReset()
我猜)与正在运行的进程交谈,也许你可以设计一种方法来传递一个新的文件名,以这种方式(重新)打开作为它的标准输出。
回答by user553965
There are subtle problems/bugs in python related to subprocess.PIPE: http://bugs.python.org/issue1652
python 中存在与 subprocess.PIPE 相关的细微问题/错误:http: //bugs.python.org/issue1652
Apparently this was fixed in python3+, but not in python 2.7 and older. For that you need to use: code.google.com/p/python-subprocess32/
显然,这在 python3+ 中已修复,但在 python 2.7 及更早版本中未修复。为此,您需要使用:code.google.com/p/python-subprocess32/