473,837 Members | 1,561 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to depress the output of an external module ?

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

Dec 26 '06 #1
14 1785
On Tue, 26 Dec 2006 15:49:10 +0800, fd********@gmai l.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_std out(*args, **kwargs):
function = args[0]
args = args[1:]
savestdout = sys.stdout
sys.stdout = cStringIO.Strin gIO()
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.

Dec 26 '06 #2
On Tuesday, 26.12.06 at 21:28, Steven D'Aprano wrote:
>
# WARNING: untested
def run_without_std out(*args, **kwargs):
function = args[0]
args = args[1:]
savestdout = sys.stdout
sys.stdout = cStringIO.Strin gIO()
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
Dec 26 '06 #3
Steven D'Aprano wrote:
Try something like this:

# WARNING: untested
def run_without_std out(*args, **kwargs):
function = args[0]
args = args[1:]
savestdout = sys.stdout
sys.stdout = cStringIO.Strin gIO()
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.Strin gIO() 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_std out(*args, **kwargs):
function = args[0]
args = args[1:]
savestdout = sys.stdout
sys.stdout = cStringIO.Strin gIO()
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_std out() 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.Strin gIO()
in my program(I mean, make "sys.stdout = cStringIO.Strin gIO()" as a
globall statement), but it worked just as previous version did.
Regards,

xiaojf
Dec 26 '06 #4

fd********@gmai l.com wrote:
Steven D'Aprano wrote:
Try something like this:

# WARNING: untested
def run_without_std out(*args, **kwargs):
function = args[0]
args = args[1:]
savestdout = sys.stdout
sys.stdout = cStringIO.Strin gIO()
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.Strin gIO() 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_std out(*args, **kwargs):
function = args[0]
args = args[1:]
savestdout = sys.stdout
sys.stdout = cStringIO.Strin gIO()
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_std out() 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.Strin gIO()
in my program(I mean, make "sys.stdout = cStringIO.Strin gIO()" as a
globall statement), but it worked just as previous version did.
Perhaps try redirecting sys.stderr instead of sys.stdout.

André

Regards,

xiaojf
Dec 26 '06 #5
fd********@gmai l.com wrote:
>
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.Strin gIO() 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_std out(*args, **kwargs):
function = args[0]
args = args[1:]
savestdout = sys.stdout
sys.stdout = cStringIO.Strin gIO()
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_std out() 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.Strin gIO()
in my program(I mean, make "sys.stdout = cStringIO.Strin gIO()" as a
globall statement), but it worked just as previous version did.
After some trials I found that put "os.close(1 )" before calling the
function will depress the output. In fact, "os.close(1 )" closed
standard output, but I don't know how to open it again after the function's
execution.

Still trying...

Regards,

xiaojf
Dec 26 '06 #6
Hi Luis,

Luis Armendariz wrote:
There's no need for savestdout. There's a backup copy in sys.__stdout__
Depending on the code that ran before the call to the
function run_without_std out, sys.stdout may not be
the same as sys.__stdout__ . Of course, you also have
to be careful regarding threads which also use
sys.stdout, perhaps implicitly via print statements.

Stefan
Dec 26 '06 #7
fd********@gmai l.com <fd********@gma il.comwrote
fd********@gmai l.com wrote:
>>
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.Strin gIO() 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_std out(*args, **kwargs):
function = args[0]
args = args[1:]
savestdout = sys.stdout
sys.stdout = cStringIO.Strin gIO()
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_std out() 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.Strin gIO()
in my program(I mean, make "sys.stdout = cStringIO.Strin gIO()" as a
globall statement), but it worked just as previous version did.
After some trials I found that put "os.close(1 )" before calling the
function will depress the output. In fact, "os.close(1 )" closed
standard output, but I don't know how to open it again after the
function's execution.

