Re: Piping stdout to Python callable

From: Rich Krauter (rmkrauter_at_yahoo.com)
Date: 08/18/04


Date: Wed, 18 Aug 2004 00:49:39 -0400
To: python-list@python.org

Edward Diener wrote:
> Antoon Pardon wrote:
>
>>Op 2004-08-17, Edward Diener schreef <eldiener@earthlink.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("python 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.StringIO()
A.test(5)
fileobj,sys.stdout = 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



Relevant Pages

  • Re: Piping stdout to Python callable
    ... > What do you mean when you say you need to invoke it as a top-level ... Shared libraries may be loaded once in memory but python.exe itself gets ... > to write to stdout? ... stdin on the other end of the pipe. ...
    (comp.lang.python)
  • Re: Exchange store event sink
    ... This is because the .NET GC actually stops all .NET processes ... are new in memory will have a shorter lifetime than objects that are ... you need not worry about Exchange invoking the Dispose() method. ... to invoke the Deactivatemethod. ...
    (microsoft.public.exchange.development)
  • Re: delete []
    ... when invoke delete, the destructor for each instance element of the ... , I malloc 10 bytes, and in the destructor of this class I will ... So the memory is balanced, ...
    (microsoft.public.dotnet.languages.vc)
  • Redirecting STDOUT under ActivePerl on Windows XP
    ... I want to redirect STDOUT to a file, and have the redirection apply to ... any programs that I invoke. ... print "outer calling middle\n"; ...
    (comp.lang.perl.misc)
  • Re: Writing a .txt file
    ... you already know how to output the values to stdout. ... invoke your program like this: ... pointer/size combo. ... my goal ist not to write the array in the file only ...
    (microsoft.public.vc.language)