Problem with subprocess.Popen wget within a thread

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

    Problem with subprocess.Popen wget within a thread

    Hi

    it seems the script (A) finishes before the downloading ends, and the
    (B) version doesn't (wanted behavior) ... this is unexpected. What
    happens ?


    (A) =============== =============== ==============
    class vid(threading.T hread):
    def __init__(self):
    threading.Threa d.__init__(self )
    def download(self):
    self.cmd = 'wget
    ftp://ftp.freebsd.org/pub/FreeBSD/releases/i386/ISO-IMAGES/7.0/7.0-RELEASE-i386-bootonly.iso'
    self.child = subprocess.Pope n(self.cmd.spli t())
    def run(self):
    self.download()

    def main():
    w = vid()
    w.start()
    w.join()

    (B) =============== =============== ==============
    class vid(threading.T hread):
    def __init__(self):
    threading.Threa d.__init__(self )
    def download(self):
    self.cmd = 'wget
    ftp://ftp.freebsd.org/pub/FreeBSD/releases/i386/ISO-IMAGES/7.0/7.0-RELEASE-i386-bootonly.iso'
    self.child = subprocess.Pope n(self.cmd.spli t(), stderr=subproce ss.PIPE)
    def run(self):
    self.download()
    self.child.stde rr.readlines()

    def main():
    w = vid()
    w.start()
    w.join()
    =============== =============== ==============
  • Sebastian \lunar\ Wiesner

    #2
    Re: Problem with subprocess.Pope n wget within a thread

    Mathieu Prevot <mathieu.prevot @gmail.com>:
    it seems the script (A) finishes before the downloading ends, and the
    (B) version doesn't (wanted behavior) ... this is unexpected. What
    happens ?
    I guess, the spawned process still references the pipe, so the kernel keeps
    the parent alive. Anyways, you're doing strange things. Why don't you
    just use the "wait" method, as described in the documentation, if you don't
    want to see the parent dying before its child?


    --
    Freedom is always the freedom of dissenters.
    (Rosa Luxemburg)

    Comment

    • Sebastian \lunar\ Wiesner

      #3
      Re: Problem with subprocess.Pope n wget within a thread

      Mathieu Prevot <mathieu.prevot @gmail.com>:
      it seems the script (A) finishes before the downloading ends, and the
      (B) version doesn't (wanted behavior) ... this is unexpected. What
      happens ?
      "readlines" blocks, until the pipe is closed, which usually happens, if the
      process dies.

      On the other hand, spawned processes are usually asynchronous, you have to
      explicitly _wait_ for them. And you're not waiting for it in example A.

      Anyway, the _proper_ way to wait for a child process is ... guess what ...
      the "wait" method of the Popen object ;)


      --
      Freedom is always the freedom of dissenters.
      (Rosa Luxemburg)

      Comment

      • Mathieu Prevot

        #4
        Re: Problem with subprocess.Pope n wget within a thread

        2008/7/6 Sebastian lunar Wiesner <basti.wiesner@ gmx.net>:
        Mathieu Prevot <mathieu.prevot @gmail.com>:
        >
        >it seems the script (A) finishes before the downloading ends, and the
        >(B) version doesn't (wanted behavior) ... this is unexpected. What
        >happens ?
        >
        "readlines" blocks, until the pipe is closed, which usually happens, if the
        process dies.
        >
        On the other hand, spawned processes are usually asynchronous, you have to
        explicitly _wait_ for them. And you're not waiting for it in example A.
        >
        Anyway, the _proper_ way to wait for a child process is ... guess what ...
        the "wait" method of the Popen object ;)
        Thanks :)
        Mathieu

        Comment

        Working...