Still trying...
On Linux systems you may try os.open('/dev/stdout', os.O_WRONLY'). This
will connect to lowest available file descriptor to standard output. If
you're lucky and no files have been opened after closing standard
output, sys.stdout will point to standard output again.

Bye
Sebastian 'lunar' Wiesner

--
Freedom is always the freedom of dissenters.
(Rosa Luxemburg)
Dec 26 '06 #8
On Tue, 26 Dec 2006 03:21:57 -0800, Luis Armendariz wrote:
On Tuesday, 26.12.06 at 21:28, Steven D'Aprano wrote:
>>
# WARNING: untested
def run_without_std out(*args, **kwargs):
function = args[0]
args = args[1:]
savestdout = sys.stdout
sys.stdout = cStringIO.Strin gIO()
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__
You shouldn't assume that sys.stdout is standard output when the function
is called. It may already have been reassigned to something else. My code
will restore sys.stdout to whatever state it was in before the function
ran.

--
Steven.

Dec 27 '06 #9
fd********@gmai l.com wrote:
After some trials I found that put "os.close(1 )" before calling the
function will depress the output. In fact, "os.close(1 )" closed
standard output, but I don't know how to open it again after the function's
execution.
Try this:

fd = os.dup(1)
os.close(1)
sys.stdout = os.fdopen(fd)

It's poor design and extremely inconsiderate for a library to print to
stdout except as an overridable default. (Not to mention it gives
ammo to people who want dynamically scoped globals.) A long term
solution might be to haggle the author to stop being such a jerk.

Carl Banks

Dec 27 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3538
by: Fernando Rodriguez | last post by:
Hi, How can I save the output of an external program, called with os.system()?
2
1665
by: PWR | last post by:
Platform: Windows Python: 2.3 Python Skill: Reading, Learning, but still newbie. Mission: My mission is writing a script that is triggered from my mail server. The script will execute and download a file. The email that triggered the message has a file size which is compared to the downloaded file to make sure the bytes match. Once that is accomplished there is a small program (.exe) that I can run against the file. The ..exe has...
4
2451
by: Avi Kak | last post by:
Is there a Python function in any of the standard-distribution modules that does what the backticks do in Perl? I want to run an external command and I'd like its output to be captured directly in my Python script in the form of a string object. The function os.system() or any of the
3
1836
by: Yuan HOng | last post by:
In my program I have to call an external program and parse its output. For that I use the os.popen2 function, and then read the output stream. But the complexity is that the external program gives back its output in a piecemeal manner, with long delays between the outputs. In the main program I want to therefore read the output in a non-blocking manner, to read as many bytes as the child process is spitting out. The question is, how...
2
1513
by: James Colannino | last post by:
Hey everyone. First off, I'm new to the list. I had had a little bit of experience with Perl before discovering Python. The more Python I learn, the more I love it :) I just have a quick question to ask. I know that this is probably a simple question, but I've been googling around, and partly because I'm not sure exactly what to search for, I've been unsuccessful at finding an answer. What I'd like to do is be able to take the output...
1
2003
by: S. van Beek | last post by:
Dear reader, In case an application is ussing an external model library you have to address this external model library in your application. This addressing takes place in a module of the application by opening of a module and activates and than brows to the location of the external library.
5
3336
by: amit.uttam | last post by:
Hey everyone, I've recently jumped big time into python and I'm working on a software program for testing automation. I had a question about proper logging of output. What I would like is: 1. a function syslog (to log output to log file only) 2. a function stdout (to log output to stdout only) 3. a function sslog (to log output to both log and stdout)
6
2948
by: drhilbert | last post by:
Dear all, in these days, we have found a problem we can't solve even after long long googling, so we are here asking your precious help. In our program instead of using cout to print messages we implemented a custom ostream able to do the following: * Allow the user to choose where to send the messages: on the stdout, in to a file or to any other ostream
0
1480
by: ravikim1 | last post by:
Hi , use Sort::External; $output = '1.txt'; open(FILE, $output); my $sortex = Sort::External->new( mem_threshold => 1024**2 * 1024 ); while (<FILE>) { $sortex->feed($_); # tried to change it to ~$_ but did not work !!!!! }
0
10883
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10578
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10631
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10276
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9409
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7813
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7006
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
4049
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3126
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.