PySide6.QtCore.QThreadPool¶
- class QThreadPool¶
QThreadPool
类管理一组 QThreads。更多…概要¶
属性¶
activeThreadCountᅟ
- 线程池中活动线程的数量expiryTimeoutᅟ
- 线程过期超时值,单位为毫秒maxThreadCountᅟ
- 线程池使用的最大线程数。此属性将默认为创建QThreadPool对象时QThread::idealThreadCount()的值stackSizeᅟ
- 线程池工作线程的堆栈大小threadPriorityᅟ
- 新工作线程的线程优先级
方法¶
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()
静态函数¶
def
globalInstance()
注意
本文档可能包含从C++自动翻译到Python的代码片段。我们始终欢迎对代码片段翻译的贡献。如果您发现翻译问题,您也可以通过在我们的https:/bugreports.qt.io/projects/PYSIDE上创建工单来告知我们。
详细描述¶
警告
本节包含从C++自动翻译到Python的代码片段,可能包含错误。
QThreadPool
管理和回收单个的QThread
对象,以帮助减少在使用线程的程序中创建线程的成本。每个 Qt 应用程序都有一个全局的QThreadPool
对象,可以通过调用globalInstance()
来访问。要使用
QThreadPool
中的一个线程,请子类化QRunnable
并实现run()虚函数。然后创建该类的对象并将其传递给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)
QThreadPool
默认会自动删除QRunnable
。使用setAutoDelete()
来更改自动删除标志。QThreadPool
支持通过从run()
内部调用tryStart
(this) 来多次执行相同的QRunnable
。如果启用了 autoDelete,当最后一个线程退出 run 函数时,QRunnable
将被删除。在启用 autoDelete 的情况下,多次使用相同的QRunnable
调用start()
会创建竞争条件,因此不推荐这样做。在一定时间内未使用的线程将过期。默认的过期超时时间为30000毫秒(30秒)。可以使用
setExpiryTimeout()
来更改此设置。设置负的过期超时将禁用过期机制。调用
maxThreadCount()
查询要使用的最大线程数。如果需要,可以使用setMaxThreadCount()
更改限制。默认的maxThreadCount()
是idealThreadCount()
。activeThreadCount()
函数返回当前正在工作的线程数。reserveThread()
函数为外部使用保留一个线程。当你使用完线程后,使用releaseThread()
,以便它可以被重新使用。本质上,这些函数临时增加或减少活动线程数,并且在实现对QThreadPool
不可见的耗时操作时非常有用。请注意,
QThreadPool
是一个用于管理线程的低级类,有关更高级的替代方案,请参阅 Qt Concurrent 模块。另请参阅
注意
当使用
from __feature__ import true_property
时,属性可以直接使用,否则通过访问器函数使用。- property activeThreadCountᅟ: int¶
此属性保存线程池中活动线程的数量。
- Access functions:
- property expiryTimeoutᅟ: int¶
此属性保存线程到期超时值(以毫秒为单位)。
未使用超过expiryTimeout毫秒的线程被认为已过期并将退出。这些线程将在需要时重新启动。默认的
expiryTimeout
为30000毫秒(30秒)。如果expiryTimeout
为负数,则新创建的线程不会过期,例如,它们将不会退出,直到线程池被销毁。请注意,设置
expiryTimeout
对已经运行的线程没有影响。只有新创建的线程才会使用新的expiryTimeout
。我们建议在创建线程池后立即设置expiryTimeout
,但在调用start()
之前。- Access functions:
- property maxThreadCountᅟ: int¶
此属性保存线程池使用的最大线程数。此属性将默认为创建
QThreadPool
对象时idealThreadCount()
的值。注意
线程池将始终使用至少1个线程,即使
maxThreadCount
限制为零或负数。默认的
maxThreadCount
是idealThreadCount()
。- Access functions:
- property stackSizeᅟ: int¶
此属性保存线程池工作线程的堆栈大小。
属性的值仅在线程池创建新线程时使用。更改它对已经创建或正在运行的线程没有影响。
默认值为0,这使得
QThread
使用操作系统的默认堆栈大小。- Access functions:
- property threadPriorityᅟ: QThread.Priority¶
此属性保存新工作线程的线程优先级。
属性的值仅在线程池启动新线程时使用。更改它对已经运行的线程没有影响。
默认值为
InheritPriority
,这使得QThread
使用与QThreadPool
对象所在的优先级相同的优先级。另请参阅
- Access functions:
使用给定的
parent
构造一个线程池。- activeThreadCount()¶
- Return type:
整数
属性
activeThreadCountᅟ
的获取器。- clear()¶
从队列中移除尚未开始的可运行项。对于
runnable->autoDelete()
返回true
的可运行项将被删除。另请参阅
如果
thread
是由此线程池管理的线程,则返回true
。- expiryTimeout()¶
- Return type:
整数
另请参阅
属性
expiryTimeoutᅟ
的获取器。- static globalInstance()¶
- Return type:
返回全局的
QThreadPool
实例。- maxThreadCount()¶
- Return type:
整数
另请参阅
属性
maxThreadCountᅟ
的获取器。- releaseThread()¶
释放之前通过调用
reserveThread()
保留的线程。注意
在没有预先保留线程的情况下调用此函数会暂时增加
maxThreadCount()
。这在某个线程进入休眠等待更多工作时非常有用,允许其他线程继续执行。确保在等待结束后调用reserveThread()
,以便线程池能够正确维护activeThreadCount()
。另请参阅
- reserveThread()¶
保留一个线程,忽略
activeThreadCount()
和maxThreadCount()
。当你完成线程后,调用
releaseThread()
以允许它被重用。注意
即使保留
maxThreadCount()
个线程或更多,线程池仍将允许至少一个线程。- setExpiryTimeout(expiryTimeout)¶
- Parameters:
expiryTimeout – int
另请参阅
属性
expiryTimeoutᅟ
的设置器。- setMaxThreadCount(maxThreadCount)¶
- Parameters:
maxThreadCount – int
另请参阅
属性
maxThreadCountᅟ
的设置器。- setStackSize(stackSize)¶
- Parameters:
stackSize – int
另请参阅
属性
stackSizeᅟ
的设置器。属性
threadPriorityᅟ
的设置器。- stackSize()¶
- Return type:
整数
另请参阅
属性
stackSizeᅟ
的获取器。- start(arg__1[, priority=0])¶
- Parameters:
arg__1 –
PyCallable
priority – int
- start(runnable[, priority=0])
- Parameters:
runnable –
QRunnable
priority – int
保留一个线程并使用它来运行
runnable
,除非此线程将使当前线程数超过maxThreadCount()
。在这种情况下,runnable
将被添加到运行队列中。priority
参数可用于控制运行队列的执行顺序。请注意,如果
runnable->autoDelete()
返回true
,线程池将拥有runnable
的所有权,并且在runnable->run()
返回后,线程池将自动删除runnable
。如果runnable->autoDelete()
返回false
,runnable
的所有权仍归调用者所有。请注意,在调用此函数后更改runnable
的自动删除设置会导致未定义的行为。释放之前通过
reserveThread()
保留的线程,并使用它来运行runnable
。请注意,如果
runnable->autoDelete()
返回true
,线程池将拥有runnable
的所有权,并且在runnable->run()
返回后,线程池将自动删除runnable
。如果runnable->autoDelete()
返回false
,runnable
的所有权仍归调用者所有。请注意,在调用此函数后更改runnable
的自动删除设置会导致未定义的行为。- threadPriority()¶
- Return type:
另请参阅
属性
threadPriorityᅟ
的获取器。- tryStart(callable)¶
- Parameters:
可调用 –
PyCallable
- Return type:
布尔
- tryStart(runnable)
- Parameters:
可运行的 –
QRunnable
- Return type:
布尔
尝试保留一个线程来运行
runnable
。如果在调用时没有可用的线程,则此函数不执行任何操作并返回
false
。否则,runnable
将立即使用一个可用线程运行,并且此函数返回true
。请注意,如果成功,线程池将拥有
runnable
的所有权,前提是runnable->autoDelete()
返回true
,并且runnable
将在runnable->run()
返回后由线程池自动删除。如果runnable->autoDelete()
返回false
,则runnable
的所有权仍归调用者所有。请注意,在调用此函数后更改runnable
的自动删除设置会导致未定义的行为。尝试从队列中移除指定的
runnable
,如果它尚未启动。如果runnable尚未启动,则返回true
,并且runnable
的所有权将转移给调用者(即使runnable->autoDelete() == true
)。否则返回false
。注意
如果
runnable->autoDelete() == true
,此函数可能会移除错误的可运行对象。这被称为 ABA问题:原始的runnable
可能已经执行完毕并被删除。内存被重新用于另一个可运行对象,然后被移除而不是预期的那个。因此,我们建议仅对非自动删除的可运行对象调用此函数。另请参阅
- waitForDone([deadline=QDeadlineTimer.Forever])¶
- Parameters:
截止日期 –
QDeadlineTimer
- Return type:
布尔
等待直到所有线程退出并移除线程池中的所有线程的
deadline
到期。如果所有线程都被移除,则返回true
;否则返回false
。- waitForDone(msecs)
- Parameters:
msecs – 整数
- Return type:
布尔
等待最多
msecs
毫秒,直到所有线程退出并从线程池中移除所有线程。如果所有线程都被移除,则返回true
;否则返回false
。如果msecs
为 -1,此函数将等待最后一个线程退出。