473,803 Members | 3,410 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Piping stdout to Python callable

From within a function in my own module I need to take the output from a
Python module "A", which is sending data to stdout and which can not be
changed and which I need to invoke as a top-level module, and pipe it into
another function in my own module so that I can read it from stdin. Is there
an easy way to do this ? The only way I can presently think to do this is
through "system python A.py | python MyOwnModule.py" , which seems a bit
laborious having to invoke python.exe itself twice. Any other solution would
be most welcome.
Jul 18 '05 #1
7 2764
Op 2004-08-17, Edward Diener schreef <el******@earth link.net>:
From within a function in my own module I need to take the output from a
Python module "A", which is sending data to stdout and which can not be
changed and which I need to invoke as a top-level module, and pipe it into
another function in my own module so that I can read it from stdin. Is there
an easy way to do this ? The only way I can presently think to do this is
through "system python A.py | python MyOwnModule.py" , which seems a bit
laborious having to invoke python.exe itself twice. Any other solution would
be most welcome.


What do you mean when you say you need to invoke it as a top-level
module? Do you mean you can't import it at all or that importing it will
startup the process of generating output immediatly?

What bothers you with twice invoking the interpreter? In these days
a program that is invoked multiples times will generally be only
loaded once in memory.

Are threads an acceptable alternative? Does your MyOwnModule.py needs
to write to stdout?

--
Antoon Pardon
Jul 18 '05 #2
Antoon Pardon wrote:
Op 2004-08-17, Edward Diener schreef <el******@earth link.net>:
From within a function in my own module I need to take the output
from a Python module "A", which is sending data to stdout and which
can not be changed and which I need to invoke as a top-level module,
and pipe it into another function in my own module so that I can
read it from stdin. Is there an easy way to do this ? The only way I
can presently think to do this is through "system python A.py |
python MyOwnModule.py" , which seems a bit laborious having to invoke
python.exe itself twice. Any other solution would be most welcome.
What do you mean when you say you need to invoke it as a top-level
module? Do you mean you can't import it at all or that importing it
will startup the process of generating output immediatly?


I mean that it has a "if __name__ == '__main__' line and I need to trigger
it by calling 'python A.py'.

What bothers you with twice invoking the interpreter?
Nothing practically. Just seems inelegant.
In these days
a program that is invoked multiples times will generally be only
loaded once in memory.
Shared libraries may be loaded once in memory but python.exe itself gets
reloaded each time.

Are threads an acceptable alternative? Does your MyOwnModule.py needs
to write to stdout?


Threads are acceptable. MyOwnModule.py can do anything, depending on
parameters, but the idea is that a certain parameter tells it to read from
stdin on the other end of the pipe.

I have implemented this via "os.system("pyt hon A.py | python MyOwnModule.py
parameters") and it works fine. I thought there might be a better, more
elegant way but since the above works without any problem I will stick to it
unless you can suggest anything better. BTW "A.py" can be any number of
types of modules which must be invoked by python.exe and writes it's results
to stdout. I am telling you this lest you suggest that I somehow import and
parse A.py in order to call directly into it rather than having python.exe
invoke it. While that might be possible, it would be a real programming
PITA.
Jul 18 '05 #3
Edward Diener wrote:
Antoon Pardon wrote:
Op 2004-08-17, Edward Diener schreef <el******@earth link.net>:
From within a function in my own module I need to take the output
from a Python module "A", which is sending data to stdout and which
can not be changed and which I need to invoke as a top-level module,
and pipe it into another function in my own module so that I can
read it from stdin. Is there an easy way to do this ? The only way I
can presently think to do this is through "system python A.py |
python MyOwnModule.py" , which seems a bit laborious having to invoke
python.exe itself twice. Any other solution would be most welcome.


What do you mean when you say you need to invoke it as a top-level
module? Do you mean you can't import it at all or that importing it
will startup the process of generating output immediatly?

I mean that it has a "if __name__ == '__main__' line and I need to trigger
it by calling 'python A.py'.

What bothers you with twice invoking the interpreter?

Nothing practically. Just seems inelegant.

In these days
a program that is invoked multiples times will generally be only
loaded once in memory.

Shared libraries may be loaded once in memory but python.exe itself gets
reloaded each time.

Are threads an acceptable alternative? Does your MyOwnModule.py needs
to write to stdout?

