subprocess.Popen howto?
norseman
norseman at hughes.net
Fri May 8 12:09:01 EDT 2009
Øystein ;
Down below: change >yourPy.py to | yourPy.py
I just noticed the typo.
Sorry
Steve
norseman wrote:
> Øystein Johansen (OJOHANS) wrote:
>> Hi,
>>
>> I have problems understanding the subprocess.Popen object. I have a
>> iterative calculation in a process running and I want to pipe the
>> output (stdout) from this calculation to a Python script.
>>
>> Let me include a simple code that simulates the calculating process:
>> /* This code simulates a big iterative calculation */
>> #include <stdio.h>
>> #include <math.h>
>>
>> int main()
>> {
>> float val[2] = { M_PI, M_E };
>> int i;
>>
>> for ( i = 0; i < 2 i++) {
>> sleep( 15 ); /* It's a hard calculation. It take 15 seconds */
>> printf("Value: %5.6f\n", val[i] );
>> fflush( stdout );
>> }
>> return 0;
>> }
>>
>> let's compile this to mycalc: gcc -o mycalc calc.c ... (untested code)
>>
>> In C I have this code which starts the mycalc process and handles the
>> output from it:
>>
>> #include <stdio.h>
>> #include <assert.h>
>> #define BUF_SIZE 256
>>
>> int main()
>> {
>> FILE *pip;
>> char line[BUF_SIZE];
>>
>> pip = popen("mycalc", "r");
>> assert( pip != NULL );
>>
>> while ( fgets( line, BUF_SIZE, pip )) {
>> printf( "Hello; I got: %s \n", line );
>> fflush( stdout );
>> }
>> pclose( pip );
>> return 0;
>> }
>> How can I make such while-loop in Python? I assume I should use
>> subprocess.Popen(), but I can't figure out how?
>>
>> -Øystein
>>
>>
>>
>> -------------------------------------------------------------------
>> The information contained in this message may be CONFIDENTIAL and is
>> intended for the addressee only. Any unauthorised use, dissemination
>> of the
>> information or copying of this message is prohibited. If you are not the
>> addressee, please notify the sender immediately by return e-mail and
>> delete
>> this message.
>> Thank you.
>>
>>
>> ------------------------------------------------------------------------
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
> ================================
>
> If you don't like a lot of typing that obscures the process,
> take a look at os.Popen2 Pg.39 or so in Lib.pdf for 2.5.2
> In this case - the popen3 is probably your best bet.
>
> I took a test run on "subprocess" a few months ago. My review:
> excessive typing to accomplish the simple. BlackBox stuff is supposed
> to be short and sweet.
>
> BlackBox was the term applied to drivers and couplers back when.
> Back when: one started by writing a subroutine that got used.
> next one got to write a program
> next one got to write an application
> then a BlackBox
> from there one could graduate to OS
> That was back when!
>
>
> to repeat from another of my postings:
> ---
> processPython 2.5.2 (r252:60911, Mar 4 2008, 10:40:55)
> [GCC 3.3.6] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>
> >>> import os
> >>> AbWd = os.spawnlp( os.P_WAIT,"abiword","abiword","")
>
> The P_WAIT stops python until the program (abiword in this case)
> completes. The "" at the end are for tokens to be given to the program
> and yes - contrary to manual, the program MUST be there TWICE (complete
> with any path needed).
>
> for windows this works:
> (for cut and paste from cmd.exe, see:
> Re: Copy & Paste in a Dos box from norseman 05/06/2009 4:28PM
> )
>
> Python 2.5.1 ... on win32
> >>> import os
> >>> result = os.spawnl( os.P_WAIT, "d:\\winmcad\\mcad","")
>
> Runs the program mcad. Returns to python when mcad exits.
> ---
>
>
> In your case: substitute ..."your_compiled_c_program", " >yourPy.py")
> at the obvious places.
>
> In case this isn't clear;
> method 1: py1.py starts compiled.c which redirects to yourPy.py
> method 2: py1.py uses os.popen3(compiled.c...) & the two work it out.
> In either case the receiver at the end stays open until completion so
> sleep() things are not needed. (may need to test for EOL and EOT)
>
> HTH
>
> Steve
> norseman at hughes.net
> --
> http://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list