Subprocess or Process or OMG!!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rh0dium

    #1

    Subprocess or Process or OMG!!

    Hi all,

    Here I was happily coming to working thinking - OK I need to create a
    wrapper for a tool (UNIX) which does nothing but lauch the end tool and
    send a sql instert letting the db know the tool was launched (Can we
    say Big Brother..). Some of the tools are very long running with lots
    of data others are small and very fast.. So I started down my
    traditional approach - Check google groups for the answer. I quickly
    became aware of how challenging this actually is. See my approach was
    simple

    Tool name = FOOBAR
    Symbolically link FOOBAR to my Wrapper.py

    1. Get the args and some other key data (user, cwd, etc)
    2. Fork this process and get FOOBAR running (time is of essense of
    course)
    3. On the side submit a mySQL call saying what happened...

    Sounds simple and I thought I was ready to go.. Until google groups...



    OK so now I need to think about I/O redirection hmmm OK More data..
    People have dealt with this - but most of it's dated..

    Wait import subprocess.. Looks promising hey some has really used
    this..





    Needless to say this is much more complex than what I was "hoping" for.
    Has anyone solved this in a generic approach..

    #!/usr/bin/env python

    import os,sys,string
    import subprocess

    argc = len(sys.argv)

    # Yes I move all exe's to the .bin direcotory
    sys.argv[0]=os.path.join(o s.getcwd() + "/.bin/" +
    os.path.basenam e(sys.argv[0]))

    cmd = string.join(sys .argv," ")
    subprocess.Pope n( cmd )

    This has several problems - least of which args aren't working.... Has
    anyone really tried this approach?

  • Robert Kern

    #2
    Re: Subprocess or Process or OMG!!

    rh0dium wrote:
    [color=blue]
    > Needless to say this is much more complex than what I was "hoping" for.
    > Has anyone solved this in a generic approach..
    >
    > #!/usr/bin/env python
    >
    > import os,sys,string
    > import subprocess
    >
    > argc = len(sys.argv)
    >
    > # Yes I move all exe's to the .bin direcotory
    > sys.argv[0]=os.path.join(o s.getcwd() + "/.bin/" +
    > os.path.basenam e(sys.argv[0]))
    >
    > cmd = string.join(sys .argv," ")
    > subprocess.Pope n( cmd )
    >
    > This has several problems - least of which args aren't working.... Has
    > anyone really tried this approach?[/color]

    Did you read the documentation? It explains how to pass the command line (hint:
    it's a list, not a string).

    Source code: Lib/subprocess.py The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace seve...


    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco

    Comment

    • blair.bethwaite@gmail.com

      #3
      Re: Subprocess or Process or OMG!!

      rh0dium wrote:[color=blue]
      > This has several problems - least of which args aren't working.... Has
      > anyone really tried this approach?[/color]

      No, they just wrote the code for the hell of it. :)

      Seriously though, you may want to consider using the popen2 module.
      Then you'll be able to wait on the subprocess to return and check the
      return code, plus you could do your DB update first then go into a
      wait() to check for success.

      -B

      Comment

      • rh0dium

        #4
        Re: Subprocess or Process or OMG!!

        ROTFLMAO - Thanks --- I "meant" has anyone done the wrapper approach
        before!! Not has anyone used the code.. hahah nice!!

        subprocess also has the wait object. I think I like this better than
        popen2 (which I have used in the past) for a number of reasons but
        primarily it's simplicity. Having said that, I don't think I want to
        use the wait object because I am afraid the db query in some cases may
        take longer than the command.. That would leave the user wondering
        what's going on.. Only testing will tell though..

        Thanks Robert - yeah I checked the docs and figured out that it takes a
        list shortly after I sent the post.

        Comment

        Working...