subprocess seems to "detach" / ignore wait()

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

    subprocess seems to "detach" / ignore wait()

    Hi there,

    it seems that child.wait() is ignored when

    print "Server running [PID %s]"%(child.pi d)
    fpid.write(chil d.pid)

    are between the process creation child = Popen(cmd.split (),
    stderr=flog) and child.wait().
    It seems to be a bug, doesn't it ?

    Mathieu

    (I'm running x11vnv with args in the cmd string on FreeBSD 8.0/CURRENT)

    flog = open(logfile, 'w')
    fpid = open(pidfile, 'w')
    try:
    child = Popen(cmd.split (), stderr=flog)
    print "Server running [PID %s]"%(child.pi d)
    fpid.write(chil d.pid)
    child.wait()
    except KeyboardInterru pt:
    print "INT sent to vnc server"
    finally:
    fpid.close()
    flog.close()
    os.remove(pidfi le)
    os.remove(logfi le)
    sys.exit(0)
  • Wojtek Walczak

    #2
    Re: subprocess seems to "detach&qu ot; / ignore wait()

    On Wed, 20 Aug 2008 15:09:11 +0200, Mathieu Prevot wrote:
    flog = open(logfile, 'w')
    fpid = open(pidfile, 'w')
    try:
    child = Popen(cmd.split (), stderr=flog)
    print "Server running [PID %s]"%(child.pi d)
    fpid.write(chil d.pid)
    What happens if you change:

    fpid.write(chil d.pid)

    into:

    fpid.write('%d\ n' % (child.pid))

    I think that the problem here is that fpid.write() fails silently
    (probably TypeError), because it takes string as its first argument,
    not integer.

    --
    Regards,
    Wojtek Walczak,
    Cena domeny: 4999 PLN (do negocjacji). Możliwość kupna na raty od 624.88 PLN miesięcznie. Oferta sprzedaży znajduje się w serwisie Aftermarket.pl, największej giełdzie domen internetowych w Polsce.

    Comment

    • Gabriel Genellina

      #3
      Re: subprocess seems to "detach&qu ot; / ignore wait()

      En Wed, 20 Aug 2008 12:22:16 -0300, Wojtek Walczak <gminick@bzt.bz tescribió:
      On Wed, 20 Aug 2008 15:09:11 +0200, Mathieu Prevot wrote:
      >
      > child = Popen(cmd.split (), stderr=flog)
      > print "Server running [PID %s]"%(child.pi d)
      > fpid.write(chil d.pid)
      >
      I think that the problem here is that fpid.write() fails silently
      (probably TypeError), because it takes string as its first argument,
      not integer.
      Exactly, but it doesn't fail "silently" (that would be a bug). The exception is raised, but due to the finally clause ending in sys.exit(0), it has no chance of being handled.
      This is the original code, for reference:

      flog = open(logfile, 'w')
      fpid = open(pidfile, 'w')
      try:
      child = Popen(cmd.split (), stderr=flog)
      print "Server running [PID %s]"%(child.pi d)
      fpid.write(chil d.pid)
      child.wait()
      except KeyboardInterru pt:
      print "INT sent to vnc server"
      finally:
      fpid.close()
      flog.close()
      os.remove(pidfi le)
      os.remove(logfi le)
      sys.exit(0)

      --
      Gabriel Genellina

      Comment

      • Mathieu Prevot

        #4
        Re: subprocess seems to &quot;detach&qu ot; / ignore wait()

        2008/8/20 Gabriel Genellina <gagsl-py2@yahoo.com.a r>:
        En Wed, 20 Aug 2008 12:22:16 -0300, Wojtek Walczak <gminick@bzt.bz tescribió:
        >
        >On Wed, 20 Aug 2008 15:09:11 +0200, Mathieu Prevot wrote:
        >>
        >> child = Popen(cmd.split (), stderr=flog)
        >> print "Server running [PID %s]"%(child.pi d)
        >> fpid.write(chil d.pid)
        >>
        >I think that the problem here is that fpid.write() fails silently
        >(probably TypeError), because it takes string as its first argument,
        >not integer.
        >
        Exactly, but it doesn't fail "silently" (that would be a bug). The exception is raised, but due to the finally clause ending in sys.exit(0), it has no chance of being handled.
        This is the original code, for reference:
        >
        flog = open(logfile, 'w')
        fpid = open(pidfile, 'w')
        try:
        child = Popen(cmd.split (), stderr=flog)
        print "Server running [PID %s]"%(child.pi d)
        fpid.write(chil d.pid)
        child.wait()
        except KeyboardInterru pt:
        print "INT sent to vnc server"
        finally:
        fpid.close()
        flog.close()
        os.remove(pidfi le)
        os.remove(logfi le)
        sys.exit(0)
        >
        --
        Gabriel Genellina

        Indeed, I got TypeError: argument 1 must be string or read-only
        character buffer, not int
        and Wojtek's code works. So what is the right thing to do so my script
        returns 1 or 0 depending on its state and success ?

        Mathieu

        Comment

        • Mathieu Prevot

          #5
          Re: subprocess seems to &quot;detach&qu ot; / ignore wait()

          2008/8/21 Mathieu Prevot <mathieu.prevot @ens.fr>:
          2008/8/20 Gabriel Genellina <gagsl-py2@yahoo.com.a r>:
          >En Wed, 20 Aug 2008 12:22:16 -0300, Wojtek Walczak <gminick@bzt.bz tescribió:
          >>
          >>On Wed, 20 Aug 2008 15:09:11 +0200, Mathieu Prevot wrote:
          >>>
          >>> child = Popen(cmd.split (), stderr=flog)
          >>> print "Server running [PID %s]"%(child.pi d)
          >>> fpid.write(chil d.pid)
          >>>
          >>I think that the problem here is that fpid.write() fails silently
          >>(probably TypeError), because it takes string as its first argument,
          >>not integer.
          >>
          >Exactly, but it doesn't fail "silently" (that would be a bug). The exception is raised, but due to the finally clause ending in sys.exit(0), it hasno chance of being handled.
          >This is the original code, for reference:
          >>
          >flog = open(logfile, 'w')
          >fpid = open(pidfile, 'w')
          >try:
          > child = Popen(cmd.split (), stderr=flog)
          > print "Server running [PID %s]"%(child.pi d)
          > fpid.write(chil d.pid)
          > child.wait()
          >except KeyboardInterru pt:
          > print "INT sent to vnc server"
          >finally:
          > fpid.close()
          > flog.close()
          > os.remove(pidfi le)
          > os.remove(logfi le)
          > sys.exit(0)
          >>
          >--
          >Gabriel Genellina
          >
          >
          Indeed, I got TypeError: argument 1 must be string or read-only
          character buffer, not int
          and Wojtek's code works. So what is the right thing to do so my script
          returns 1 or 0 depending on its state and success ?
          PS: BTW how can I detach my process ie have an equivalent to
          `myscript.py&` from the python script ?

          Thanks,
          Mathieu

          Comment

          • Gabriel Genellina

            #6
            Re: subprocess seems to &quot;detach&qu ot; / ignore wait()

            En Thu, 21 Aug 2008 02:46:06 -0300, Mathieu Prevot <mathieu.prevot @ens.frescribió :
            >So what is the right thing to do so my script
            >returns 1 or 0 depending on its state and success ?
            I use something like this:

            def main(argv):
            try:
            try:
            do_things()
            return 0
            finally:
            do_cleanup()
            except:
            log_exception()
            return 1

            if __name__=='__ma in__':
            import sys
            sys.exit(main(s ys.argv))
            PS: BTW how can I detach my process ie have an equivalent to
            `myscript.py&` from the python script ?
            There are a few recipes in the Python CookBook at http://code.activestate.com/recipes/langs/python/

            --
            Gabriel Genellina

            Comment

            • Mathieu Prevot

              #7
              Re: subprocess seems to &quot;detach&qu ot; / ignore wait()

              2008/8/21 Gabriel Genellina <gagsl-py2@yahoo.com.a r>:
              En Thu, 21 Aug 2008 02:46:06 -0300, Mathieu Prevot <mathieu.prevot @ens.frescribió :
              >
              >>So what is the right thing to do so my script
              >>returns 1 or 0 depending on its state and success ?
              >
              I use something like this:
              >
              def main(argv):
              try:
              try:
              do_things()
              return 0
              finally:
              do_cleanup()
              except:
              log_exception()
              return 1
              >
              if __name__=='__ma in__':
              import sys
              sys.exit(main(s ys.argv))
              >
              >PS: BTW how can I detach my process ie have an equivalent to
              >`myscript.py &` from the python script ?
              >
              There are a few recipes in the Python CookBook at http://code.activestate..com/recipes/langs/python/
              The return from main()... it was my thought too.
              Thank you Gabriel :)

              Mathieu

              Comment

              Working...