捕获子流程标准输出流

亲爱的大家
我有基于Tkinkter的基于Fortran的程序的前端.我用
作为儿童流程启动Fortran计划的子过程,我希望
要查看Fortran程序的输出,因为它是在
安慰.
Fortran计划最多可以完成20台式,并在
瞬间,我将首先看到Fortran程序之后的任何输出
完毕.如何使我的函数写入该过程的输出
来了?
def runprogram(icommand,ijobfile,ioutput):
如果OS.Name ==" POSIX":
OS.System(PythonPath+"/bin/"+icommand+" <"+ijobfile+" | Tee
"+iOutput)
elif os.name ==" nt":
导入子过程
ofile = open(iOutput,'w')
p = subprocess.popen([[os.path.join(pythonpath," bin",icommand)
+'.exe')],
stdin = open(ijobfile,
" rb"),bufsize = 1024,shell = false,
stdout = subprocess.pipe)
而p.poll()无:#检查子进程是否已终止.
o = p.stdout.readline()
ofile.writelines(o)
打印o,
ofile.close
亲切的问候
托马斯·詹森

# 回答1


在9月8日,8:37*AM,Thomas Jansson 导入线程,队列,子过程
类icommand(螺纹螺纹):
def __init__(self,icommand,ijobfile,队列):
螺纹.__ init __(self)
self.icommand = icommand
self.queue =队列
self.ijobfile = ijobfile
def run(self):
p = subprocess.popen([os.path.join(" c:/python25"," bin",
self.icommand +'.exe']],
stdin = open(self.ijobfile,
" rb"),bufsize = 1024,shell = false,
stdout = subprocess.pipe)
而p.poll()==无:
Q.put(p.stdout.readline())
命令=" foo"
jobfile = =" fah"
aqueue = queue.queue()
fo = open(" c:/temp/foo.out",'w')
athread = icommand(命令,工作文件,水溶液)
athread.start()
而athread.isalive()或not oqueue.empty():
l = aqueue.get().rstrip()
fo.write(l)
打印l
fo.close()
一个不眠之夜的乐趣...
〜肖恩
# 回答2


肖恩·迪扎佐(Sean Dizazzo)写道:
除非您将电话添加到时间.
也整夜保持CPU ...
# 回答3


在9月8日,5:37*PM,Thomas Jansson 有时技术解决方案可能就足够了.例如
Unix
您可以在类似的内容上致电OS.System:
xterm -e" the-fortran-recutable |少"
确保Fortran可执行输出未掩盖.
Michele Simionato

标签: python

添加新评论