wxpython增加MainLoop持续时间

问题描述 投票:0回答:2

我发布了与此类似的问题,但我删除了该问题,并在一个更简单的程序中重现了该问题,该程序将在此处发布,以便希望其他人可以重现该问题并帮助我找到解决方案。

我正在使用带有自定义MainLoop的wxPython,问题是随着时间的流逝,某条指令的执行时间在增加,我不知道为什么。我在https://github.com/wxWidgets/wxPython-Classic/blob/master/samples/mainloop/mainloop.py

上找到了如何自定义MainLoop的示例

该程序旨在从大型目录读取图像,并按顺序保存许多屏幕截图,最多20,000。每个MainLoop迭代都应该继续前进到目录中的新映像。每次迭代时,图像都会在wxpython面板中设置为背景。在本示例中,我已对其进行了简化,使其仅从ini上的路径读取单个图像,然后在每次迭代中将wxPython面板的bg连续设置为该图像,而不是依次读取许多图像。

问题是面板类中的代码块的执行时间会随着时间快速增加,这是设置面板背景图像的部分。每次可能执行20次迭代之后,此块的执行时间似乎都稳定增加了15ms。这是我程序的代码。它很容易复制,您要做的就是将img路径更改为计算机上的某些img。

import time
import os
import gc
import wx

# APP
class MyApp(wx.App):

    def __init__(self):

        self.img_file_path = 'your_path.png'

        super().__init__(clearSigInt=True)

    def MainLoop(self):

        evtloop = wx.GUIEventLoop()
        #old = wx.EventLoop.GetActive()
        wx.EventLoop.SetActive(evtloop)

        while self.keepGoing:

            # this is the only instruction in the customized main loop that I added,
            # everything else is straight out of the example
            self.frame.panel.set_background_image_to_ss(self.img_file_path)

            while evtloop.Pending():
                evtloop.Dispatch()

            # i also changed the sleep from 0.1s in the example to 0.03.
            # either way the execution time increases over time.
            time.sleep(0.03)

            evtloop.ProcessIdle()

        wx.EventLoop.SetActive(old)


    def OnInit(self):

        self.frame = MyFrame(self)
        self.frame.Show(True)

        self.SetTopWindow(self.frame)

        self.keepGoing = True
        return True

# FRAME
class MyFrame(wx.Frame):

    def __init__(self, app_obj, title="CM1", pos=(100, 100), size=(760,800)):

        super().__init__(None, title=title, pos=pos, size=wx.Size(size))

        self.init_panel(app_obj)

    def init_panel(self, app_obj):

        self.panel = MyPanel(self, app_obj)

# PANEL            
class MyPanel(wx.Panel):

    def __init__(self, parent, app_obj):

        super().__init__(parent=parent)

        self.app_obj = app_obj

        self.bg_img = -1

    # PROBLEM METHOD 
    def set_background_image_to_ss(self, path):

        bmp1 = wx.Image(path, wx.BITMAP_TYPE_ANY).ConvertToBitmap()

        # I'm benchmarking the execution time of the following if/else statement
        # for some reason this increases over time
        curr_milliseconds_loop = time.time() * 1000

        if self.bg_img == -1:
            self.bitmap1 = wx.StaticBitmap(self, -1, bmp1, (0, 0))
        else:
            self.bitmap1.Destroy()
            self.bitmap1.SetBitmap(wx.Bitmap(path))

        print("SET BG BITMAP MS=", time.time() * 1000 - curr_milliseconds_loop)


app = MyApp()
app.MainLoop()

这是终端中可能进行的前100次迭代的结果输出:

