簡體   English   中英

結合 PYQT GUI 顯着增加了循環時間

[英]Incorporating PYQT GUI significantly increases loop time

我一直致力於轉換一個程序,該程序將 CSV 文件作為建模應用程序的輸入,該應用程序迭代了數千個時間步,並對每個時間步進行計算。 在這種格式下,程序最多需要 1-2 分鍾才能完成。 在計算過程中,我使用了一個簡單的 TKinter 進度條來指示模擬進度。

使用 PyQT5 完成 UI 后,計算時間顯着減慢。 現在計算需要 30 多分鍾。

我嘗試過的事情:

QT 線程:我將計算推送到單獨的工作線程,如下所示: https://realpython.com/python-pyqt-qthread/

class Simulator(QObject):
    def __init__(self,inputs):
        super(Simulator, self).__init__()
        self.inputs = inputs

    finished = pyqtSignal()
    progress = pyqtSignal(int)

    def run_task(self):
        print(self.inputs)
        simEVs(self.inputs)
        self.finished.emit()

隱藏 QWidgets 和阻塞信號:對於另一個項目,我發現更新 QTable 小部件會由於發送大量信號而減慢程序速度,因此阻止小部件的信號提高了那里的性能。 在這里,我嘗試在模擬期間阻止信號並隱藏我的 UI 的所有子元素,但沒有效果。

def sleep_ui(self):
    children = self.findChildren(QWidget)
    for child in children:  #ignore the double indent here
            child.blockSignals(True)
            child.setUpdatesEnabled(False)
            child.hide()
def wake_ui(self):
    children = self.findChildren(QWidget)
    for child in children:
        child.blockSignals(True)
        child.setUpdatesEnabled(True)
        child.show()

在其他代碼實現中,我刪除了 ui window 並在計算完成后重新創建它,但我試圖在這里避免這種情況。

這是我正在談論的一個例子:

longsim() 使用 UI 需要 0.0029883 秒,不使用 UI 需要 0.0009970 秒,計算時間增加了近 300%。

longsim.py:

import pyqtgraph as pg
import numpy as np
import matplotlib
matplotlib.use('Qt5Agg')
import time
pg.mkQApp()


def longsim():
    t = time.time()
    # do stuff
    tmax = 365*24*4
    timev = np.linspace(0,tmax)

    for i in timev:
        for i in timev: # an example loop with some placeholder calculations
            output = i*100

    elapsed = time.time() - t
    print(elapsed)

堆棧交換.py:

import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import os
import matplotlib

from PyQt5 import QtWidgets, QtGui

from longloop import longsim
matplotlib.use('Qt5Agg')

pg.mkQApp()

path = os.path.dirname(os.path.abspath(__file__))
uiFile = os.path.join(path, 'Example.ui')
WindowTemplate, TemplateBaseClass = pg.Qt.loadUiType(uiFile)


class MainWindow(TemplateBaseClass):
    def __init__(self):
        TemplateBaseClass.__init__(self)
        self.setWindowTitle('window')

        # Create the main window
        self.ui = WindowTemplate()
        self.ui.setupUi(self)

        self.ui.buttonBox.clicked.connect(self.clicked)
        self.show()

    def clicked(self):
        longsim()

win = MainWindow()

if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

這是 UI 文件:example.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>640</width>
    <height>480</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <widget class="QDialogButtonBox" name="buttonBox">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>440</y>
     <width>621</width>
     <height>32</height>
    </rect>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
   <property name="standardButtons">
    <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections>
  <connection>
   <sender>buttonBox</sender>
   <signal>accepted()</signal>
   <receiver>Dialog</receiver>
   <slot>accept()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>248</x>
     <y>254</y>
    </hint>
    <hint type="destinationlabel">
     <x>157</x>
     <y>274</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>buttonBox</sender>
   <signal>rejected()</signal>
   <receiver>Dialog</receiver>
   <slot>reject()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>316</x>
     <y>260</y>
    </hint>
    <hint type="destinationlabel">
     <x>286</x>
     <y>274</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

謝謝大家的建議,通常情況下,像這樣的錯誤在其他地方。 在修復了一個應該是百分比的值(0-1 而不是 0-100)之后,循環將按應有的方式運行。 到目前為止,還沒有增加循環時間的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM