久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <i id='GUnb5'><tr id='GUnb5'><dt id='GUnb5'><q id='GUnb5'><span id='GUnb5'><b id='GUnb5'><form id='GUnb5'><ins id='GUnb5'></ins><ul id='GUnb5'></ul><sub id='GUnb5'></sub></form><legend id='GUnb5'></legend><bdo id='GUnb5'><pre id='GUnb5'><center id='GUnb5'></center></pre></bdo></b><th id='GUnb5'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='GUnb5'><tfoot id='GUnb5'></tfoot><dl id='GUnb5'><fieldset id='GUnb5'></fieldset></dl></div>

    <small id='GUnb5'></small><noframes id='GUnb5'>

    • <bdo id='GUnb5'></bdo><ul id='GUnb5'></ul>

      <tfoot id='GUnb5'></tfoot>

        <legend id='GUnb5'><style id='GUnb5'><dir id='GUnb5'><q id='GUnb5'></q></dir></style></legend>

        使用 QObject 從 Python 線程發出信號

        Emitting signals from a Python thread using QObject(使用 QObject 從 Python 線程發出信號)

            <tbody id='sygP7'></tbody>

          <legend id='sygP7'><style id='sygP7'><dir id='sygP7'><q id='sygP7'></q></dir></style></legend>

          <small id='sygP7'></small><noframes id='sygP7'>

        • <tfoot id='sygP7'></tfoot>
              • <bdo id='sygP7'></bdo><ul id='sygP7'></ul>

                  <i id='sygP7'><tr id='sygP7'><dt id='sygP7'><q id='sygP7'><span id='sygP7'><b id='sygP7'><form id='sygP7'><ins id='sygP7'></ins><ul id='sygP7'></ul><sub id='sygP7'></sub></form><legend id='sygP7'></legend><bdo id='sygP7'><pre id='sygP7'><center id='sygP7'></center></pre></bdo></b><th id='sygP7'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='sygP7'><tfoot id='sygP7'></tfoot><dl id='sygP7'><fieldset id='sygP7'></fieldset></dl></div>

                  本文介紹了使用 QObject 從 Python 線程發出信號的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我想知道與 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.ThreadQThread 并沒有真正的區別,只要 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模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  How to bind a function to an Action from Qt menubar?(如何將函數綁定到 Qt 菜單欄中的操作?)
                  PyQt progress jumps to 100% after it starts(PyQt 啟動后進度躍升至 100%)
                  How to set yaxis tick label in a fixed position so that when i scroll left or right the yaxis tick label should be visible?(如何將 yaxis 刻度標簽設置在固定位置,以便當我向左或向右滾動時,yaxis 刻度標簽應該可見
                  `QImage` constructor has unknown keyword `data`(`QImage` 構造函數有未知關鍵字 `data`)
                  Change x-axis ticks to custom strings(將 x 軸刻度更改為自定義字符串)
                  How to show progress bar while saving file to excel in python?(如何在python中將文件保存為excel時顯示進度條?)

                  <i id='oYGfI'><tr id='oYGfI'><dt id='oYGfI'><q id='oYGfI'><span id='oYGfI'><b id='oYGfI'><form id='oYGfI'><ins id='oYGfI'></ins><ul id='oYGfI'></ul><sub id='oYGfI'></sub></form><legend id='oYGfI'></legend><bdo id='oYGfI'><pre id='oYGfI'><center id='oYGfI'></center></pre></bdo></b><th id='oYGfI'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='oYGfI'><tfoot id='oYGfI'></tfoot><dl id='oYGfI'><fieldset id='oYGfI'></fieldset></dl></div>
                  1. <legend id='oYGfI'><style id='oYGfI'><dir id='oYGfI'><q id='oYGfI'></q></dir></style></legend>

                    <small id='oYGfI'></small><noframes id='oYGfI'>

                          <bdo id='oYGfI'></bdo><ul id='oYGfI'></ul>

                              <tbody id='oYGfI'></tbody>
                            <tfoot id='oYGfI'></tfoot>
                            主站蜘蛛池模板: 国产精品视频一区二区三区四蜜臂 | 天天干狠狠| 日韩精品一区二区三区在线播放 | www.夜夜草 | 中文字幕在线观看视频一区 | 日韩a级片| 亚洲人成网亚洲欧洲无码 | 一区二区三区精品视频 | 91av精品| 国产高清免费视频 | 日本色高清 | 91精品久久久久久久久99蜜臂 | 亚洲成人精品久久久 | 黄色毛片黄色毛片 | 国产极品车模吞精高潮呻吟 | 黑人巨大精品欧美一区二区免费 | 精品亚洲一区二区三区四区五区 | 亚洲天堂av在线 | 亚洲久久一区 | 色婷婷亚洲国产女人的天堂 | 五月婷婷丁香婷婷 | 亚洲天堂成人在线视频 | 成人亚洲精品 | 日韩精品四区 | 精品久久久久久久久亚洲 | 精品国产欧美一区二区 | 欧美另类视频 | 一区二区三区在线看 | 精品福利av导航 | 99re6在线视频 | 正在播放国产精品 | 天天操精品视频 | 激情影院久久 | 538在线精品 | 欧美黑人体内she精在线观看 | 亚洲欧美中文日韩在线v日本 | 日韩一区二区三区在线观看 | 亚洲国产成人精品女人 | 国产亚洲欧美在线 | 久久小视频| 日韩免费视频一区二区 |