使用(wx)python记录外部程序的输出 [英] Logging output of external program with (wx)python

查看:141
本文介绍了使用(wx)python记录外部程序的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用oracle exp/imp命令的GUI,并通过sqlplus启动sql脚本.子流程类使启动命令变得容易,但是我需要一些其他功能.我想在使用wxPython GUI时摆脱命令提示符,但仍然需要一种方法来显示exp/imp命令的输出.

I'm writing a GUI for using the oracle exp/imp commands and starting sql-scripts through sqlplus. The subprocess class makes it easy to launch the commands, but I need some additional functionality. I want to get rid of the command prompt when using my wxPython GUI, but I still need a way to show the output of the exp/imp commands.

我已经尝试了这两种方法:

I already tried these two methods:

command = "exp userid=user/pwd@nsn file=dump.dmp"

process = subprocess.Popen(command, stdout=subprocess.PIPE)
output = process.communicate()[0]

process = subprocess.Popen(command, stdout=subprocess.PIPE)
process.wait()
output = process.stdout.read()

通过这些方法之一(忘了哪一种),我才真正获得了exp/imp的输出,但是仅在命令完成之后,这对我来说是毫无价值的,因为在这些可能长时间运行的操作中,我需要经常进行更新. sqlplus产生了更多问题,因为发生错误时sqlplus大多需要一些输入.发生这种情况时,python等待进程完成,但用户看不到提示,因此您不知道要等待多长时间或该怎么做...

Through one of these methods (forgot which one) I really got the output of exp/imp, but only after the command finishes, which is quite worthless to me, as I need a frequent update during these potentially long running operations. And sqlplus made even more problems, as sqlplus mostly wants some input when an error occurs. When this happens python waits for the process to finish but the user can't see the prompt, so you don't know how long to wait or what to do...

我想要的是一个包装器,该包装器输出我在标准命令行上可以看到的所有内容.我想将其记录到文件中并显示在wxPython控件中.

What I'd like to have is a wrapper that outputs everything I can see on the standard commandline. I want to log this to a file and show it inside a wxPython control.

我还尝试了此页面上的代码: http://code.activestate.com/recipes /440554/ 但这也无法读取输出. 此答案的OutputWrapper也不起作用:如何可以从wxPython应用程序捕获所有异常吗?

I also tried the code from this page: http://code.activestate.com/recipes/440554/ but this can't read the output either. The OutputWrapper from this answer doesn't work either: How can I capture all exceptions from a wxPython application?

任何帮助将不胜感激!


子流程似乎没有刷新其输出.我已经使用.readline()进行了尝试.
我的工具必须在Windows和Unix上运行,因此,如果没有Windows版本,pexpect将无法解决.而且使用cx_oracle将是极端的矫kill过正,因为我将不得不重新构建exp,imp和sqlplus的全部功能.


The subprocesses don't seem to flush their output. I already tried it with .readline().
My Tool has to run on windows and unix, so pexpect is no solution if there's no windows version. And using cx_oracle would be extreme overkill as I would have to rebuild the whole functionality of exp, imp and sqlplus.

推荐答案

如果您使用的是Linux,请查看

If you're on Linux, check out pexpect. It does exactly what you want.

如果您需要在Windows上工作,也许您应该咬紧牙关,并使用与Oracle的Python绑定,例如

If you need to work on Windows, maybe you should bite the bullet and use Python bindings to Oracle, such as cx_Oracle, instead of running CL stuff via subprocess.

这篇关于使用(wx)python记录外部程序的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