PDA

View Full Version : QThreadPool - how to remove all threads



ZioCanguro
4th October 2011, 08:38
I have a QThreadPool and I limit the max amount of thread with setMaxThreadCount.

Everything works fine but the problem is that I'm not able to delete all the threads in the pool.
THe Threads are just downloading HTML files, but I really don't know how to obtain the list of threads and what to do with them (terminate?).

Python code is perfect... C++ is... ok... I can read it 8-)

THank you

wysota
4th October 2011, 09:13
Why do you want to delete threads in the pool?

ZioCanguro
4th October 2011, 09:36
Because someone hit the "STOP" button and that shoud mean: please don't go ahead running the 100 QRunnable object that are waiting in the pool.

Take this example. I have a list with 100 urls. I iterate the list and with with urllib2 I read data for every single url. setMaxThreadCount=4 so there are 4 running threads and all the other element are waiting in the pool. I want to stop the execution of all these "waiting" thread (and if it's possible also the 4 running!)

Thank you

wysota
4th October 2011, 10:21
Then just make the runnable check whether it was canceled when it starts running. Or don't use threads at all, you don't need them for networking. Or use a queue of tasks and static threads instead of using QtConcurrent.

ZioCanguro
4th October 2011, 12:46
Maybe I'm becoming too old for programmin 8-) THis is an excerpt of my code:

class ConvertThreadDirect(QtCore.QRunnable):
def __init__(self, item, soft, itemnumber):
self.emitter = QtCore.QObject()
QtCore.QRunnable.__init__(self)
self.item = item
self.soft = soft
self.itemnumber=itemnumber
self.softname=self.soft.softname
self.str=''

def run(self):
print "inside threadDirect"
self.emitter.emit(QtCore.SIGNAL("itemSetChecking(PyQt_PyObject)"),(self.itemnumber))
self.str=self.read_page(self.soft.url, self.soft.regexp)
qlist=QtCore.QStringList([str(self.itemnumber), self.str])
self.emitter.emit(QtCore.SIGNAL("itemCheckedDirect(PyQt_PyObject)"),(self.itemnumber, self.str, self.soft))

def read_page(self, urlStr, tobefound):
pass

class MainForm(QtGui.QMainWindow):
def __init__(self, parent=None):
self.threadPool=QtCore.QThreadPool(self)
self.threadPool.setMaxThreadCount(4)
[....]
[....]
def actStop(self):
print "I have to stop all the thread not started yet!!!"
def actRun(self):
print("Run")
tree=self.ui.treeWidget
nitem=tree.topLevelItemCount()
self.nitem=0
self.pb.setRange(0, nitem)

for i in range(0,nitem):
item=tree.topLevelItem(i)
s=self.softDic[unicode(item.text(0))]
ct = ConvertThreadDirect(item, s, i)
QtCore.QObject.connect(ct.emitter,QtCore.SIGNAL('i temCheckedDirect(PyQt_PyObject)'),self.itemChecked Direct)
QtCore.QObject.connect(ct.emitter,QtCore.SIGNAL('i temSetChecking(PyQt_PyObject)'),self.itemSetChecki ng)
self.threadPool.start(ct)
print item.text(0)
self.ui.statusbar.showMessage(self.tr("Ready"))

As you can see I'm iterating over a TreeWidget and all the items are passed to a QthreadPool
You are thinking to add a check in the run method of my class ConvertThreadDirect ?
But how can I set the variable inside the run method from the main thread?

I'm confused.

Thank you

ZioCanguro
11th October 2011, 09:56
No suggestion?

wysota
11th October 2011, 10:02
Use a global variable or something... Or don't use QtConcurrent but rather a queue as I already suggested.

james_koley
22nd August 2013, 08:01
In real life, writing code that does multiple tasks in parallel introduces new problems and bugs rising from synchronization issues between the tasks. In this article I'll explore what Qt has to offer to developers writing concurrent applications, and consider the best mechanism to use for every situation. Although writing parallel applications can be a pain, Qt does a lot to ease that pain........

asadsays
6th September 2016, 19:58
In real life, writing code that does multiple tasks in parallel introduces new problems and bugs rising from synchronization issues between the tasks. In this article I'll explore what Qt has to offer to developers writing concurrent applications, and consider the best mechanism to use for every situation. Although writing parallel applications can be a pain, Qt does a lot to ease that pain........

Really appreciate your reply on this post. It’s hard to sort the good from the bad sometimes, but I think you’ve nailed it!