PyQt5 Study Notes 15 ---- PyQt thread-related classes, pyqt515 ---- pyqt

Source: Internet
Author: User

PyQt5 Study Notes 15 ---- PyQt thread-related classes, pyqt515 ---- pyqt
QThread is the first class we will introduce in detail. It is also the core underlying class in the Qt Thread class. Due to PyQt's cross-platform feature, QThread must hide all platform-related code.


As mentioned above, to start a thread using QThread, we can create a subclass of IT and overwrite its QThread. run () function:

Class Thread (QThread): def _ init _ (self): super (Thread, self ). _ init _ () def run (self): pass # thread-related code

Then we create a new thread.

thread=Thread()thread.start()
The qthread.exe c () function is actually used for simple calls. As we mentioned above, this function actually starts an event loop.
QRunnable is the second class we will introduce. This is a lightweight abstract class used to start a task in another thread. This type of task is discarded after running. Because 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 # thread-related code
To really execute a QRunnable object, we need to use the QThreadPool class. As the name suggests, this class is used to manage a thread pool. By calling the QThreadPool. start (runnable) function, we put a QRunnable object into the execution queue of QThreadPool. Once a thread is available, the thread pool selects a QRunnable object and starts execution in that thread. All PyQt applications have a global thread pool. We can use QThreadPool. globalInstance () obtains this global thread pool. At the same time, we can also create a private thread pool and manage it manually.

task=Task()QThreadPool.globalInstance().start(task)
It should be noted that QRunnable is not a QObject, so there is no built-in interaction mechanism with other components. To interact with other components, you must write low-level thread primitives, such as using the mutex daemon to obtain results.


QtConcurrent is the last object we will introduce. This is an advanced API built on QThreadPool and is used to process most common parallel computing modes: map, reduce, and filter. It also provides the QtConcurrent. run () function to run a function in another thread. Note that QtConcurrent is a namespace rather than a class. Therefore, all functions are global functions in the namespace.


Unlike QThread and QRunnable, QtConcurrent does not require the use of low-level synchronization primitives: All QtConcurrent returns a pure uture object. This object can be used to query the current computing status (that is, the progress of the task), pause, reply, or cancel the task, and of course obtain the calculation result. Note that not all snapshot objects support the pause or cancel operation. For example, the producer uture object returned by QtConcurrent. run () cannot be canceled, but can be returned by QtConcurrent. mappedReduced. The QFutureWatcher class is used to monitor the progress of QFuture. We can use the signal slot to interact with QFutureWatcher (note that QFuture does not inherit QObject ).


Next we can compare the three types described above:
Features QThread QRunnable QtConcurrent
Advanced APIs Bytes Bytes
Task-oriented Bytes
Built-in support for pause, recovery, and cancellation Bytes Bytes
Priority Bytes Bytes
Runable event Loop Bytes Bytes

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.