QThreadPoolclass manages a collection of QThreads. 更多 …
def
activeThreadCount
()
def
cancel
(runnable)
def
clear
()
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])
def
globalInstance
()
QThreadPoolmanages and recyles individualQThreadobjects to help reduce thread creation costs in programs that use threads. Each Qt application has one globalQThreadPoolobject, which can be accessed by callingglobalInstance().To use one of the
QThreadPoolthreads, subclassQRunnable和实现 run() 虚函数。然后创建该类的对象并把它传递给start().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)
QThreadPooldeletes theQRunnable默认情况下是自动的。使用setAutoDelete()to change the auto-deletion flag.
QThreadPoolsupports executing the sameQRunnable多次通过调用tryStart(this) 从run(). If autoDelete is enabled theQRunnable将被删除当最后一个线程退出 run 函数时。调用start()multiple times with the sameQRunnablewhen 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.调用
maxThreadCount()to query the maximum number of threads to be used. If needed, you can change the limit withsetMaxThreadCount()。默认maxThreadCount()isidealThreadCount()。activeThreadCount()function returns the number of threads currently doing work.
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.注意:
QThreadPoolis a low-level class for managing threads, see the Qt Concurrent module for higher level alternatives.另请参阅
PySide2.QtCore.QThreadPool.
activeThreadCount
(
)
¶
int
PySide2.QtCore.QThreadPool.
cancel
(
runnable
)
¶
runnable
–
QRunnable
注意
此函数被弃用。
use
tryTake()
instead, but note the different deletion rules.
移除指定
runnable
from the queue if it is not yet started. The runnables for which
runnable->autoDelete()
返回
true
are deleted.
PySide2.QtCore.QThreadPool.
clear
(
)
¶
Removes the runnables that are not yet started from the queue. The runnables for which
runnable->autoDelete()
返回
true
are deleted.
另请参阅
PySide2.QtCore.QThreadPool.
expiryTimeout
(
)
¶
int
另请参阅
PySide2.QtCore.QThreadPool.
globalInstance
(
)
¶
返回全局
QThreadPool
实例。
PySide2.QtCore.QThreadPool.
maxThreadCount
(
)
¶
int
另请参阅
PySide2.QtCore.QThreadPool.
releaseThread
(
)
¶
Releases a thread previously reserved by a call to
reserveThread()
.
注意
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()
.
另请参阅
PySide2.QtCore.QThreadPool.
reserveThread
(
)
¶
Reserves one thread, disregarding
activeThreadCount()
and
maxThreadCount()
.
Once you are done with the thread, call
releaseThread()
to allow it to be reused.
注意
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 than
maxThreadCount()
.
另请参阅
PySide2.QtCore.QThreadPool.
setExpiryTimeout
(
expiryTimeout
)
¶
expiryTimeout
–
int
另请参阅
PySide2.QtCore.QThreadPool.
setMaxThreadCount
(
maxThreadCount
)
¶
maxThreadCount
–
int
另请参阅
PySide2.QtCore.QThreadPool.
setStackSize
(
stackSize
)
¶
stackSize
–
uint
另请参阅
PySide2.QtCore.QThreadPool.
stackSize
(
)
¶
uint
另请参阅
PySide2.QtCore.QThreadPool.
start
(
runnable
[
,
priority=0
]
)
¶
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,
runnable
is added to a run queue instead. The
priority
argument can be used to control the run queue’s order of execution.
Note that the thread pool takes ownership of the
runnable
if
runnable->autoDelete()
返回
true
,和
runnable
will be deleted automatically by the thread pool after the
runnable->run()
returns. If
runnable->autoDelete()
返回
false
, ownership of
runnable
remains with the caller. Note that changing the auto-deletion on
runnable
after calling this functions results in undefined behavior.
PySide2.QtCore.QThreadPool.
tryStart
(
runnable
)
¶
runnable
–
QRunnable
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
。否则,
runnable
is run immediately using one available thread and this function returns
true
.
Note that on success the thread pool takes ownership of the
runnable
if
runnable->autoDelete()
返回
true
,和
runnable
will be deleted automatically by the thread pool after the
runnable->run()
returns. If
runnable->autoDelete()
返回
false
, ownership of
runnable
remains with the caller. Note that changing the auto-deletion on
runnable
after calling this function results in undefined behavior.
PySide2.QtCore.QThreadPool.
tryTake
(
runnable
)
¶
runnable
–
QRunnable
bool
Attempts to remove the specified
runnable
from the queue if it is not yet started. If the runnable had not been started, returns
true
, and ownership of
runnable
is transferred to the caller (even when
runnable->autoDelete()
==
true
). Otherwise returns
false
.
注意
若
runnable->autoDelete()
==
true
, this function may remove the wrong runnable. This is known as the
ABA problem
: the original
runnable
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.
另请参阅
PySide2.QtCore.QThreadPool.
waitForDone
(
[
msecs=-1
]
)
¶
msecs
–
int
bool
Waits up to
msecs
milliseconds for all threads to exit and removes all threads from the thread pool. Returns
true
if all threads were removed; otherwise it returns
false
。若
msecs
is -1 (the default), the timeout is ignored (waits for the last thread to exit).