Threads are acceptable. MyOwnModule.py can do anything, depending on
parameters, but the idea is that a certain parameter tells it to read from
stdin on the other end of the pipe.

I have implemented this via "os.system("pyt hon A.py | python MyOwnModule.py
parameters") and it works fine. I thought there might be a better, more
elegant way but since the above works without any problem I will stick to it
unless you can suggest anything better. BTW "A.py" can be any number of
types of modules which must be invoked by python.exe and writes it's results
to stdout. I am telling you this lest you suggest that I somehow import and
parse A.py in order to call directly into it rather than having python.exe
invoke it. While that might be possible, it would be a real programming
PITA.


You could do something like this:

$ cat A.py
def test(arg):
for i in range(1,5):
print arg*i

$ cat B.py
import A
import sys
import StringIO

sys.stdout = StringIO.String IO()
A.test(5)
fileobj,sys.std out = sys.stdout,sys. __stdout__

fileobj.seek(0)
for line in fileobj:
print line.strip()
Worth the effort to change your code? Probably not. Kinda neat, though.

Rich
Jul 18 '05 #4
Rich Krauter wrote:
[silly post snipped]


Oh, duh. You you said you need to invoke the code in the
if __name__ == '__main__': part of the code. Sorry, I saw your post
yesterday and didn't register that then. Obviously my suggestion doesn't
help you.

Well, unless A.py looks something like

def main():
...
...

if __name__ == '__main__:
main()
Rich

Jul 18 '05 #5
Op 2004-08-18, Edward Diener schreef <el******@earth link.net>:
Antoon Pardon wrote:
Op 2004-08-17, Edward Diener schreef <el******@earth link.net>:
From within a function in my own module I need to take the output
from a Python module "A", which is sending data to stdout and which
can not be changed and which I need to invoke as a top-level module,
and pipe it into another function in my own module so that I can
read it from stdin. Is there an easy way to do this ? The only way I
can presently think to do this is through "system python A.py |
python MyOwnModule.py" , which seems a bit laborious having to invoke
python.exe itself twice. Any other solution would be most welcome.


What do you mean when you say you need to invoke it as a top-level
module? Do you mean you can't import it at all or that importing it
will startup the process of generating output immediatly?


I mean that it has a "if __name__ == '__main__' line and I need to trigger
it by calling 'python A.py'.


What bothers you with twice invoking the interpreter?


Nothing practically. Just seems inelegant.
In these days
a program that is invoked multiples times will generally be only
loaded once in memory.


Shared libraries may be loaded once in memory but python.exe itself gets
reloaded each time.


That depends on the O.S. It is possible the O.S. notices that an
invoked program is already loaded en uses the same code-segment
in memory for following invocations.
Are threads an acceptable alternative? Does your MyOwnModule.py needs
to write to stdout?


Threads are acceptable. MyOwnModule.py can do anything, depending on
parameters, but the idea is that a certain parameter tells it to read from
stdin on the other end of the pipe.


Then I fear there is no other solution. As far as I understand, you
can't redirect standard output on a per thread basis, (at least not
in python.) That means that if you redirect stdout of the A.py module
to go into a pipe, the stdout of MyOwnModule.py will go into the
same pipe. I don't think you want that.

--
Antoon Pardon
Jul 18 '05 #6
Antoon Pardon wrote:
Op 2004-08-18, Edward Diener schreef <el******@earth link.net>:
Antoon Pardon wrote:
Op 2004-08-17, Edward Diener schreef <el******@earth link.net>:
From within a function in my own module I need to take the output
from a Python module "A", which is sending data to stdout and which
can not be changed and which I need to invoke as a top-level
module, and pipe it into another function in my own module so that
I can read it from stdin. Is there an easy way to do this ? The
only way I can presently think to do this is through "system
python A.py | python MyOwnModule.py" , which seems a bit laborious
having to invoke python.exe itself twice. Any other solution would
be most welcome.

What do you mean when you say you need to invoke it as a top-level
module? Do you mean you can't import it at all or that importing it
will startup the process of generating output immediatly?


I mean that it has a "if __name__ == '__main__' line and I need to
trigger it by calling 'python A.py'.


What bothers you with twice invoking the interpreter?


Nothing practically. Just seems inelegant.
In these days
a program that is invoked multiples times will generally be only
loaded once in memory.


