問題描述
我想知道與 QThread 相比,從 QObject 中的常規 python 線程發出信號的后果是什么.
I would like to know what are the consequences of emitting a signal from a regular python thread within a QObject, compared with a QThread.
請參閱以下課程:
class MyObject(QtCore.QObject):
def __init__(self):
super().__init__()
sig = pyqtSignal()
def start(self):
self._thread = Thread(target=self.run)
self._thread.start()
def run(self):
self.sig.emit()
# Do something
現在,假設在 GUI 線程中,我有:
Now, assuming that in the GUI thread, I have:
def __init__(self):
self.obj = MyObject()
self.obj.sig.connect(self.slot)
self.obj.start()
def slot(self):
# Do something
slot
確實在信號發出時執行.但是,我想知道 slot
方法將在哪個線程中執行?如果我在 MyObject
中使用 QThread
而不是 python 線程,會有什么不同嗎?
the slot
is indeed executed when the signal is emitted. However, I would like to know which thread will the slot
method be executed in? Would it be any different if I used a QThread
instead of a python thread in MyObject
?
我正在使用 PyQt5 和 Python 3.
I am using PyQt5 and Python 3.
推薦答案
默認情況下,Qt 信號在跨線程發出時自動排隊.為此,它將信號參數序列化,然后將事件發布到接收線程的事件隊列,最終將執行任何連接的插槽.因此,以這種方式發出的信號保證是線程安全的.
By default, Qt automatically queues signals when they are emitted across threads. To do this, it serializes the signal parameters and then posts an event to the event-queue of the receiving thread, where any connected slots will eventually be executed. Signals emitted in this way are therefore guaranteed to be thread-safe.
關于外部線程,Qt 文檔聲明如下:
注意:Qt 的線程類是用原生線程實現的蜜蜂;例如,Win32 和 pthreads.因此,它們可以與同一原生 API 的線程.
Note: Qt's threading classes are implemented with native threading APIs; e.g., Win32 and pthreads. Therefore, they can be used with threads of the same native API.
一般來說,如果文檔聲明 Qt API 是線程安全的,該保證適用于使用同一本機庫創建的所有線程 - 而不僅僅是由 Qt 本身創建的線程.這意味著使用諸如 postEvent()
和 調用()
.
In general, if the docs state that a Qt API is thread-safe, that guarantee applies to all threads that were created using the same native library - not just the ones that were created by Qt itself. This means it is also safe to explicitly post events to other threads using such thread-safe APIs as postEvent()
and invoke()
.
因此,在發出跨線程信號時,使用 threading.Thread
和 QThread
并沒有真正的區別,只要 Python 和 Qt 都適用使用相同的底層原生線程庫.這表明在 PyQt 應用程序中更喜歡使用 QThread
的一個可能原因是 可移植性,因為這樣就不會有混合不兼容的線程實現的危險.但是,鑒于 Python 和 Qt 都被刻意設計為跨平臺,因此在實踐中不太可能出現此問題.
There is therefore no real difference between using threading.Thread
and QThread
when it comes to emitting cross-thread signals, so long as both Python and Qt use the same underlying native threading library. This suggests that one possible reason to prefer using QThread
in a PyQt application is portability, since there will then be no danger of mixing incompatible threading implementations. However, it is highly unlikely that this issue will ever arise in practice, given that both Python and Qt are deliberately designed to be cross-platform.
關于 slot
將在哪個線程中執行的問題 - 對于 Python 和 Qt,它將在 main 線程中.相比之下,run
方法將在 worker 線程中執行.在 Qt 應用程序中進行多線程處理時,這是一個非常重要的考慮因素,因為在主線程之外執行 gui 操作是不安全的.使用信號可以讓您在工作線程和 gui 之間安全地進行通信,因為連接到工作線程發出的信號的插槽將在主線程中被調用,從而允許您在必要時更新那里的 gui.
As to the question of which thread the slot
will be executed in - for both Python and Qt, it will be in the main thread. By contrast, the run
method will be executed in the worker thread. This is a very important consideration when doing multi-threading in a Qt application, because it is not safe to perform gui operations outside the main thread. Using signals allows you to safely communicate between the worker thread and the gui, because the slot connected to the signal emitted from the worker will be called in the main thread, allowing you to update the gui there if necessary.
下面是一個簡單的腳本,顯示了每個方法在哪個線程中被調用:
Below is a simple script that shows which thread each method is called in:
import sys, time, threading
from PyQt5 import QtCore, QtWidgets
def thread_info(msg):
print(msg, int(QtCore.QThread.currentThreadId()),
threading.current_thread().name)
class PyThreadObject(QtCore.QObject):
sig = QtCore.pyqtSignal()
def start(self):
self._thread = threading.Thread(target=self.run)
self._thread.start()
def run(self):
time.sleep(1)
thread_info('py:run')
self.sig.emit()
class QtThreadObject(QtCore.QThread):
sig = QtCore.pyqtSignal()
def run(self):
time.sleep(1)
thread_info('qt:run')
self.sig.emit()
class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.pyobj = PyThreadObject()
self.pyobj.sig.connect(self.pyslot)
self.pyobj.start()
self.qtobj = QtThreadObject()
self.qtobj.sig.connect(self.qtslot)
self.qtobj.start()
def pyslot(self):
thread_info('py:slot')
def qtslot(self):
thread_info('qt:slot')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(600, 100, 300, 200)
window.show()
thread_info('main')
sys.exit(app.exec_())
輸出:
main 140300376593728 MainThread
py:run 140299947104000 Thread-1
py:slot 140300376593728 MainThread
qt:run 140299871450880 Dummy-2
qt:slot 140300376593728 MainThread
這篇關于使用 QObject 從 Python 線程發出信號的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!