SET BG BITMAP MS= 15.666748046875
SET BG BITMAP MS= 0.0
SET BG BITMAP MS= 0.0
SET BG BITMAP MS= 0.0
SET BG BITMAP MS= 15.400390625
SET BG BITMAP MS= 15.609619140625
SET BG BITMAP MS= 0.0
SET BG BITMAP MS= 15.399658203125
SET BG BITMAP MS= 15.40478515625
SET BG BITMAP MS= 15.408203125
SET BG BITMAP MS= 15.404296875
SET BG BITMAP MS= 15.400390625
SET BG BITMAP MS= 0.0
SET BG BITMAP MS= 0.0
SET BG BITMAP MS= 15.4013671875
SET BG BITMAP MS= 0.0
SET BG BITMAP MS= 0.0
SET BG BITMAP MS= 16.65966796875
SET BG BITMAP MS= 15.408935546875
SET BG BITMAP MS= 15.398193359375
SET BG BITMAP MS= 15.401611328125
SET BG BITMAP MS= 0.0
SET BG BITMAP MS= 15.589111328125
SET BG BITMAP MS= 0.0
SET BG BITMAP MS= 15.52734375
SET BG BITMAP MS= 15.4091796875
SET BG BITMAP MS= 15.39404296875
SET BG BITMAP MS= 15.388671875
SET BG BITMAP MS= 15.382080078125
SET BG BITMAP MS= 15.400390625
SET BG BITMAP MS= 31.01318359375
SET BG BITMAP MS= 15.3837890625
SET BG BITMAP MS= 15.407958984375
SET BG BITMAP MS= 15.37744140625
SET BG BITMAP MS= 15.6201171875
SET BG BITMAP MS= 15.388916015625
SET BG BITMAP MS= 15.400146484375
SET BG BITMAP MS= 15.401611328125
SET BG BITMAP MS= 15.388427734375
SET BG BITMAP MS= 15.61328125
SET BG BITMAP MS= 15.61474609375
SET BG BITMAP MS= 15.537841796875
SET BG BITMAP MS= 15.378662109375
SET BG BITMAP MS= 15.5322265625
SET BG BITMAP MS= 15.625732421875
SET BG BITMAP MS= 15.623779296875
SET BG BITMAP MS= 15.376708984375
SET BG BITMAP MS= 15.40576171875
SET BG BITMAP MS= 15.56689453125
SET BG BITMAP MS= 15.4033203125
SET BG BITMAP MS= 15.41259765625
SET BG BITMAP MS= 15.607666015625
SET BG BITMAP MS= 15.623291015625
SET BG BITMAP MS= 15.621826171875
SET BG BITMAP MS= 15.5947265625
SET BG BITMAP MS= 15.5625
SET BG BITMAP MS= 15.5693359375
SET BG BITMAP MS= 15.62109375
SET BG BITMAP MS= 31.192138671875
SET BG BITMAP MS= 31.173828125
SET BG BITMAP MS= 31.23779296875
SET BG BITMAP MS= 31.18017578125
SET BG BITMAP MS= 15.62060546875
SET BG BITMAP MS= 15.563720703125
SET BG BITMAP MS= 15.61083984375
SET BG BITMAP MS= 31.21728515625
SET BG BITMAP MS= 31.23828125
SET BG BITMAP MS= 31.23486328125
SET BG BITMAP MS= 31.23779296875
SET BG BITMAP MS= 31.23583984375
SET BG BITMAP MS= 31.236572265625
SET BG BITMAP MS= 31.23388671875
SET BG BITMAP MS= 31.23876953125
SET BG BITMAP MS= 31.27392578125
SET BG BITMAP MS= 31.236083984375
SET BG BITMAP MS= 31.23876953125
SET BG BITMAP MS= 31.18408203125
SET BG BITMAP MS= 31.235107421875
SET BG BITMAP MS= 15.621337890625
SET BG BITMAP MS= 15.616943359375
SET BG BITMAP MS= 15.62353515625
SET BG BITMAP MS= 15.62255859375
SET BG BITMAP MS= 15.62353515625
SET BG BITMAP MS= 15.622802734375
SET BG BITMAP MS= 15.619873046875
SET BG BITMAP MS= 31.2353515625
SET BG BITMAP MS= 31.229248046875
SET BG BITMAP MS= 31.2265625
SET BG BITMAP MS= 31.226806640625
SET BG BITMAP MS= 31.14990234375
SET BG BITMAP MS= 31.271728515625
SET BG BITMAP MS= 31.23095703125
SET BG BITMAP MS= 31.229736328125
SET BG BITMAP MS= 31.273193359375
SET BG BITMAP MS= 31.2353515625
SET BG BITMAP MS= 31.194091796875
SET BG BITMAP MS= 31.235107421875
SET BG BITMAP MS= 31.23828125
SET BG BITMAP MS= 31.232421875
SET BG BITMAP MS= 31.2373046875
SET BG BITMAP MS= 31.23291015625
SET BG BITMAP MS= 31.2353515625
SET BG BITMAP MS= 31.236572265625
SET BG BITMAP MS= 31.235107421875
SET BG BITMAP MS= 31.231201171875
SET BG BITMAP MS= 31.239501953125
SET BG BITMAP MS= 31.2041015625
SET BG BITMAP MS= 31.238525390625
SET BG BITMAP MS= 31.231201171875
SET BG BITMAP MS= 31.231201171875
SET BG BITMAP MS= 31.2314453125
SET BG BITMAP MS= 31.2333984375
SET BG BITMAP MS= 31.19775390625
SET BG BITMAP MS= 31.22412109375
SET BG BITMAP MS= 31.2421875
SET BG BITMAP MS= 46.819580078125
SET BG BITMAP MS= 46.85400390625
SET BG BITMAP MS= 46.846435546875
SET BG BITMAP MS= 31.23193359375
SET BG BITMAP MS= 46.853759765625
SET BG BITMAP MS= 31.2265625
SET BG BITMAP MS= 46.853759765625
SET BG BITMAP MS= 46.85595703125
SET BG BITMAP MS= 46.84912109375
SET BG BITMAP MS= 46.844970703125
SET BG BITMAP MS= 46.85107421875
SET BG BITMAP MS= 46.8525390625
SET BG BITMAP MS= 46.853271484375
SET BG BITMAP MS= 46.845703125
SET BG BITMAP MS= 46.794921875
SET BG BITMAP MS= 46.889404296875
SET BG BITMAP MS= 46.845458984375
SET BG BITMAP MS= 46.851806640625
SET BG BITMAP MS= 46.841064453125
SET BG BITMAP MS= 46.849853515625
SET BG BITMAP MS= 46.85107421875
SET BG BITMAP MS= 31.239501953125
SET BG BITMAP MS= 46.842529296875
SET BG BITMAP MS= 46.8466796875
SET BG BITMAP MS= 46.846435546875
SET BG BITMAP MS= 46.841064453125
SET BG BITMAP MS= 46.8544921875
SET BG BITMAP MS= 46.818603515625
SET BG BITMAP MS= 46.843017578125
SET BG BITMAP MS= 46.89453125
SET BG BITMAP MS= 46.789306640625
SET BG BITMAP MS= 46.848388671875
SET BG BITMAP MS= 46.843994140625
SET BG BITMAP MS= 46.8505859375
SET BG BITMAP MS= 46.849609375
SET BG BITMAP MS= 46.8486328125
SET BG BITMAP MS= 46.841796875
SET BG BITMAP MS= 46.89404296875
SET BG BITMAP MS= 46.79248046875
SET BG BITMAP MS= 46.853515625
SET BG BITMAP MS= 46.772705078125
SET BG BITMAP MS= 46.79248046875
SET BG BITMAP MS= 46.845703125
SET BG BITMAP MS= 46.81884765625
SET BG BITMAP MS= 46.848388671875
SET BG BITMAP MS= 46.845458984375
SET BG BITMAP MS= 46.853271484375
SET BG BITMAP MS= 46.8486328125
SET BG BITMAP MS= 46.842529296875
SET BG BITMAP MS= 46.84521484375
SET BG BITMAP MS= 46.845458984375
SET BG BITMAP MS= 46.8017578125
SET BG BITMAP MS= 46.8505859375
SET BG BITMAP MS= 46.857666015625
SET BG BITMAP MS= 46.853515625
SET BG BITMAP MS= 46.847900390625
SET BG BITMAP MS= 62.470458984375
SET BG BITMAP MS= 46.868408203125
SET BG BITMAP MS= 46.84228515625
SET BG BITMAP MS= 46.847412109375
SET BG BITMAP MS= 46.8515625
SET BG BITMAP MS= 46.837158203125
SET BG BITMAP MS= 62.46875
SET BG BITMAP MS= 62.46923828125
SET BG BITMAP MS= 62.474853515625
SET BG BITMAP MS= 46.85302734375
SET BG BITMAP MS= 62.447021484375
SET BG BITMAP MS= 62.474365234375
SET BG BITMAP MS= 62.471923828125
SET BG BITMAP MS= 62.43212890625
SET BG BITMAP MS= 46.848876953125
SET BG BITMAP MS= 62.472412109375
SET BG BITMAP MS= 62.468505859375
SET BG BITMAP MS= 62.51220703125
SET BG BITMAP MS= 62.41455078125
SET BG BITMAP MS= 62.417236328125
SET BG BITMAP MS= 62.47021484375
SET BG BITMAP MS= 62.501953125
SET BG BITMAP MS= 62.4287109375
SET BG BITMAP MS= 46.843994140625
SET BG BITMAP MS= 62.46630859375
SET BG BITMAP MS= 62.476806640625
SET BG BITMAP MS= 62.467041015625
SET BG BITMAP MS= 69.049560546875
SET BG BITMAP MS= 69.44189453125
SET BG BITMAP MS= 67.87939453125
SET BG BITMAP MS= 60.30517578125
SET BG BITMAP MS= 46.8681640625

