Re: subprocess.Popen howto?

Discussion in 'Python' started by norseman, May 7, 2009.

  1. norseman

    norseman Guest

    Ø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 );
    > 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, May 7, 2009
    #1
    1. Advertising

  2. norseman

    Carl Banks Guest

    On May 7, 2:58 pm, norseman <> wrote:
    > 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.



    Hmm, I won't argue that it can be excessive typing, but I think its
    purpose isn't to accomplish the simple but to hit all the corners of
    subprocesses.

    What if you want os.system without the shell? What if you want popen
    with the shell? On Windows what if I want a subprocess without a
    console from a Python program in a console. Stuff like that.

    I still use os.system for Q&D stuff, but now use subprocess for
    everything else since I prefer thorough to short and sweet.


    Carl Banks
     
    Carl Banks, May 8, 2009
    #2
    1. Advertising

  3. norseman

    norseman Guest

    Carl Banks wrote:
    > On May 7, 2:58 pm, norseman <> wrote:
    >> 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.

    >
    >
    > Hmm, I won't argue that it can be excessive typing, but I think its
    > purpose isn't to accomplish the simple but to hit all the corners of
    > subprocesses.
    >
    > What if you want os.system without the shell? What if you want popen
    > with the shell? On Windows what if I want a subprocess without a
    > console from a Python program in a console. Stuff like that.
    >

    Shell? You lost me.
    Why would you even use Windows? Just kidding.
    Build the TSR and Start from Python or the bootup autostart
    and use it from Python or whatever.
    Those in Unix, put the " &" after the program name in the starter
    to get it to background. Be sure to use P_NOWAIT in the starter.

    > I still use os.system for Q&D stuff, but now use subprocess for
    > everything else since I prefer thorough to short and sweet.


    As long as speed (volume vs time) isn't a problem - OK.

    >
    >
    > Carl Banks
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >

    ==================================

    1) That one has access to and can make for ones self choices is
    freedom.
    2) As far as forcing a 'do all' into use is concerned,
    especially at the expense of removing others choices
    (Python notes say subprocess is supplanting popen et al),
    well... that is dictitorial, anti-freedom.
    3) Consider this: a do all is as useful as driving a Ferrari on
    a Jeep Jamboree cross crountry race. Or chasing a Ferrari
    through paved mountain roads with a Jeep. The SUV is
    supposed to be a "do all". Would you actually expect it to
    win either race? After all, they are all "cars".
    Use/create the proper tool for the job. I do not know any good mechanics
    that have a monkey-wrench (a crescent) in their tool box. And all of
    them will tell you that the guy who picks one up and starts toward the
    car may be upset with the driver but he is not a mechanic.
    (Wrong tool of choice. Besides, should not have been available. :)
    (Use the long handle 1/2" socket drive ratchet. Weight, length and
    balance are just right. ;)

    Everybody - Read #1 one more time and make it (speak out loud) "my way".
    If the client is happy, who cares how it was done? But don't force
    others .... (Python admins taking notes?)

    Steve
     
    norseman, May 8, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michele Simionato

    subprocess.Popen

    Michele Simionato, Dec 12, 2004, in forum: Python
    Replies:
    5
    Views:
    17,778
  2. Ivo Woltring
    Replies:
    4
    Views:
    3,621
    Jeff Shannon
    Jan 27, 2005
  3. John Abel
    Replies:
    2
    Views:
    3,454
    John Abel
    May 12, 2005
  4. Earl Eiland
    Replies:
    3
    Views:
    546
    Peter Hansen
    May 16, 2005
  5. File.popen/IO.popen

    , May 20, 2006, in forum: Ruby
    Replies:
    1
    Views:
    272
    Robert Klemme
    May 20, 2006
Loading...

Share This Page