如何重定向子进程的标准输出?
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
我想做的是仅启动一次可执行文件,但在每次迭代中我想将进程输出重定向到不同的文件。发生的情况是,文件是在不同的路径中创建的,但输出被重定向到第一次创建的文件。
注意:MyReset()
在第一次迭代后将进程(可执行文件)初始化为其默认值。
以下行会将进程标准输出更改为新文件吗?
proc.stdout = proc_log
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.
Will the following line change the process stdout to new file?
proc.stdout = proc_log
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就像 unwind 所说的那样,您无法更改子进程将其输出写入其中的文件描述符。
您可以做的是读取 python 脚本中子进程的输出,然后将其写回到您想要的任何文件中。例如:
显然,您需要弄清楚控制在您的应用程序中应如何表现,即您可以从主线程进行写入吗?在这种情况下主线程何时应从
StartProc()
返回,或者您是否必须从另一个线程进行写入,以便主线程可以立即从 StartProc() 返回。Like unwind said, you cannot change the file descriptor that the child process is writing its output to.
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.:
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 fromStartProc()
immediately.你不能。一旦进程启动,它的标准输出就无法从外部更改。您需要进入该进程的空间来处理其文件描述符。
如果您有一种方法(我猜是在 MyReset() 中)与正在运行的进程对话,也许您可以设计一种方法来传递新文件名,以便将其(重新)打开作为其标准输出,即方式。
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.python 中存在与 subprocess.PIPE 相关的微妙问题/错误:
http://bugs.python.org/issue1652
显然这是在 python3+ 中修复的,但在 python 2.7 中没有修复和年纪大了。为此,您需要使用:
code.google.com/p/python-subprocess32/
There are subtle problems/bugs in python related to 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/