如何压下外部模块的输出? [英] How to depress the output of an external module ?

查看:68
本文介绍了如何压下外部模块的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在编写一个程序,用C语言导入外部模块,

调用模块提供的功能来做我的工作。但是这个方法

产生了很多输出到stdout的输出,这大部分都是在运行时耗尽的。


我的问题是,有没有办法压低

函数产生的输出,从而使我的程序运行得更快?这对我来说太复杂了

修改源代码并重新编译外部模块。


任何提示都将不胜感激。


问候,


xiaojf

解决方案

On Tue ,2006年12月26日15:49:10 +0800, fd ******** @ gmail.com 写道:





我正在编写一个程序,用C和<进口外部模块进行编写br />
调用模块提供的功能来完成我的工作。但是这个方法

产生了很多输出到stdout的输出,这大部分都是在运行时耗尽的。


我的问题是,有没有办法压低

函数产生的输出,从而使我的程序运行得更快?对我来说太复杂了

修改源代码并重新编译外部模块。



尝试这样的事情:


#警告:未经测试

def run_without_stdout(* args ,** kwargs):

function = args [0]

args = args [1:]

savestdout = sys.stdout

sys.stdout = cStringIO.StringIO()

结果=无

尝试:

result = function(* args ,** kwargs)

终于:

#别忘了恢复标准输出,或者你

#真的会后悔.. 。

sys.stdout = savestdout

返回结果


-

史蒂文。


在星期二,26.12.06在21:28,Steven D''Aprano写道:


>

#警告:未经测试

def run_without_stdout(* args,** kwargs):

function = args [0]

args = args [1:]

savestdout = sys.stdout

sys.stdout = cStringIO.StringIO()

结果= N.一个

尝试:

结果=功能(* args,** kwargs)

终于:

#don别忘了恢复标准输出,或者你

#真的会后悔...

sys.stdout = savestdout

返回结果



没有必要使用savestdout。 sys .__ stdout__中有备份副本

-Luis


Steven D''Aprano写道:


尝试这样的事情:


#警告:未经测试

def run_without_stdout(* args,** kwargs):

function = args [0]

args = args [1:]

savestdout = sys.stdout

sys。 stdout = cStringIO.StringIO()

结果=无

尝试:

result = function(* args,** kwargs)

终于:

#别忘了恢复stdout,或者你

#真的会后悔...

sys.stdout = savestdout

返回结果



谢谢!


我试过你的方法,但我发现它没有按预期工作。


外部函数产生的输出不能被抑制,

但是"打印"我在python中写的语句很郁闷。似乎

使cStringIO.StringIO()作为sys.stdout的临时替换

对外部函数没有影响。


这是一个让自己清楚的例子(实际上它是修改后的版本
史蒂文的代码
):


def run_without_stdout(* args,** kwargs):

function = args [0]

args = args [1:]

savestdout = sys.stdout

sys.stdout = cStringIO.StringIO()

打印某事物

结果=无

尝试:

结果=功能(* args,** kwargs)

终于:

#别忘了恢复标准输出,或者你

#真的会后悔...

sys.stdout = savestdout

打印其他一些东西

返回结果


当调用run_without_stdout()时,print将打印出来。在python中写的语句

不产生输出,但是function()产生输出到标准输出

就像以前一样:(


我试图在我的程序中使用cStringIO.StringIO()

全局替换sys.stdout(我的意思是,makesys.stdout = cStringIO.StringIO()作为

globall声明),但它的工作方式与之前的版本一样。

问候,


xiaojf


Hi,

I''m writing a program which imports an external module writing in C and
calls a function provided by the module to do my job. But the method
produces
a lot of output to the stdout, and this consumes most of the running time.

My question is, is there a way to depress the output produced by the
function and hence make my program run faster? It''s too complicated for me
to modify the source code and recompile the external module.

Any hints will be greatly appreciated.

Regards,

xiaojf

解决方案

On Tue, 26 Dec 2006 15:49:10 +0800, fd********@gmail.com wrote:

Hi,

I''m writing a program which imports an external module writing in C and
calls a function provided by the module to do my job. But the method
produces
a lot of output to the stdout, and this consumes most of the running time.

My question is, is there a way to depress the output produced by the
function and hence make my program run faster? It''s too complicated for me
to modify the source code and recompile the external module.

Try something like this:

# WARNING: untested
def run_without_stdout(*args, **kwargs):
function = args[0]
args = args[1:]
savestdout = sys.stdout
sys.stdout = cStringIO.StringIO()
result = None
try:
result = function(*args, **kwargs)
finally:
# don''t forget to restore stdout, or you
# really will regret it...
sys.stdout = savestdout
return result

--
Steven.


On Tuesday, 26.12.06 at 21:28, Steven D''Aprano wrote:

>
# WARNING: untested
def run_without_stdout(*args, **kwargs):
function = args[0]
args = args[1:]
savestdout = sys.stdout
sys.stdout = cStringIO.StringIO()
result = None
try:
result = function(*args, **kwargs)
finally:
# don''t forget to restore stdout, or you
# really will regret it...
sys.stdout = savestdout
return result

There''s no need for savestdout. There''s a backup copy in sys.__stdout__
-Luis


Steven D''Aprano wrote:

Try something like this:

# WARNING: untested
def run_without_stdout(*args, **kwargs):
function = args[0]
args = args[1:]
savestdout = sys.stdout
sys.stdout = cStringIO.StringIO()
result = None
try:
result = function(*args, **kwargs)
finally:
# don''t forget to restore stdout, or you
# really will regret it...
sys.stdout = savestdout
return result

Thanks!

I have tried your method, but I found it didn''t work as expected.

The output produced by the external function couldn''t be depressed,
but the "print " statement i wrote in python is depressed. It seems
make cStringIO.StringIO() as a temporary replacement of sys.stdout
has no effect on the external function.

Here is an example to make myself clear(actually it''s modified version
of Steven''s code):

def run_without_stdout(*args, **kwargs):
function = args[0]
args = args[1:]
savestdout = sys.stdout
sys.stdout = cStringIO.StringIO()
print "something"
result = None
try:
result = function(*args, **kwargs)
finally:
# don''t forget to restore stdout, or you
# really will regret it...
sys.stdout = savestdout
print "some other thing"
return result

When run_without_stdout() is called, the "print" statements wrote in python
don''t produce output, but function() produces output to the standard output
just as before:(

I have tried to replace sys.stdout globally with cStringIO.StringIO()
in my program(I mean, make "sys.stdout = cStringIO.StringIO()" as a
globall statement), but it worked just as previous version did.
Regards,

xiaojf


这篇关于如何压下外部模块的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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