PySide6.QtCore.QThreadPool¶
- class QThreadPool¶
- The - QThreadPoolclass manages a collection of QThreads. More…- Synopsis¶- Properties¶- activeThreadCountᅟ- The number of active threads in the thread pool
- expiryTimeoutᅟ- The thread expiry timeout value in milliseconds
- maxThreadCountᅟ- The maximum number of threads used by the thread pool. This property will default to the value of QThread::idealThreadCount() at the moment the QThreadPool object is created
- stackSizeᅟ- The stack size for the thread pool worker threads
- threadPriorityᅟ- The thread priority for new worker threads
 - Methods¶- def - __init__()
- def - clear()
- def - contains()
- def - expiryTimeout()
- def - maxThreadCount()
- def - releaseThread()
- def - reserveThread()
- def - setStackSize()
- def - stackSize()
- def - start()
- def - threadPriority()
- def - tryStart()
- def - tryTake()
- def - waitForDone()
 - Static functions¶- def - globalInstance()
 - Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - Detailed Description¶- Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - QThreadPoolmanages and recycles individual- QThreadobjects to help reduce thread creation costs in programs that use threads. Each Qt application has one global- QThreadPoolobject, which can be accessed by calling- globalInstance().- To use one of the - QThreadPoolthreads, subclass- QRunnableand implement the run() virtual function. Then create an object of that class and pass it to- start().- class HelloWorldTask(QRunnable): def run(): print("Hello world from thread", QThread.currentThread()) hello = HelloWorldTask() # QThreadPool takes ownership and deletes 'hello' automatically QThreadPool.globalInstance().start(hello) - QThreadPooldeletes the- QRunnableautomatically by default. Use- setAutoDelete()to change the auto-deletion flag.- QThreadPoolsupports executing the same- QRunnablemore than once by calling- tryStart(this) from within- run(). If autoDelete is enabled the- QRunnablewill be deleted when the last thread exits the run function. Calling- start()multiple times with the same- QRunnablewhen 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 with- setMaxThreadCount(). The default- maxThreadCount()is- idealThreadCount(). The- activeThreadCount()function returns the number of threads currently doing work.- The - reserveThread()function reserves a thread for external use. Use- releaseThread()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 the- QThreadPool.- Note that - QThreadPoolis a low-level class for managing threads, see the Qt Concurrent module for higher level alternatives.- See also - Note - Properties can be used directly when - from __feature__ import true_propertyis used or via accessor functions otherwise.- property activeThreadCountᅟ: int¶
 - This property holds 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(). See- reserveThread()for more details.- See also - Access functions:
 - property expiryTimeoutᅟ: int¶
 - This property holds the thread expiry timeout value in milliseconds.. - Threads that are unused for expiryTimeout milliseconds are considered to have expired and will exit. Such threads will be restarted as needed. The default - expiryTimeoutis 30000 milliseconds (30 seconds). If- expiryTimeoutis negative, newly created threads will not expire, e.g., they will not exit until the thread pool is destroyed.- Note that setting - expiryTimeouthas no effect on already running threads. Only newly created threads will use the new- expiryTimeout. We recommend setting the- expiryTimeoutimmediately after creating the thread pool, but before calling- start().- Access functions:
 - property maxThreadCountᅟ: int¶
 - This property holds the maximum number of threads used by the thread pool. This property will default to the value of - idealThreadCount()at the moment the- QThreadPoolobject is created..- Note - The thread pool will always use at least 1 thread, even if - maxThreadCountlimit is zero or negative.- The default - maxThreadCountis- idealThreadCount().- Access functions:
 - property stackSizeᅟ: int¶
 - This property holds 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 - QThreaduse the operating system default stack size.- Access functions:
 - property threadPriorityᅟ: QThread.Priority¶
 - This property holds the thread priority for new worker threads.. - The value of the property is only used when the thread pool starts new threads. Changing it has no effect for already running threads. - The default value is - InheritPriority, which makes- QThreaduse the same priority as the one the- QThreadPoolobject lives in.- See also - Access functions:
 - Constructs a thread pool with the given - parent.- activeThreadCount()¶
- Return type:
- int 
 
 - Getter of property - activeThreadCountᅟ.- clear()¶
 - Removes the runnables that are not yet started from the queue. The runnables for which - runnable->autoDelete()returns- trueare deleted.- See also - Returns - trueif- threadis a thread managed by this thread pool.- expiryTimeout()¶
