Start and control an extern program

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

    Start and control an extern program

    Hallo,

    I want to start an extern program and simple stop it, after a
    given time, if not succesfull finished.
    OS is Linux



    Axel


    --
    _______________ _______________ _______________ _______________ _______________
    Axel K"ohler, axel.koehler@tu-berlin.de Tel: 030-314 73141

  • Miki Tebeka

    #2
    Re: Start and control an extern program

    Hello Axel,
    [color=blue]
    > I want to start an extern program and simple stop it, after a
    > given time, if not succesfull finished.
    > OS is Linux[/color]
    Use a Timer object and os.kill.
    Usually when a program exit successfully it's return value will be 0
    (the result of sys.exit for example)

    HTH.
    Miki

    Comment

    • Axel Koehler

      #3
      Re: Start and control an extern program

      Helo Miki,

      thank you for your help.

      Miki Tebeka <miki.tebeka@zo ran.com> writes:
      [color=blue]
      > Hello Axel,
      >[color=green]
      >> I want to start an extern program and simple stop it, after a
      >> given time, if not succesfull finished. OS is Linux[/color]
      > Use a Timer object and os.kill.[/color]

      But what is a timer objekt? Can't find it in my documentation
      of the python library.

      Axel

      --
      _______________ _______________ _______________ _______________ _______________
      Dipl. Ing. Axel K"ohler, axel.koehler@tu-berlin.de Tel: 030-314 73141
      TU Berlin, FAK I, Sekr. FR 3-15, Franklinstr. 28-29 10587 Berlin

      Comment

      • Josiah Carlson

        #4
        Re: Start and control an extern program

        [color=blue]
        > But what is a timer objekt? Can't find it in my documentation
        > of the python library.[/color]

        threading.Timer


        - Josiah

        Comment

        • Axel Koehler

          #5
          Re: Start and control an extern program



          Josiah Carlson <jcarlson@nospa m.uci.edu> writes:
          [color=blue][color=green]
          >> But what is a timer objekt? Can't find it in my documentation of the
          >> python library.[/color]
          >
          > threading.Timer
          > http://www.python.org/doc/current/li...r-objects.html
          >[/color]
          Thank you for the hint,


          but this needs threads, is this really portabel in all versions of
          python under linux?

          Axel

          --
          _______________ _______________ _______________ _______________ _______________
          Dipl. Ing. Axel K"ohler, axel.koehler@tu-berlin.de Tel: 030-314 73141
          TU Berlin, FAK I, Sekr. FR 3-15, Franklinstr. 28-29 10587 Berlin

          Comment

          • Noah

            #6
            Re: Start and control an extern program

            Axel Koehler <axel.koehler@t u-berlin.de> wrote in message news:<kpd67jfdk 4.fsf@rosebud.g p.tu-berlin.de>...[color=blue]
            > Hallo,
            > I want to start an extern program and simple stop it, after a
            > given time, if not succesfull finished.
            > OS is Linux
            > Axel[/color]

            You could try the Pexpect library.
            Download Pexpect - Pure Python Expect-like module for free. Pexpect is a Python module for spawning child applications; controlling them;

            It allows you to start an external program and then wait for a given result
            with a given timeout. Pexpect has a default timeout of 30 seconds, but
            you can change that. The result that you are looking for can be
            any string that you expect in the output. The string can be a
            regular expression pattern. So you could write code like this:

            import pexpect
            child = pexpect.spawn ('my_external_p rogram', timeout=120) # Two minute timeout.
            index = p.expect (['results_you_ar e_looking_for', pexpect.TIMEOUT])
            if index == 0:
            do_something ()
            elif index == 1:
            child.kill (9)
            child.close ()

            Yours,
            Noah

            Comment

            • Peter Hansen

              #7
              Re: Start and control an extern program

              Axel Koehler wrote:
              [color=blue]
              > Josiah Carlson <jcarlson@nospa m.uci.edu> writes:[color=green]
              >>threading.Tim er
              >>http://www.python.org/doc/current/li...r-objects.html[/color]
              >
              > Thank you for the hint,
              >
              > but this needs threads, is this really portabel in all versions of
              > python under linux?[/color]

              It wouldn't be portable to versions of Python that don't have threads,
              one would suppose.

              Have you encountered such? Frequently?

              -Peter

              Comment

              • Josiah Carlson

                #8
                Re: Start and control an extern program

                > It wouldn't be portable to versions of Python that don't have threads,[color=blue]
                > one would suppose.
                >
                > Have you encountered such? Frequently?[/color]

                The only platform I've run into that doesn't support threads is Cygwin
                (a couple years back). I don't know if modern Python + Cygwin supports
                threads, so it may be worth checking.

                On the other hand, every version of Python I've used on linux since mid
                2001 (Python 2.0 on linux kernel 2.4.x) has supported threads.

                - Josiah

                Comment

                Working...