我希望有人可以帮助我重现此问题,并帮助我理解为什么基准的set_background_image_to_ss方法会花费更长的时间。在经过不那么多次迭代之后,执行时间的增加确实使我的程序崩溃。谢谢。

python wxpython
2个回答
0
投票

我想我找到了解决方法。

在初始化第一个映像之后,我从未将self.bg_img设置为不等于-1,因此在添加新映像之前,else子句未执行,并且先前映像未销毁。更改此项似乎已解决了问题。

感谢那些评论过的人。


0
投票

所有图像大小都一样吗?我已将位图创建移动到面板的init中,仅将SetBitmap和图像转换保留在set_background_image_to_ss中。同时,我列出了20,000张大致相同大小的图像的虚拟列表,并且根本看不到速度的任何实际下降。

import time
import glob
import wx

# APP
class MyApp(wx.App):

    def __init__(self):
        # 2 images roughly the same size
        self.images = glob.glob('./image3/*.png')
        # falsify 20000 images
        self.images = self.images * 10000
        self.loop_count = 0
        super().__init__(clearSigInt=True)

    def MainLoop(self):

        evtloop = wx.GUIEventLoop()
        #old = wx.EventLoop.GetActive()
        wx.EventLoop.SetActive(evtloop)

        for i in self.images:

            # this is the only instruction in the customized main loop that I added,
            # everything else is straight out of the example
            self.frame.panel.set_background_image_to_ss(i)

            while evtloop.Pending():
                evtloop.Dispatch()

            # i also changed the sleep from 0.1s in the example to 0.03.
            # either way the execution time increases over time.
            time.sleep(0.01)

            evtloop.ProcessIdle()

        #wx.EventLoop.SetActive(old)


    def OnInit(self):

        self.frame = MyFrame(self)
        self.frame.Show(True)

        self.SetTopWindow(self.frame)

        self.keepGoing = True
        return True

