QThread

概要

函数

虚函数

信号

静态函数

详细描述

PySide.QtCore.QThread 类提供独立于平台的方式去管理线程。

A PySide.QtCore.QThread object manages one thread of control within the program. QThreads begin executing in PySide.QtCore.QThread.run() . By default, PySide.QtCore.QThread.run() starts the event loop by calling exec() and runs a Qt event loop inside the thread.

可以使用 Worker 对象,通过把它们移动到线程使用 QObject.moveToThread() .

<Code snippet "doc/src/snippets/code/src_corelib_thread_qthread.cpp:worker" not found>
									

然后,会在单独线程中执行 Worker 槽代码。无论如何,把 Worker 槽连接到来自任何对象、任何线程中的任何信号是自由的。跨不同线程连接信号和槽是安全的,得益于机制 queued connections .

Another way to make code run in a separate thread, is to subclass PySide.QtCore.QThread and reimplement PySide.QtCore.QThread.run() 。例如:

<Code snippet "code/src_corelib_thread_qthread.cpp:reimpl-run" not found>
									

在该范例中,线程会在 run 函数有返回之后退出。没有任何事件循环会运行在线程中,除非调用 exec() .

It is important to remember that a PySide.QtCore.QThread object usually lives in the thread where it was created, not in the thread that it manages. This oft-overlooked detail means that a PySide.QtCore.QThread ‘s slots will be executed in the context of its home thread, not in the context of the thread it is managing. For this reason, implementing new slots in a PySide.QtCore.QThread subclass is error-prone and discouraged.

注意

If you interact with an object, using any technique other than queued signal/slot connections (e.g. direct function calls), then the usual multithreading precautions need to be taken.

注意

It is not possible to change the thread affinity of GUI objects; they must remain in the main thread.

管理线程

PySide.QtCore.QThread will notifiy you via a signal when the thread is PySide.QtCore.QThread.started() , PySide.QtCore.QThread.finished() ,和 PySide.QtCore.QThread.terminated() , or you can use PySide.QtCore.QThread.isFinished() and PySide.QtCore.QThread.isRunning() to query the state of the thread.

可以停止线程通过调用 PySide.QtCore.QThread.exit() or PySide.QtCore.QThread.quit() . In extreme cases, you may want to forcibly PySide.QtCore.QThread.terminate() an executing thread. However, doing so is dangerous and discouraged. Please read the documentation for PySide.QtCore.QThread.terminate() and PySide.QtCore.QThread.setTerminationEnabled() for detailed information.

从 Qt 4.8 起,解除活在刚结束线程中的对象的分配是可能的,通过连接 PySide.QtCore.QThread.finished() signal to QObject.deleteLater() .

使用 PySide.QtCore.QThread.wait() to block the calling thread, until the other thread has finished execution (or until a specified time has passed).

静态函数 PySide.QtCore.QThread.currentThreadId() and PySide.QtCore.QThread.currentThread() return identifiers for the currently executing thread. The former returns a platform specific ID for the thread; the latter returns a PySide.QtCore.QThread 指针。

要选择给予线程的名称 (作为标识通过命令 ps -L 例如在 Linux),可以调用 PySide.QtCore.QObject.setObjectName() 在启动线程之前。若不调用 PySide.QtCore.QObject.setObjectName() ,给予线程的名称将是线程对象 Runtime (运行时) 类型的类名 (例如: "RenderThread" 在案例 Mandelbrot 范例 , as that is the name of the PySide.QtCore.QThread subclass). Note that this is currently not available with release builds on Windows.

PySide.QtCore.QThread also provides static, platform independent sleep functions: PySide.QtCore.QThread.sleep() , PySide.QtCore.QThread.msleep() ,和 PySide.QtCore.QThread.usleep() allow full second, millisecond, and microsecond resolution respectively.

注意

PySide.QtCore.QThread.wait() PySide.QtCore.QThread.sleep() functions should be unnecessary in general, since Qt is an event-driven framework. Instead of PySide.QtCore.QThread.wait() , consider listening for the PySide.QtCore.QThread.finished() signal. Instead of the PySide.QtCore.QThread.sleep() functions, consider using PySide.QtCore.QTimer .

另请参阅

Qt 中的线程支持 QThreadStorage PySide.QtCore.QMutex PySide.QtCore.QSemaphore PySide.QtCore.QWaitCondition Mandelbrot 范例 信号量范例 等待条件范例

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

构造新 PySide.QtCore.QThread 去管理新线程。 parent 拥有所有权对于 PySide.QtCore.QThread . The thread does not begin executing until PySide.QtCore.QThread.start() 被调用。

PySide.QtCore.QThread. Priority

此枚举类型指示操作系统应如何调度新创建线程。

常量 描述
QThread.IdlePriority 才调度,当没有其它线程在运行时。
QThread.LowestPriority scheduled less often than LowPriority .
QThread.LowPriority scheduled less often than NormalPriority .
QThread.NormalPriority 操作系统的默认优先级。
QThread.HighPriority scheduled more often than NormalPriority .
QThread.HighestPriority scheduled more often than HighPriority .
QThread.TimeCriticalPriority 尽可能经常调度。
QThread.InheritPriority 使用如创建线程的相同优先级。这是默认。
static PySide.QtCore.QThread. cleanup ( )
static PySide.QtCore.QThread. currentThread ( )
返回类型: PySide.QtCore.QThread

返回指针指向 PySide.QtCore.QThread 其管理目前正执行线程。

static PySide.QtCore.QThread. currentThreadId ( )
返回类型: PySide.QtCore.Qt::HANDLE

返回目前执行线程的线程句柄。

警告

由此函数返回的句柄可用于内部目的,且不应被用于任何应用程序代码中。

警告

On Windows, the returned value is a pseudo-handle for the current thread. It can't be used for numerical comparison. i.e., this function returns the DWORD (Windows-Thread ID) returned by the Win32 function getCurrentThreadId(), not the HANDLE (Windows-Thread HANDLE) returned by the Win32 function getCurrentThread().

PySide.QtCore.QThread. exec_ ( )
返回类型: PySide.QtCore.int

进入事件循环并等待,直到 PySide.QtCore.QThread.exit() is called, returning the value that was passed to PySide.QtCore.QThread.exit() . The value returned is 0 if PySide.QtCore.QThread.exit() is called via PySide.QtCore.QThread.quit() .

It is necessary to call this function to start event handling.

PySide.QtCore.QThread. exit ( [ retcode=0 ] )
参数: retcode PySide.QtCore.int

告诉线程的事件循环采用返回代码退出。

在调用此函数之后,线程离开事件循环并返回从调用 QEventLoop.exec() QEventLoop.exec() function returns returnCode .

按约定, returnCode 0 意味着成功,任何非零值指示出错。

注意:不像同名 C 库函数,此函数 does return to the caller – it is event processing that stops.

没有 QEventLoop 会在此线程中被再次启动,直到 QThread.exec() has been called again. If the eventloop in QThread.exec() is not running then the next call to QThread.exec() will also return immediately.

PySide.QtCore.QThread. finished ( )
static PySide.QtCore.QThread. idealThreadCount ( )
返回类型: PySide.QtCore.int

Returns the ideal number of threads that can be run on the system. This is done querying the number of processor cores, both real and logical, in the system. This function returns -1 if the number of processor cores could not be detected.

static PySide.QtCore.QThread. initialize ( )
PySide.QtCore.QThread. isFinished ( )
返回类型: PySide.QtCore.bool

Returns true if the thread is finished; otherwise returns false.

PySide.QtCore.QThread. isRunning ( )
返回类型: PySide.QtCore.bool

Returns true if the thread is running; otherwise returns false.

static PySide.QtCore.QThread. msleep ( arg__1 )
参数: arg__1 – long

强制当前线程休眠 msecs 毫秒。

PySide.QtCore.QThread. priority ( )
返回类型: PySide.QtCore.QThread.Priority

返回正运行线程的优先级。若线程未在运行,此函数返回 InheritPriority .

PySide.QtCore.QThread. quit ( )

Tells the thread's event loop to exit with return code 0 (success). Equivalent to calling QThread::exit(0).

此函数什么都不做,若线程没有事件循环。

PySide.QtCore.QThread. run ( )

线程的起点。先调用 PySide.QtCore.QThread.start() , the newly created thread calls this function. The default implementation simply calls exec() .

可以重实现此函数以促进高级线程管理。来自此方法的返回将结束线程的执行。

PySide.QtCore.QThread. setPriority ( priority )
参数: priority PySide.QtCore.QThread.Priority

此函数设置 priority 为正运行线程。若线程未在运行,此函数什么都不做并立即返回。使用 PySide.QtCore.QThread.start() to start a thread with a specific priority.

priority 自变量可以是任意值在 QThread::Priority 枚举除了 InheritPriorty .

作用为 priority 参数从属于操作系统的调度策略。尤其, priority will be ignored on systems that do not support thread priorities (such as on Linux, see http://linux.die.net/man/2/sched_setscheduler for more details).

PySide.QtCore.QThread. setStackSize ( stackSize )
参数: stackSize PySide.QtCore.uint

把线程的最大堆栈尺寸设为 stackSize 。若 stackSize 大于 0,最大堆栈尺寸被设为 stackSize 字节,否则,最大堆栈尺寸由操作系统自动确定。

警告

大多数操作系统对线程堆栈尺寸,有最小和最大限制。线程将无法启动,若堆栈尺寸超出这些限制。

static PySide.QtCore.QThread. setTerminationEnabled ( [ enabled=true ] )
参数: enabled PySide.QtCore.bool

启用 (或禁用) 当前线程的终止,基于 enabled 参数。线程必须已被启动由 PySide.QtCore.QThread .

enabled 为 false,终止被禁用。未来调用 QThread.terminate() will return immediately without effect. Instead, the termination is deferred until termination is enabled.

enabled 为 true,终止被启用。未来调用 QThread.terminate() will terminate the thread normally. If termination has been deferred (i.e. QThread.terminate() was called with termination disabled), this function will terminate the calling thread immediately 。注意:此函数不会返回,在此情况下。

static PySide.QtCore.QThread. sleep ( arg__1 )
参数: arg__1 – long

强制当前线程休眠 secs 秒。

PySide.QtCore.QThread. stackSize ( )
返回类型: PySide.QtCore.uint

返回线程的最大堆栈尺寸 (若设置采用 PySide.QtCore.QThread.setStackSize() ); otherwise returns zero.

PySide.QtCore.QThread. start ( [ priority=InheritPriority ] )
参数: priority PySide.QtCore.QThread.Priority

开始执行线程通过调用 PySide.QtCore.QThread.run() . The operating system will schedule the thread according to the priority 参数。若线程已经在运行,此函数什么都不做。

作用为 priority 参数从属于操作系统的调度策略。尤其, priority will be ignored on systems that do not support thread priorities (such as on Linux, see http://linux.die.net/man/2/sched_setscheduler for more details).

PySide.QtCore.QThread. started ( )
PySide.QtCore.QThread. terminate ( )

Terminates the execution of the thread. The thread may or may not be terminated immediately, depending on the operating system's scheduling policies. Listen for the PySide.QtCore.QThread.terminated() signal, or use QThread.wait() after PySide.QtCore.QThread.terminate() , to be sure.

当线程被终止时,等待线程完成的所有线程都将被唤醒。

警告

此函数是危险的,且不鼓励使用。线程可以在其代码路径中的任何点被终止。线程可以被终止,当修改数据时。线程没有机会在本身、解锁任何保持互斥、等之后被清理。简而言之,若绝对有必要才使用此函数。

终止可以被明确启用 (或禁用) 通过调用 QThread.setTerminationEnabled() . Calling this function while termination is disabled results in the termination being deferred, until termination is re-enabled. See the documentation of QThread.setTerminationEnabled() 了解更多信息。

PySide.QtCore.QThread. terminated ( )
static PySide.QtCore.QThread. usleep ( arg__1 )
参数: arg__1 – long

强制当前线程休眠 usecs 微秒。

PySide.QtCore.QThread. wait ( [ time=ULONG_MAX ] )
参数: time – long
返回类型: PySide.QtCore.bool

阻塞线程,直到满足这些条件之一:

  • 关联此线程的 PySide.QtCore.QThread 对象已执行完成 (即:当它返回从 PySide.QtCore.QThread.run() ). This function will return true if the thread has finished. It also returns true if the thread has not been started yet.
  • time milliseconds has elapsed. If time is ULONG_MAX (the default), then the wait will never timeout (the thread must return from PySide.QtCore.QThread.run() ). This function will return false if the wait timed out.

这提供的功能类似 POSIX pthread_join() 函数。

static PySide.QtCore.QThread. yieldCurrentThread ( )

把当前线程的执行产生到另一可运行线程,若有的话。注意:操作系统决定切换到哪个线程。