|
Prev: jobangebote norddeutschland Hauswirtschafter Hauswirtschafterin stellenangebot bremen stellenangebote koeln
Next: how are strings immutable in python?
From: Mathieu Prevot on 6 Jul 2008 14:05 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() ============================================
From: "Sebastian "lunar" Wiesner" on 6 Jul 2008 14:27 Mathieu Prevot <mathieu.prevot(a)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)
From: "Sebastian "lunar" Wiesner" on 6 Jul 2008 14:33 Mathieu Prevot <mathieu.prevot(a)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)
From: Mathieu Prevot on 7 Jul 2008 09:16
2008/7/6 Sebastian lunar Wiesner <basti.wiesner(a)gmx.net>: > Mathieu Prevot <mathieu.prevot(a)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 |