[PyQt] QThread problem
Sergio Jovani
lesergi at gmail.com
Wed Dec 10 09:32:09 GMT 2008
El Monday 08 December 2008 03:53:28 vàreu escriure:
> On Mon, Dec 8, 2008 at 12:53 AM, Sergio Jovani <lesergi at gmail.com> wrote:
> > Hi all,
> >
> > I'm developing a downloads application and I have created a QThread for
> > that reason. I start the QThread with no problems, but when I try
> > terminate it clicking on cancel pushbutton, I can't, application freezes.
> > Thanks in advance. Here the code:
> >
> > ---
> >
> > class MainWindow(QMainWindow):
> > def __init__(self, parent = None):
> > QMainWindow.__init__(self, parent)
> > ...
> > self.pbCancel=QPushButton(self.tr("Cancel"))
> > self.connect(self.pbCancel, SIGNAL("clicked()"),
> > self.cancel) ...
> > def download(self):
> > ...
> > self.threadDownload = Download(url, path, filename)
> > self.threadDownload.start()
> >
> > def cancel(self):
> > self.threadDownload.terminate()
> > self.threadDownload.wait()
> >
> > class Download(QThread):
> > def __init__(self, url, path, filename, parent = None):
> > QThread.__init__(self, parent)
> > self.path=path
> > self.url=url
> > self.filename=filename
> >
> > def run(self):
> > os.chdir(self.path)
> > urllib.urlretrieve(self.url,self.filename,reporthook=self.myreporthook)
Dears subcribers,
A few days ago, I received a lot of help trying solve this problem. Thanks for
that.
After testing and writing and erasing code, I discovered the main problem of
this. QThread is powerful but also dangerous, it have to terminate in a safe
way, if not, program will become unstable.
The main problem was the urllib2 function, because if a file is opened inside
thread, file wll have to be closed inside thread in order to terminate properly
the thread.
I hope this helps someone. I wrote a piece of code replacing
urlllib.urlretrieve function. and doing this I can close opened file before to
terminate thread.
---
class MainWindow(QMainWindow):
def __init__(self, parent = None):
QMainWindow.__init__(self, parent)
...
self.pbCancel=QPushButton(self.tr("Cancel"))
self.connect(self.pbCancel, SIGNAL("clicked()"), self.cancel)
...
def download(self):
...
self.threadDownload = Download(url, path, filename)
self.threadDownload.start()
def cancel(self):
self.threadDownload.interrupt() # Not terminate() !
self.threadDownload.wait()
class Download(QThread):
def __init__(self, url, path, filename, parent = None):
QThread.__init__(self, parent)
self.path=path
self.url=url
self.filename=filename
self.interrupted = False # We will have to change this to terminate thread!
def interrupt(self):
self.interrupted = True
def run(self):
os.chdir(self.path)
block_size = 4096
i = 0
counter = 0
temp = urllib2.urlopen(self.url)
headers = temp.info()
size = int(headers['Content-Length'])
data = open(self.filename, 'wb')
while i < size:
if not self.interrupted:
data.write(temp.read(block_size))
i += block_size
counter += 1
self.myreporthook(counter, block_size, size) # you can use reporthook!
else:
break
data.close()
temp.close()
self.quit() # terminate() too dangerous, now we can use quit() or exit(int).
---
Thanks! See you!
More information about the PyQt
mailing list