473,385 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.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()
============================================
Jul 6 '08 #1
3 2887
Mathieu Prevot <ma************@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)
Jul 6 '08 #2
Mathieu Prevot <ma************@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)
Jul 6 '08 #3
2008/7/6 Sebastian lunar Wiesner <ba***********@gmx.net>:
Mathieu Prevot <ma************@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
Jul 7 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: David S. | last post by:
Python 2.4 on Windows XP In the python command-line the following works fine: >>> from subprocess import * >>> p = Popen('dir', stdout=PIPE) >From within IDLE or PythonWin I get the following...
2
by: Xah Lee | last post by:
Python Doc Problem Example: os.system Xah Lee, 2005-09 today i'm trying to use Python to call shell commands. e.g. in Perl something like output=qx(ls) in Python i quickly located the...
2
by: Do Re Mi chel La Si Do | last post by:
Hi! This script (under Win-XP + P-2.4.1) : import subprocess p1=subprocess.Popen(r'cmd /cdir *.* /S /W /B', stdout=subprocess.PIPE) chaineretour=p1.stdout.read() run OK if called from...
2
by: Stewart Midwinter | last post by:
this has me puzzled; I've created a small test app to show the problem I'm having. I want to use subprocess to execute system commands from inside a Tkinter app running under Cygwin. When I...
0
by: Stewart Midwinter | last post by:
I have a Tkinter app running on cygwin. It includes a Test menu item that does nothing more than fetch a directory listing and display it in a Toplevel window (I'd use a tkMessageBox showinfo...
5
by: Cameron Laird | last post by:
Question: import subprocess, StringIO input = StringIO.StringIO("abcdefgh\nabc\n") # I don't know of a compact, evocative, and # cross-platform way to exhibit this behavior. # For now, depend...
12
by: bhunter | last post by:
Hi, I've used subprocess with 2.4 several times to execute a process, wait for it to finish, and then look at its output. Now I want to spawn the process separately, later check to see if it's...
25
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name for a function that created a subprocess, but the...
2
by: dudeja.rajat | last post by:
On Mon, Sep 8, 2008 at 11:50 AM, <dudeja.rajat@gmail.comwrote: Ok, I re-phrase my question: there is a batch file that executes a exe file. The batch just works if run from command prompt and...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.