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

  • <tfoot id='vukC3'></tfoot>

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

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

      1. <small id='vukC3'></small><noframes id='vukC3'>

      2. 在單獨的線程中運行異步循環,信號來自和循環

        Running an asyncio loop in a separate thread, Signals from, and to loop(在單獨的線程中運行異步循環,信號來自和循環)
      3. <tfoot id='mo6rf'></tfoot>
        • <legend id='mo6rf'><style id='mo6rf'><dir id='mo6rf'><q id='mo6rf'></q></dir></style></legend>

                  <tbody id='mo6rf'></tbody>
                • <bdo id='mo6rf'></bdo><ul id='mo6rf'></ul>

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

                • <i id='mo6rf'><tr id='mo6rf'><dt id='mo6rf'><q id='mo6rf'><span id='mo6rf'><b id='mo6rf'><form id='mo6rf'><ins id='mo6rf'></ins><ul id='mo6rf'></ul><sub id='mo6rf'></sub></form><legend id='mo6rf'></legend><bdo id='mo6rf'><pre id='mo6rf'><center id='mo6rf'></center></pre></bdo></b><th id='mo6rf'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='mo6rf'><tfoot id='mo6rf'></tfoot><dl id='mo6rf'><fieldset id='mo6rf'></fieldset></dl></div>
                  本文介紹了在單獨的線程中運行異步循環,信號來自和循環的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在嘗試制作一個在后臺與多個 BLE 設備進行通信的 UI.為此,我實現了一個運行 asyncio.loop 的單獨線程.這是必要的,因為我使用 bleak 0.9.1 連接到設備.

                  I'm trying to make a UI which communicates in the background with several BLE devices. For that I've implemented a separate thread which runs an asyncio.loop. This is necessary because I use bleak 0.9.1 to connect to the devices.

                  使用信號和槽將數據從 UI 線程獲取到工作線程可以正常工作.但是,它在另一個方向上不起作用.據我所知,這是因為線程忙于運行循環并且永遠不會停止這樣做.因此,它無法處理來自 UI 線程的輸入.

                  Using signals and slots to get data from the UI-thread to the worker thread works fine. However, it does not work in the other direction. As far as I know this is because the thread is busy running the loop and never stops doing that. Therefore, it cannot process the inputs from the UI-thread.

                  下面有一個顯示問題的示例代碼.

                  Below there is an example code which shows the problem.

                  有什么方法可以在運行 asyncio 循環的同時處理線程中的輸入槽?

                  Is there any way to process the input slots in the thread while running the asyncio loop?

                  import sys
                  from PyQt5.QtWidgets import QWidget, QPushButton, QApplication, QVBoxLayout
                  from PyQt5.QtCore import QThread, QObject, pyqtSignal, pyqtSlot
                  import asyncio
                  
                  class Test_Thread(QObject):
                      signal_back = pyqtSignal(int)
                  
                      def __init__(self,
                                   loop: asyncio.AbstractEventLoop,
                                   parent=None):
                          super(Test_Thread, self).__init__(parent)
                          self.text = "Task1 not configured"
                          self.loop = loop
                          self.counter = 0
                  
                      @pyqtSlot(str)
                      def set_text_slot(self, txt):
                          self.text = txt
                  
                      async def do_stuff1(self):
                          while True:
                              print(self.text)
                              await asyncio.sleep(2.0)
                  
                      async def do_stuff2(self):
                          while True:
                              self.counter += 1
                              self.signal_back.emit(self.counter)
                              await asyncio.sleep(1.0)
                  
                      def work(self):
                          #run the event loop
                          try:
                              asyncio.ensure_future(self.do_stuff1(), loop=self.loop)
                              asyncio.ensure_future(self.do_stuff2(), loop=self.loop)
                              self.loop.run_forever()
                          finally:
                              print("Disconnect...")
                  
                  
                  class Window(QWidget):
                  
                      set_text_signal = pyqtSignal(str)
                  
                      def __init__(self, parent=None):
                          super(Window, self).__init__()
                          self.initUi()
                          self.startThread()
                  
                      def initUi(self):
                          layout = QVBoxLayout()
                          self.button = QPushButton('User input')
                          self.button.clicked.connect(self.sendtotask)
                          layout.addWidget(self.button)
                          self.setLayout(layout)
                          self.show()
                  
                      def startThread(self):
                          loop = asyncio.get_event_loop()
                          self.asyciothread = Test_Thread(loop)
                          self.thread = QThread()
                          self.asyciothread.moveToThread(self.thread)
                  
                          self.set_text_signal.connect(self.asyciothread.set_text_slot)
                          self.asyciothread.signal_back.connect(self.receivefromthread)
                          self.thread.started.connect(self.asyciothread.work)
                  
                          self.thread.start()
                  
                      @pyqtSlot(int)
                      def receivefromthread(self, number):
                          print(str(number))
                  
                      def sendtotask(self):
                          self.set_text_signal.emit("Task: Configured")
                  
                  
                  if __name__ == "__main__":
                      app = QApplication(sys.argv)
                      ui = Window()
                      ui.show()
                      sys.exit(app.exec_())
                  

                  推薦答案

                  Qt 不需要使用線程來使用 asyncio,因為有 asyncqtqasync 啟用它:

                  It is not necessary to use threads to use asyncio with Qt since there are libraries like asyncqt and qasync that enable it:

                  import asyncio
                  import sys
                  
                  from PyQt5.QtWidgets import QWidget, QPushButton, QApplication, QVBoxLayout
                  from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot
                  
                  from asyncqt import QEventLoop
                  # from qasync import QEventLoop
                  
                  
                  class Worker(QObject):
                      signal_back = pyqtSignal(int)
                  
                      def __init__(self, loop: asyncio.AbstractEventLoop, parent=None):
                          super(Worker, self).__init__(parent)
                          self.text = "Task1 not configured"
                          self.loop = loop
                          self.counter = 0
                  
                      @pyqtSlot(str)
                      def set_text_slot(self, txt):
                          self.text = txt
                  
                      async def do_stuff1(self):
                          while True:
                              print(self.text)
                              await asyncio.sleep(2.0)
                  
                      async def do_stuff2(self):
                          while True:
                              self.counter += 1
                              self.signal_back.emit(self.counter)
                              await asyncio.sleep(1.0)
                  
                      def work(self):
                          asyncio.ensure_future(self.do_stuff1(), loop=self.loop)
                          asyncio.ensure_future(self.do_stuff2(), loop=self.loop)
                  
                  
                  class Window(QWidget):
                      set_text_signal = pyqtSignal(str)
                  
                      def __init__(self, parent=None):
                          super(Window, self).__init__()
                          self.initUi()
                          self.start_task()
                  
                      def initUi(self):
                          layout = QVBoxLayout(self)
                          self.button = QPushButton("User input")
                          self.button.clicked.connect(self.sendtotask)
                          layout.addWidget(self.button)
                  
                      def start_task(self):
                          loop = asyncio.get_event_loop()
                          self.worker = Worker(loop)
                          self.set_text_signal.connect(self.worker.set_text_slot)
                          self.worker.signal_back.connect(self.receive_from_worker)
                          self.worker.work()
                  
                      @pyqtSlot(int)
                      def receive_from_worker(self, number):
                          print(str(number))
                  
                      def sendtotask(self):
                          self.set_text_signal.emit("Task: Configured")
                  
                  
                  if __name__ == "__main__":
                      app = QApplication(sys.argv)
                      loop = QEventLoop(app)
                      asyncio.set_event_loop(loop)
                      ui = Window()
                      ui.show()
                      with loop:
                          loop.run_forever()
                  

                  這篇關于在單獨的線程中運行異步循環,信號來自和循環的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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時顯示進度條?)
                • <legend id='mpXcx'><style id='mpXcx'><dir id='mpXcx'><q id='mpXcx'></q></dir></style></legend>
                    <tbody id='mpXcx'></tbody>

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

                        <tfoot id='mpXcx'></tfoot>

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

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

                            主站蜘蛛池模板: 日韩国产高清在线观看 | 在线观看国产视频 | 久久久女女女女999久久 | 亚洲国产精品久久久 | 伊人影院99| 精品日韩一区二区 | 久久99精品久久久久久 | 亚洲丝袜天堂 | 欧美精品一区二区三区四区五区 | 午夜私人影院 | 欧美日韩国产一区 | 中文字幕日韩专区 | 古典武侠第一页久久777 | 免费h在线 | 五月婷六月丁香 | 中文字幕在线二区 | 老牛影视av一区二区在线观看 | 日韩精品一区二区三区在线播放 | 中文字幕在线看人 | 91视频一区二区三区 | 欧美一级在线 | 色婷婷久久久久swag精品 | www.五月天婷婷| 手机av在线 | 污污免费网站 | 九九亚洲精品 | 视频在线亚洲 | 天天躁日日躁狠狠躁白人 | h视频免费在线观看 | 一级特黄网站 | 亚洲国产精品久久久久婷婷老年 | 成人欧美一区二区三区黑人孕妇 | 91中文字幕在线观看 | 亚洲色欲色欲www | 欧美一区二区 | 91视频a | 欧美亚洲国语精品一区二区 | 91久久综合| 国产一区二区三区高清 | 99av成人精品国语自产拍 | 久久久亚洲一区 |