Problem with subprocess.Popen wget within a thread

Discussion in 'Python' started by Mathieu Prevot, Jul 6, 2008.

  1. 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.Thread):
    def __init__(self):
    threading.Thread.__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.Popen(self.cmd.split())
    def run(self):
    self.download()

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

    (B) ============================================
    class vid(threading.Thread):
    def __init__(self):
    threading.Thread.__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.Popen(self.cmd.split(), stderr=subprocess.PIPE)
    def run(self):
    self.download()
    self.child.stderr.readlines()

    def main():
    w = vid()
    w.start()
    w.join()
    ============================================
    Mathieu Prevot, Jul 6, 2008
    #1
    1. Advertising

  2. Mathieu Prevot <>:

    > 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)
    Sebastian \lunar\ Wiesner, Jul 6, 2008
    #2
    1. Advertising

  3. Mathieu Prevot <>:

    > 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)
    Sebastian \lunar\ Wiesner, Jul 6, 2008
    #3
  4. 2008/7/6 Sebastian lunar Wiesner <>:
    > Mathieu Prevot <>:
    >
    >> 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
    Mathieu Prevot, Jul 7, 2008
    #4
    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. Jesse

    Popen and wget, problems

    Jesse, May 12, 2007, in forum: Python
    Replies:
    3
    Views:
    521
  2. 7stud

    subprocess.Popen() problem

    7stud, Jun 26, 2007, in forum: Python
    Replies:
    3
    Views:
    300
    7stud
    Jun 26, 2007
  3. The Cruisemaniac

    Problem with subprocess.Popen()

    The Cruisemaniac, Aug 1, 2007, in forum: Python
    Replies:
    1
    Views:
    462
    O.R.Senthil Kumaran
    Aug 1, 2007
  4. Tim Golden
    Replies:
    0
    Views:
    442
    Tim Golden
    Oct 7, 2007
  5. Danny Wong (dannwong)

    subprocess.Popen and thread module

    Danny Wong (dannwong), Aug 10, 2011, in forum: Python
    Replies:
    1
    Views:
    209
    Thomas Rachel
    Aug 10, 2011
Loading...

Share This Page