# FRAME
class MyFrame(wx.Frame):

    def __init__(self, app_obj, title="CM1", pos=(100, 100), size=(760,800)):

        super().__init__(None, title=title, pos=pos, size=wx.Size(size))

        self.init_panel(app_obj)

    def init_panel(self, app_obj):

        self.panel = MyPanel(self, app_obj)

# PANEL
class MyPanel(wx.Panel):

    def __init__(self, parent, app_obj):

        super().__init__(parent=parent)
        self.loop = app_obj.loop_count
        self.bmp1 = wx.Image(app_obj.images[0], wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        self.bitmap1 = wx.StaticBitmap(self, -1, self.bmp1, (0, 0))

    # PROBLEM METHOD
    def set_background_image_to_ss(self, path):
        self.bmp1 = wx.Image(path, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        curr_milliseconds_loop = time.time() * 1000
        self.bitmap1.SetBitmap(self.bmp1)
        self.loop += 1
        print("Loop:",self.loop,"SET BG BITMAP MS=", time.time() * 1000 - curr_milliseconds_loop)


app = MyApp()
app.MainLoop()

开始时的速度:

Loop: 155 SET BG BITMAP MS= 1.70263671875
Loop: 156 SET BG BITMAP MS= 2.387939453125
Loop: 157 SET BG BITMAP MS= 2.416748046875
Loop: 158 SET BG BITMAP MS= 1.94580078125
Loop: 159 SET BG BITMAP MS= 1.8955078125
Loop: 160 SET BG BITMAP MS= 1.4580078125
Loop: 161 SET BG BITMAP MS= 2.04833984375
Loop: 162 SET BG BITMAP MS= 2.198486328125
Loop: 163 SET BG BITMAP MS= 1.79150390625
Loop: 164 SET BG BITMAP MS= 5.07958984375
Loop: 165 SET BG BITMAP MS= 1.69580078125

末尾速度:

Loop: 19925 SET BG BITMAP MS= 2.36962890625
Loop: 19926 SET BG BITMAP MS= 2.014892578125
Loop: 19927 SET BG BITMAP MS= 1.75390625
Loop: 19928 SET BG BITMAP MS= 1.5390625
Loop: 19929 SET BG BITMAP MS= 1.9013671875
Loop: 19930 SET BG BITMAP MS= 2.01953125
Loop: 19931 SET BG BITMAP MS= 1.921875
Loop: 19932 SET BG BITMAP MS= 1.669677734375
Loop: 19933 SET BG BITMAP MS= 1.630126953125
Loop: 19934 SET BG BITMAP MS= 2.14501953125
Loop: 19935 SET BG BITMAP MS= 2.1103515625
© www.soinside.com 2019 - 2024. All rights reserved.