• PySide 模块
  • PySide.QtCore
  • 内容表

    上一话题

    QCoreApplication

    下一话题

    QThread

    QThreadPool

    概要

    函数

    静态函数

    详细描述

    PySide.QtCore.QThreadPool class manages a collection of QThreads.

    PySide.QtCore.QThreadPool manages and recyles individual PySide.QtCore.QThread objects to help reduce thread creation costs in programs that use threads. Each Qt application has one global PySide.QtCore.QThreadPool object, which can be accessed by calling PySide.QtCore.QThreadPool.globalInstance() .

    To use one of the PySide.QtCore.QThreadPool threads, subclass PySide.QtCore.QRunnable 和实现 run() 虚函数。然后创建该类的对象并把它传递给 QThreadPool.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)
    										

    PySide.QtCore.QThreadPool deletes the PySide.QtCore.QRunnable 默认情况下是自动的。使用 QRunnable.setAutoDelete() to change the auto-deletion flag.

    PySide.QtCore.QThreadPool supports executing the same PySide.QtCore.QRunnable more than once by calling tryStart(this) from within QRunnable.run() . If autoDelete is enabled the PySide.QtCore.QRunnable 将被删除当最后一个线程退出 run 函数时。调用 PySide.QtCore.QThreadPool.start() multiple times with the same PySide.QtCore.QRunnable 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 PySide.QtCore.QThreadPool.setExpiryTimeout() . Setting a negative expiry timeout disables the expiry mechanism.

    调用 PySide.QtCore.QThreadPool.maxThreadCount() to query the maximum number of threads to be used. If needed, you can change the limit with PySide.QtCore.QThreadPool.setMaxThreadCount() 。默认 PySide.QtCore.QThreadPool.maxThreadCount() is QThread.idealThreadCount() PySide.QtCore.QThreadPool.activeThreadCount() function returns the number of threads currently doing work.

    PySide.QtCore.QThreadPool.reserveThread() function reserves a thread for external use. Use PySide.QtCore.QThreadPool.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 PySide.QtCore.QThreadPool .

    注意: PySide.QtCore.QThreadPool is a low-level class for managing threads, see QtConcurrent.run() or the other Qt Concurrent APIs for higher level alternatives.

    class PySide.QtCore. QThreadPool ( [ parent=None ] )
    参数: parent PySide.QtCore.QObject

    构造线程池采用给定 parent .

    PySide.QtCore.QThreadPool. activeThreadCount ( )
    返回类型: PySide.QtCore.int

    This property represents the number of active threads in the thread pool.

    注意

    It is possible for this function to return a value that is greater than PySide.QtCore.QThreadPool.maxThreadCount() 。见 PySide.QtCore.QThreadPool.reserveThread() 了解更多细节。

    PySide.QtCore.QThreadPool. expiryTimeout ( )
    返回类型: PySide.QtCore.int

    Threads that are unused for expiryTimeout milliseconds are considered to have expired and will exit. Such threads will be restarted as needed. The default expiryTimeout is 30000 milliseconds (30 seconds). If expiryTimeout 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 new expiryTimeout . We recommend setting the expiryTimeout immediately after creating the thread pool, but before calling PySide.QtCore.QThreadPool.start() .

    static PySide.QtCore.QThreadPool. globalInstance ( )
    返回类型: PySide.QtCore.QThreadPool

    返回全局 PySide.QtCore.QThreadPool 实例。

    PySide.QtCore.QThreadPool. maxThreadCount ( )
    返回类型: PySide.QtCore.int

    This property represents the maximum number of threads used by the thread pool.

    注意

    The thread pool will always use at least 1 thread, even if maxThreadCount limit is zero or negative.

    默认 maxThreadCount is QThread.idealThreadCount() .

    PySide.QtCore.QThreadPool. releaseThread ( )

    Releases a thread previously reserved by a call to PySide.QtCore.QThreadPool.reserveThread() .

    注意

    Calling this function without previously reserving a thread temporarily increases PySide.QtCore.QThreadPool.maxThreadCount() . This is useful when a thread goes to sleep waiting for more work, allowing other threads to continue. Be sure to call PySide.QtCore.QThreadPool.reserveThread() when done waiting, so that the thread pool can correctly maintain the PySide.QtCore.QThreadPool.activeThreadCount() .

    PySide.QtCore.QThreadPool. reserveThread ( )

    Reserves one thread, disregarding PySide.QtCore.QThreadPool.activeThreadCount() and PySide.QtCore.QThreadPool.maxThreadCount() .

    Once you are done with the thread, call PySide.QtCore.QThreadPool.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 PySide.QtCore.QThreadPool.activeThreadCount() to return a value greater than PySide.QtCore.QThreadPool.maxThreadCount() .

    PySide.QtCore.QThreadPool. setExpiryTimeout ( expiryTimeout )
    参数: expiryTimeout PySide.QtCore.int

    Threads that are unused for expiryTimeout milliseconds are considered to have expired and will exit. Such threads will be restarted as needed. The default expiryTimeout is 30000 milliseconds (30 seconds). If expiryTimeout 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 new expiryTimeout . We recommend setting the expiryTimeout immediately after creating the thread pool, but before calling PySide.QtCore.QThreadPool.start() .

    PySide.QtCore.QThreadPool. setMaxThreadCount ( maxThreadCount )
    参数: maxThreadCount PySide.QtCore.int

    This property represents the maximum number of threads used by the thread pool.

    注意

    The thread pool will always use at least 1 thread, even if maxThreadCount limit is zero or negative.

    默认 maxThreadCount is QThread.idealThreadCount() .

    PySide.QtCore.QThreadPool. start ( runnable [ , priority=0 ] )
    参数:

    Reserves a thread and uses it to run runnable , unless this thread will make the current thread count exceed PySide.QtCore.QThreadPool.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() returns true, and the runnable will be deleted automatically by the thread pool after the runnable->run() returns. If runnable->autoDelete() returns false, ownership of runnable remains with the caller. Note that changing the auto-deletion on runnable after calling this functions results in undefined behavior.

    PySide.QtCore.QThreadPool. tryStart ( runnable )
    参数: runnable PySide.QtCore.QRunnable
    返回类型: PySide.QtCore.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 returns true.

    Note that the thread pool takes ownership of the runnable if runnable->autoDelete() returns true, and the runnable will be deleted automatically by the thread pool after the runnable->run() returns. If runnable->autoDelete() returns false, ownership of runnable remains with the caller. Note that changing the auto-deletion on runnable after calling this function results in undefined behavior.

    PySide.QtCore.QThreadPool. waitForDone ( )

    Waits for each thread to exit and removes all threads from the thread pool.

    PySide.QtCore.QThreadPool. waitForDone ( msecs )
    参数: msecs PySide.QtCore.int
    返回类型: PySide.QtCore.bool

    此函数重载 PySide.QtCore.QThreadPool.waitForDone() .

    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.