Piping stdout to Python callable

Antoon Pardon apardon at forel.vub.ac.be
Wed Aug 18 09:36:58 CEST 2004


Op 2004-08-18, Edward Diener schreef <eldiener at earthlink.net>:
> Antoon Pardon wrote:
>> Op 2004-08-17, Edward Diener schreef <eldiener at 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.

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



More information about the Python-list mailing list