- Return type:
- int 
 - See also 
 - Getter of property - expiryTimeoutᅟ.- static globalInstance()¶
- Return type:
 
 - Returns the global - QThreadPoolinstance.- maxThreadCount()¶
- Return type:
- int 
 - See also 
 - Getter of property - maxThreadCountᅟ.- 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 call- reserveThread()when done waiting, so that the thread pool can correctly maintain the- activeThreadCount().- See also - reserveThread()¶
 - Reserves one thread, disregarding - activeThreadCount()and- maxThreadCount().- Once you are done with the thread, call - releaseThread()to allow it to be reused.- Note - Even if reserving - maxThreadCount()threads or more, the thread pool will still allow a minimum of one thread.- Note - This function will increase the reported number of active threads. This means that by using this function, it is possible for - activeThreadCount()to return a value greater than- maxThreadCount().- See also - setExpiryTimeout(expiryTimeout)¶
- Parameters:
- expiryTimeout – int 
 - See also 
 - Setter of property - expiryTimeoutᅟ.- setMaxThreadCount(maxThreadCount)¶
- Parameters:
- maxThreadCount – int 
 - See also 
 - Setter of property - maxThreadCountᅟ.- setStackSize(stackSize)¶
- Parameters:
- stackSize – int 
 - See also 
 - Setter of property - stackSizeᅟ.- Setter of property - threadPriorityᅟ.- stackSize()¶
- Return type:
- int 
 - See also 
 - Getter of property - stackSizeᅟ.- start(arg__1[, priority=0])¶
- Parameters:
- arg__1 – - PyCallable
- priority – int 
 
 
 - start(runnable[, priority=0])
- Parameters:
- runnable – - QRunnable
- priority – int 
 
 
 - Reserves a thread and uses it to run - runnable, unless this thread will make the current thread count exceed- maxThreadCount(). In that case,- runnableis added to a run queue instead. The- priorityargument can be used to control the run queue’s order of execution.- Note that the thread pool takes ownership of the - runnableif- runnable->autoDelete()returns- true, and the- runnablewill be deleted automatically by the thread pool after the- runnable->run()returns. If- runnable->autoDelete()returns- false, ownership of- runnableremains with the caller. Note that changing the auto-deletion on- runnableafter calling this functions results in undefined behavior.- Releases a thread previously reserved with - reserveThread()and uses it to run- runnable.- Note that the thread pool takes ownership of the - runnableif- runnable->autoDelete()returns- true, and the- runnablewill be deleted automatically by the thread pool after the- runnable->run()returns. If- runnable->autoDelete()returns- false, ownership of- runnableremains with the caller. Note that changing the auto-deletion on- runnableafter calling this functions results in undefined behavior.- Note - Calling this when no threads are reserved results in undefined behavior. - See also - threadPriority()¶
- Return type:
 - See also 
 - Getter of property - threadPriorityᅟ.- tryStart(callable)¶
- Parameters:
- callable – - PyCallable
- Return type:
- bool 
 
 - tryStart(runnable)
- Parameters:
- runnable – - 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,- runnableis run immediately using one available thread and this function returns- true.- Note that on success the thread pool takes ownership of the - runnableif- runnable->autoDelete()returns- true, and the- runnablewill be deleted automatically by the thread pool after the- runnable->run()returns. If- runnable->autoDelete()returns- false, ownership of- runnableremains with the caller. Note that changing the auto-deletion on- runnableafter calling this function results in undefined behavior.- Attempts to remove the specified - runnablefrom the queue if it is not yet started. If the runnable had not been started, returns- true, and ownership of- runnableis transferred to the caller (even when- runnable->autoDelete() == true). Otherwise returns- false.- Note - If - runnable->autoDelete() == true, this function may remove the wrong runnable. This is known as the ABA problem : the original- runnablemay 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 - waitForDone([deadline=QDeadlineTimer.Forever])¶
- Parameters:
- deadline – - QDeadlineTimer
- Return type:
- bool 
 
 - Waits until - deadlineexpires for all threads to exit and removes all threads from the thread pool. Returns- trueif all threads were removed; otherwise it returns- false.- waitForDone(msecs)
- Parameters:
- msecs – int 
- Return type:
- bool 
 
 - Waits up to - msecsmilliseconds for all threads to exit and removes all threads from the thread pool. Returns- trueif all threads were removed; otherwise it returns- false. If- msecsis -1, this function waits for the last thread to exit.