Shared libraries may be loaded once in memory but python.exe itself
gets reloaded each time.


That depends on the O.S. It is possible the O.S. notices that an
invoked program is already loaded en uses the same code-segment
in memory for following invocations.
Are threads an acceptable alternative? Does your MyOwnModule.py
needs to write to stdout?


Threads are acceptable. MyOwnModule.py can do anything, depending on
parameters, but the idea is that a certain parameter tells it to
read from stdin on the other end of the pipe.


Then I fear there is no other solution. As far as I understand, you
can't redirect standard output on a per thread basis, (at least not
in python.) That means that if you redirect stdout of the A.py module
to go into a pipe, the stdout of MyOwnModule.py will go into the
same pipe. I don't think you want that.


Just wanted to write that someone else pointed out to me os.popen("pytho n
A.py") and that did work well as an alternative to using os.system(etc.) .
Nonetheless thanks for your help. I mentioned this so that you would know
also know about it yourself if you hadn't encountered it.
Jul 18 '05 #7
Op 2004-08-19, Edward Diener schreef <el******@earth link.net>:
Antoon Pardon wrote:

Then I fear there is no other solution. As far as I understand, you
can't redirect standard output on a per thread basis, (at least not
in python.) That means that if you redirect stdout of the A.py module
to go into a pipe, the stdout of MyOwnModule.py will go into the
same pipe. I don't think you want that.


Just wanted to write that someone else pointed out to me os.popen("pytho n
A.py") and that did work well as an alternative to using os.system(etc.) .
Nonetheless thanks for your help. I mentioned this so that you would know
also know about it yourself if you hadn't encountered it.


I know about popen, but that didn't seem to resolve your concern.
Working with popen will result in launching a second interpreter.
Since one of your mayor concerns about your own solution was
having two interpreters running, I didn't thought popen was a
viable solution.

--
Antoon Pardon
Jul 18 '05 #8

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

Similar topics

4
9928
by: Christian Long | last post by:
Hi I'm trying to pipe data into a python program on Windows 2000, on the command line. Like this: dir | myProgram.py Here's what I tried:
2
2676
by: Csaba Henk | last post by:
For me, one reason for using Python is that with the help of it I can rid of several problems that I faced with during writing scripts in Bourneish shells. Biggest of these problem was the quotation madness stemming from the typelessness of the shell language. When using Python as a shell replacement I often launch apps from Python and use different piping constructs to pass data to/get data from these apps. I expect from the language...
2
1876
by: Apple Grew | last post by:
I want to develope a Winboard like application which will support Winboard protocol. For that I require to know how to handle piping in Python. I seperated out module popen2, but when I use fileObject.readline() then it halts the program if the sub-process is waiting for user input; will executing the readline() part in a seperate thread be helpful? My target platform is Windows. Links to helpful web resources and sample Python codes...
6
2422
by: mhenry1384 | last post by:
On WinXP, I am doing this nant.exe | python MyFilter.py This command always returns 0 (success) because MyFilter.py always succeeds. MyFilter.py looks like this while 1:
22
1771
by: lechequier | last post by:
Let's say I define a list of pairs as follows: >>l = Can anyone explain why this does not work? >>h = {}.update(l) and instead I have to go: >>h = {} >>h.update(l) to initialize a dictionary with the given list of pairs?
9
2004
by: Andy Leszczynski | last post by:
I have got following program: import sys import binascii from string import * sys.stdout.write(binascii.unhexlify("41410A4141")) when I run under Unix I got:
112
13876
by: mystilleef | last post by:
Hello, What is the Pythonic way of implementing getters and setters. I've heard people say the use of accessors is not Pythonic. But why? And what is the alternative? I refrain from using them because they smell "Javaish." But now my code base is expanding and I'm beginning to appreciate the wisdom behind them. I welcome example code and illustrations.
1
2694
by: James McGill | last post by:
Hi All, I'm using subprocess.Popen to run a C++ compiler and have set stdout = PIPE. The exact line of code that I am using is: process = Popen(command, stdout=PIPE) status = process.wait() This works fine until a large amount of data is written to stdout. When this occurs, my python program seems to freeze. It will no longer
0
9700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9564
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10292
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
10068
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
9121
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
7603
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...
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
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.