QThreadPool¶
The
QThreadPool
class manages a collection of QThreads. More…
Synopsis¶
Functions¶
def
activeThreadCount
()def
cancel
(runnable)def
clear
()def
contains
(thread)def
expiryTimeout
()def
maxThreadCount
()def
releaseThread
()def
reserveThread
()def
setExpiryTimeout
(expiryTimeout)def
setMaxThreadCount
(maxThreadCount)def
setStackSize
(stackSize)def
stackSize
()def
start
(runnable[, priority=0])def
tryStart
(runnable)def
tryTake
(runnable)def
waitForDone
([msecs=-1])
Static functions¶
def
globalInstance
()
Detailed Description¶
QThreadPool
manages and recycles individualQThread
objects to help reduce thread creation costs in programs that use threads. Each Qt application has one globalQThreadPool
object, which can be accessed by callingglobalInstance()
.To use one of the
QThreadPool
threads, subclassQRunnable
and implement the run() virtual function. Then create an object of that class and pass it tostart()
.class HelloWorldTask(QRunnable): def run(self): print "Hello world from thread", QThread.currentThread() hello = HelloWorldTask() # QThreadPool takes ownership and deletes 'hello' automatically QThreadPool.globalInstance().start(hello)
QThreadPool
deletes theQRunnable
automatically by default. UsesetAutoDelete()
to change the auto-deletion flag.
QThreadPool
supports executing the sameQRunnable
more than once by callingtryStart
(this) from withinrun()
. If autoDelete is enabled theQRunnable
will be deleted when the last thread exits the run function. Callingstart()
multiple times with the sameQRunnable
when autoDelete is enabled creates a race condition and is not recommended.Threads that are unused for a certain amount of time will expire. The default expiry timeout is 30000 milliseconds (30 seconds). This can be changed using
setExpiryTimeout()
. Setting a negative expiry timeout disables the expiry mechanism.Call
maxThreadCount()
to query the maximum number of threads to be used. If needed, you can change the limit withsetMaxThreadCount()
. The defaultmaxThreadCount()
isidealThreadCount()
. TheactiveThreadCount()
function returns the number of threads currently doing work.The
reserveThread()
function reserves a thread for external use. UsereleaseThread()
when your are done with the thread, so that it may be reused. Essentially, these functions temporarily increase or reduce the active thread count and are useful when implementing time-consuming operations that are not visible to theQThreadPool
.Note that
QThreadPool
is a low-level class for managing threads, see the Qt Concurrent module for higher level alternatives.See also
- class PySide2.QtCore.QThreadPool([parent=None])¶
- param parent:
Constructs a thread pool with the given
parent
.
- PySide2.QtCore.QThreadPool.activeThreadCount()¶
- Return type:
int
This property represents the number of active threads in the thread pool.
Note
It is possible for this function to return a value that is greater than
maxThreadCount()
. SeereserveThread()
for more details.See also
- PySide2.QtCore.QThreadPool.cancel(runnable)¶
- Parameters:
runnable –
PySide2.QtCore.QRunnable
Note
This function is deprecated.
use
tryTake()
instead, but note the different deletion rules.Removes the specified
runnable
from the queue if it is not yet started. The runnables for whichrunnable->autoDelete()
returnstrue
are deleted.
- PySide2.QtCore.QThreadPool.clear()¶
Removes the runnables that are not yet started from the queue. The runnables for which
runnable->autoDelete()
returnstrue
are deleted.See also
- PySide2.QtCore.QThreadPool.contains(thread)¶
- Parameters:
thread –
PySide2.QtCore.QThread
- Return type:
bool
Returns
true
ifthread
is a thread managed by this thread pool.
- PySide2.QtCore.QThreadPool.expiryTimeout()¶
- Return type:
int
Threads that are unused for
expiryTimeout
milliseconds are considered to have expired and will exit. Such threads will be restarted as needed. The defaultexpiryTimeout
is 30000 milliseconds (30 seconds). IfexpiryTimeout
is negative, newly created threads will not expire, e.g., they will not exit until the thread pool is destroyed.Note that setting
expiryTimeout
has no effect on already running threads. Only newly created threads will use the newexpiryTimeout
. We recommend setting theexpiryTimeout
immediately after creating the thread pool, but before callingstart()
.
- static PySide2.QtCore.QThreadPool.globalInstance()¶
- Return type:
Returns the global
QThreadPool
instance.
- PySide2.QtCore.QThreadPool.maxThreadCount()¶
- Return type:
int
This property represents the maximum number of threads used by the thread pool.
Note
The thread pool will always use at least 1 thread, even if
maxThreadCount
limit is zero or negative.The default
maxThreadCount
isidealThreadCount()
.
- PySide2.QtCore.QThreadPool.releaseThread()¶
Releases a thread previously reserved by a call to
reserveThread()
.Note
Calling this function without previously reserving a thread temporarily increases
maxThreadCount()
. This is useful when a thread goes to sleep waiting for more work, allowing other threads to continue. Be sure to callreserveThread()
when done waiting, so that the thread pool can correctly maintain theactiveThreadCount()
.See also
- PySide2.QtCore.QThreadPool.reserveThread()¶
Reserves one thread, disregarding
activeThreadCount()
andmaxThreadCount()
.Once you are done with the thread, call
releaseThread()
to allow it to be reused.Note
This function will always increase the number of active threads. This means that by using this function, it is possible for
activeThreadCount()
to return a value greater thanmaxThreadCount()
.See also
- PySide2.QtCore.QThreadPool.setExpiryTimeout(expiryTimeout)¶
- Parameters:
expiryTimeout – int
Threads that are unused for
expiryTimeout
milliseconds are considered to have expired and will exit. Such threads will be restarted as needed. The defaultexpiryTimeout
is 30000 milliseconds (30 seconds). IfexpiryTimeout
is negative, newly created threads will not expire, e.g., they will not exit until the thread pool is destroyed.Note that setting
expiryTimeout
has no effect on already running threads. Only newly created threads will use the newexpiryTimeout
. We recommend setting theexpiryTimeout
immediately after creating the thread pool, but before callingstart()
.
- PySide2.QtCore.QThreadPool.setMaxThreadCount(maxThreadCount)¶
- Parameters:
maxThreadCount – int
This property represents the maximum number of threads used by the thread pool.
Note
The thread pool will always use at least 1 thread, even if
maxThreadCount
limit is zero or negative.The default
maxThreadCount
isidealThreadCount()
.
- PySide2.QtCore.QThreadPool.setStackSize(stackSize)¶
- Parameters:
stackSize –
uint
This property contains the stack size for the thread pool worker threads.
The value of the property is only used when the thread pool creates new threads. Changing it has no effect for already created or running threads.
The default value is 0, which makes
QThread
use the operating system default stack size.
- PySide2.QtCore.QThreadPool.stackSize()¶
- Return type:
uint
This property contains the stack size for the thread pool worker threads.
The value of the property is only used when the thread pool creates new threads. Changing it has no effect for already created or running threads.
The default value is 0, which makes
QThread
use the operating system default stack size.
- PySide2.QtCore.QThreadPool.start(runnable[, priority=0])¶
- Parameters:
runnable –
PySide2.QtCore.QRunnable
priority – int
Reserves a thread and uses it to run
runnable
, unless this thread will make the current thread count exceedmaxThreadCount()
. In that case,runnable
is added to a run queue instead. Thepriority
argument can be used to control the run queue’s order of execution.Note that the thread pool takes ownership of the
runnable
ifrunnable->autoDelete()
returnstrue
, and therunnable
will be deleted automatically by the thread pool after therunnable->run()
returns. Ifrunnable->autoDelete()
returnsfalse
, ownership ofrunnable
remains with the caller. Note that changing the auto-deletion onrunnable
after calling this functions results in undefined behavior.
- PySide2.QtCore.QThreadPool.tryStart(runnable)¶
- Parameters:
runnable –
PySide2.QtCore.QRunnable
- Return type:
bool
Attempts to reserve a thread to run
runnable
.If no threads are available at the time of calling, then this function does nothing and returns
false
. Otherwise,runnable
is run immediately using one available thread and this function returnstrue
.Note that on success the thread pool takes ownership of the
runnable
ifrunnable->autoDelete()
returnstrue
, and therunnable
will be deleted automatically by the thread pool after therunnable->run()
returns. Ifrunnable->autoDelete()
returnsfalse
, ownership ofrunnable
remains with the caller. Note that changing the auto-deletion onrunnable
after calling this function results in undefined behavior.
- PySide2.QtCore.QThreadPool.tryTake(runnable)¶
- Parameters:
runnable –
PySide2.QtCore.QRunnable
- Return type:
bool
Attempts to remove the specified
runnable
from the queue if it is not yet started. If the runnable had not been started, returnstrue
, and ownership ofrunnable
is transferred to the caller (even whenrunnable->autoDelete() == true
). Otherwise returnsfalse
.Note
If
runnable->autoDelete() == true
, this function may remove the wrong runnable. This is known as the ABA problem : the originalrunnable
may already have executed and has since been deleted. The memory is re-used for another runnable, which then gets removed instead of the intended one. For this reason, we recommend calling this function only for runnables that are not auto-deleting.See also
- PySide2.QtCore.QThreadPool.waitForDone([msecs=-1])¶
- Parameters:
msecs – int
- Return type:
bool
Waits up to
msecs
milliseconds for all threads to exit and removes all threads from the thread pool. Returnstrue
if all threads were removed; otherwise it returnsfalse
. Ifmsecs
is -1 (the default), the timeout is ignored (waits for the last thread to exit).
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.