Problem with subprocess.Popen wget within a thread

M

Mathieu Prevot

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()
============================================
 
S

Sebastian \lunar\ Wiesner

Mathieu Prevot said:
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?
 
S

Sebastian \lunar\ Wiesner

Mathieu Prevot said:
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 ;)
 
M

Mathieu Prevot

2008/7/6 Sebastian lunar Wiesner said:
"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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top