python - How to limit QThread number -


What is queue via QtCore.QThread . The code below starts out as running threads as it was submitted.

Import QtCore from QtCore, QtGui as line queue Que = QtGui.QApplication ([]) theQueue = queue.Queue () class TheThread (QtCore.QThread ): Def __init __ (self, theQueue, parent = none): QtCore.QThread .__ init __ (self, parent) self.theQueue = theQuef def (self): while correct: function = self.theQueue.get ( ) Self.sleep (1) self.theQueue.task_done () Thread = [] In the range I (1, 3): thread = TheThread (theQueue) threads.append (thread) thread.start () for the category (for i Lane (thread)): theQueue.put (i)

If you want now Instead of creating a list of threads, a habit To use the dirty thread pool object, you can use:

  import QtCore, QtGui queue import time range signal (QtCore.QObject) from PyQt4 import: Sig = QtCore.pyqSignal (int) class worker (QtCore.QRunnable): def __init __ (self, theQueue, sig): QtCore.QRunnable .__ init __ (self) self.theQueue = theQueue self.signal = sig def run (self) : While correct: function = self.theQueue .get () if there is no work: self.theQueue.task_done () return time.Sleep (1) print (function) self.signal.sig.emit (int (work)) Self.theQueue.task_done () def result_callback (result:) print ("understand { } ". Format (results) MAX_THREADS = 2 def launch_threads (): qu = queue.Queue () pool = QtCore.QThreadPool () pool.setMaxThreadCount (MAX_THREADS) for category (MAX_THREADS) for work: Sig = signal () sig.sig .connect (result_callback) in category for category.start (work (theQueue, sig)) (MAX_THREADS * 6): sending more values ​​than threads # queue.put (i) to # For details of the threads in the pool (MAX_ threads): Quyutput (none) pool.witfordon () qtcore.ctimer.onlineShot (0, QtGui.QApplication.instance (exit)) if __name__ == " __main__ ": app = QtGui.QApplication ([]) QtCore .QTimer.singleShot (0, launch_threads) app.exec_ ()  

Output:

  0 1 2 3 4 5 6 7 8 9 10 11 Understand 0 got 1 get 2 understand 3 understand 4 understand 5 get understanding 7 7 get 9 times 9 9 get 10 got 11  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -