PYQT5 Learning notes----PyQt thread-related classes

Source: Internet
Author: User

Qthread is the first class that we will cover in detail. It is also the core class in the Qt threading class. Due to the cross-platform nature of PyQt, Qthread hides all platform-related code.


As we said earlier, to start a thread with Qthread, we can create a subclass of it and then overwrite its qthread.run () function:

Class Thread (Qthread):    def __init__ (self):        super (Thread,self). __init__ ()    def run (self):        pass               #线程相关代码

And then we create a new thread like this.

Thread=thread () Thread.Start ()
This default implementation actually simply calls the Qthread.exec () function, and this function, as we said earlier, actually starts an event loop
Qrunnable is the second class we are going to introduce. This is a lightweight abstract class that is used to start a task for another thread. This task is discarded after it has been run. Since this class is an abstract class, we need to inherit qrunnable and then rewrite its pure virtual function qrunnable.run ():

Class Task (qrunnable):    def __init__ (self):        super (Qrunnable,self). __init__ ()    def run (self):        Pass               #线程相关代码
To actually execute a Qrunnable object, we need to use the Qthreadpool class. As the name implies, this class is used to manage a thread pool. By calling the Qthreadpool.start (runnable) function, we put a qrunnable object into the Qthreadpool execution queue. Once the threads are available, the thread pool will select a Qrunnable object and start execution on that one. All PyQt applications have a global thread pool, and we can use Qthreadpool.globalinstance () to get the global thread pool, while we can create our own private thread pool and manage it manually.

Task=task () qthreadpool.globalinstance (). Start (Task)
It is important to note that qrunnable is not a qobject, so there is no built-in mechanism for interacting with other components. In order to interact with other components, you must write your own low-level Hodohara language, such as using the mutex daemon to obtain results.


Qtconcurrent is the last object we want to introduce. This is a high-level API, built on top of Qthreadpool, to handle most common parallel computing patterns: map, reduce, and filter. It also provides the Qtconcurrent.run () function, which is used to run a function on another thread. Note that Qtconcurrent is a namespace and not a class, so all of these functions are global functions within the namespace.


Unlike Qthread and qrunnable,qtconcurrent, we do not require the use of low-level synchronization primitives: All Qtconcurrent return a Qfuture object. This object can be used to query the current operation state (i.e. the task's progress), can be used to pause/reply/Cancel the task, of course, can also be used to obtain the results of the operation. Note that not all Qfuture objects support paused or canceled operations. For example, the Qfuture object returned by Qtconcurrent.run () cannot be canceled, but is returned by qtconcurrent.mappedreduced (). The Qfuturewatcher class is used to monitor the progress of the qfuture, and we can interact with the qfuturewatcher with a signal slot (note that qfuture does not inherit Qobject).


Below we can compare the three types described above:
Characteristics QThread QRunnable QtConcurrent
Advanced API ? ? ?
Task oriented ? ? ?
Built support for pause/Resume/Cancel ? ? ?
Has priority ? ? ?
Can run event loops ? ? ?

PYQT5 Learning notes----PyQt thread-related classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.